ldc-volena/app/Traits/Requests/HasPicture.php

190 lines
5.7 KiB
PHP
Raw Permalink Normal View History

2023-11-24 11:11:32 +01:00
<?php
namespace App\Traits\Requests;
use App\Lib\Helpers\ToolboxHelper;
/**
* ! This assumes file system is using local storage
*/
trait HasPicture
{
/**
2024-03-20 17:39:24 +01:00
* Minimum width.
2023-11-24 11:11:32 +01:00
*
* @var integer
*/
private $minWidth = 400;
2023-11-24 11:11:32 +01:00
/**
2024-03-20 17:39:24 +01:00
* Maximum width.
2023-11-24 11:11:32 +01:00
*
* @var integer
*/
private $maxWidth = 1800;
2023-11-24 11:11:32 +01:00
/**
2024-03-20 17:39:24 +01:00
* Minimum height.
2023-11-24 11:11:32 +01:00
*
* @var integer
*/
private $minHeight = 300;
/**
2024-03-20 17:39:24 +01:00
* Maximum height.
2023-11-24 11:11:32 +01:00
*
* @var integer
*/
private $maxHeight = 1800;
/**
2024-03-20 17:39:24 +01:00
* Image rules.
2023-11-24 11:11:32 +01:00
*
* @param string $name
* @param integer|null $minWidth
2023-11-24 11:11:32 +01:00
* @param integer|null $minHeight
* @param integer|null $maxWidth
2023-11-24 11:11:32 +01:00
* @param integer|null $maxHeight
* @param boolean $nullable
* @param integer|null $size
2023-11-24 11:11:32 +01:00
* @param string|null $requiredWith
* @param string|null $requiredWithout
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
2023-11-24 11:11:32 +01:00
*/
public function pictureRules(
string $name = 'picture',
?int $minWidth = null,
2023-11-24 11:11:32 +01:00
?int $minHeight = null,
?int $maxWidth = null,
2023-11-24 11:11:32 +01:00
?int $maxHeight = null,
bool $nullable = false,
?int $size = null,
2023-11-24 11:11:32 +01:00
?string $requiredWith = null,
?string $requiredWithout = null,
2023-11-24 11:11:32 +01:00
): array {
$dimensions = [
'minWidth' => $minWidth ?? $this->minWidth,
2023-11-24 11:11:32 +01:00
'minHeight' => $minHeight ?? $this->minHeight,
'maxWidth' => $maxWidth ?? $this->maxWidth,
2023-11-24 11:11:32 +01:00
'maxHeight' => $maxHeight ?? $this->maxHeight
];
return [
$name => [
!$nullable ? 'required' : 'nullable',
(strlen($requiredWith)) ? 'required_with:' . $requiredWith : '',
(strlen($requiredWithout)) ? 'required_without:' . $requiredWithout : '',
2023-11-24 11:11:32 +01:00
($size) ? 'max:' . $size : '',
function (string $attribute, $value, callable $fail) use ($dimensions) {
ToolboxHelper::validatePicture($attribute, $value, $fail, $dimensions);
2023-11-24 11:11:32 +01:00
}
]
];
}
/**
2024-03-20 17:39:24 +01:00
* Images rules.
2023-11-24 11:11:32 +01:00
*
* @param string $name
* @param integer|null $minWidth
2023-11-24 11:11:32 +01:00
* @param integer|null $minHeight
* @param integer|null $maxWidth
2023-11-24 11:11:32 +01:00
* @param integer|null $maxHeight
* @param integer $limitMin
* @param integer $limitMax
* @param boolean $nullable
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
2023-11-24 11:11:32 +01:00
*/
public function picturesRules(
string $name = 'pictures',
?int $minWidth = null,
2023-11-24 11:11:32 +01:00
?int $minHeight = null,
?int $maxWidth = null,
?int $maxHeight = null,
int $limitMin = 1,
int $limitMax = 5,
bool $nullable = false
2023-11-24 11:11:32 +01:00
): array {
$sizes = [
'minWidth' => $minWidth ?? $this->minWidth,
2023-11-24 11:11:32 +01:00
'minHeight' => $minHeight ?? $this->minHeight,
'maxWidth' => $maxWidth ?? $this->maxWidth,
'maxHeight' => $maxHeight ?? $this->maxHeight,
2023-11-24 11:11:32 +01:00
];
return [
$name => [!$nullable ? 'required' : 'nullable', 'array', "between:{$limitMin},{$limitMax}"],
2023-11-24 11:11:32 +01:00
"{$name}Validate" => "required_with:{$name}|array",
"{$name}Validate.*" => [
'required',
function (string $attribute, $value, callable $fail) use ($sizes) {
ToolboxHelper::validatePicture($attribute, $value, $fail, $sizes);
2023-11-24 11:11:32 +01:00
}
]
];
}
/**
2024-03-20 17:39:24 +01:00
* Merges products pictures fields with request data.
2023-11-24 11:11:32 +01:00
*
* @param string $name
* @return void
*/
protected function mergePicture(string $name = 'picture'): void
{
$picturePath = $this->{$name};
if (is_string($picturePath)) {
$this->merge(["{$name}Validate" => []]);
// ! Empty picture.
if ($picturePath === 'false') {
// * Empty picture and exit
$this->merge([$name => []]);
return;
}
$decodedPath = \urldecode($picturePath);
$this->merge([
"{$name}Validate" => array_merge(
$this->{"{$name}Validate"},
[\public_path(ltrim($decodedPath, '/'))]
)
]);
$changedPicture = ltrim($decodedPath, '/');
$this->merge([
$name => $changedPicture
]);
} //end if
}
/**
2024-03-20 17:39:24 +01:00
* Merges products pictures fields with request data.
2023-11-24 11:11:32 +01:00
*
* @param string $name
* @return void
*/
protected function mergePictures(string $name = 'pictures'): void
{
$pictures = $this->{$name};
if (is_array($pictures)) {
$this->merge(["{$name}Validate" => []]);
foreach ($pictures as $key => $picturePath) {
// ! Empty picture.
if ($picturePath === 'false') {
// * Empty picture and exit
$this->merge([$name => []]);
return;
}
$decodedPath = \urldecode($picturePath);
$this->merge([
"{$name}Validate" => array_merge(
$this->{"{$name}Validate"},
[\public_path(ltrim($decodedPath, '/'))]
)
]);
$changedPicture = $this->{$name};
$changedPicture[$key] = ltrim($decodedPath, '/');
$this->merge([
$name => $changedPicture
]);
}
} //end if
}
}