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

217 lines
6.6 KiB
PHP
Raw Permalink Normal View History

2022-06-01 13:41:24 +02:00
<?php
2022-07-04 10:17:34 +00:00
namespace App\Traits\Requests;
2022-06-01 13:41:24 +02:00
use Illuminate\Support\Facades\File;
trait HasPicture
{
/**
2022-07-04 10:17:34 +00:00
* Minimum width
*
* @var integer
*/
private $minWith = 400;
/**
* Maximum width
*
* @var integer
*/
private $maxWith = 1800;
/**
* Minimum height
*
* @var integer
*/
private $minHeight = 300;
/**
* Maximum height
2022-06-01 13:41:24 +02:00
*
2022-07-04 10:17:34 +00:00
* @var integer
*/
private $maxHeight = 1800;
/**
* Image rules
*
2022-07-18 08:24:42 +00:00
* @param string $name
2022-07-04 10:17:34 +00:00
* @param integer|null $minWith
* @param integer|null $minHeight
* @param integer|null $maxWith
* @param integer|null $maxHeight
2022-06-01 13:41:24 +02:00
* @return array
*/
2022-07-04 10:17:34 +00:00
public function pictureRules(
2022-07-18 08:24:42 +00:00
string $name = 'picture',
2022-07-04 10:17:34 +00:00
?int $minWith = null,
?int $minHeight = null,
?int $maxWith = null,
?int $maxHeight = null
): array {
2022-07-18 08:24:42 +00:00
$sizes = (object)[
'minWith' => $minWith ?? $this->minWith,
'minHeight' => $minHeight ?? $this->minHeight,
'maxWith' => $maxWith ?? $this->maxWith,
'maxHeight' => $maxHeight ?? $this->maxHeight
];
2022-06-01 13:41:24 +02:00
return [
2022-07-18 08:24:42 +00:00
$name => [
2022-09-23 17:19:00 +02:00
'sometimes',
2022-07-18 08:24:42 +00:00
function (string $attribute, $value, callable $fail) use ($sizes) {
return $this->validatePicture($attribute, $value, $fail, $sizes);
2022-06-01 13:41:24 +02:00
}
]
];
}
/**
* Images rules
*
2022-07-18 08:24:42 +00:00
* @param string $name
2022-07-04 10:17:34 +00:00
* @param integer|null $minWith
* @param integer|null $minHeight
* @param integer|null $maxWith
* @param integer|null $maxHeight
2022-06-01 13:41:24 +02:00
* @return array
*/
2022-07-04 10:17:34 +00:00
public function picturesRules(
2022-07-18 08:24:42 +00:00
string $name = 'pictures',
2022-07-04 10:17:34 +00:00
?int $minWith = null,
?int $minHeight = null,
?int $maxWith = null,
?int $maxHeight = null
): array {
2022-07-18 08:24:42 +00:00
$sizes = (object)[
'minWith' => $minWith ?? $this->minWith,
'minHeight' => $minHeight ?? $this->minHeight,
'maxWith' => $maxWith ?? $this->maxWith,
'maxHeight' => $maxHeight ?? $this->maxHeight
];
2022-06-01 13:41:24 +02:00
return [
2022-07-18 08:24:42 +00:00
$name => 'sometimes|array',
"{$name}Validate" => "required_with:{$name}|array",
"{$name}Validate.*" => [
2022-06-01 13:41:24 +02:00
'required',
2022-07-18 08:24:42 +00:00
function (string $attribute, $value, callable $fail) use ($sizes) {
return $this->validatePicture($attribute, $value, $fail, $sizes);
2022-06-01 13:41:24 +02:00
}
]
];
}
/**
* Validate picture
*
* @param string $attribute
* @param mixed $value
* @param callback $fail
2022-07-18 08:24:42 +00:00
* @param object $sizes
2022-06-01 13:41:24 +02:00
* @return void
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
*/
2022-07-18 08:24:42 +00:00
private function validatePicture(string $attribute, $value, callable $fail, object $sizes)
2022-06-01 13:41:24 +02:00
{
if (!File::exists($value)) {
$fail(trans(":attribute n'existe pas dans le système de fichier"));
return;
}
2022-07-04 10:17:34 +00:00
if (!\in_array(File::mimeType($value), ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'])) {
$fail(trans(":attribute doit être une image de type 'image/jpeg,image/jpg,image/png,image/gif'"));
2022-06-01 13:41:24 +02:00
}
if (!($dimensions = \getimagesize($value)) or !isset($dimensions[0]) or !isset($dimensions[1])) {
$fail(trans(":attribute impossible de valider les dimensions"));
} else {
2022-07-04 10:17:34 +00:00
$width = intval($dimensions[0]);
$height = intval($dimensions[1]);
$xMessage = trans(
2022-07-18 08:24:42 +00:00
":attribute la largeur doit être comprise entre {$sizes->minWith}px et {$sizes->maxWith}px"
2022-07-04 10:17:34 +00:00
);
2022-07-18 08:24:42 +00:00
if ($sizes->minWith === $sizes->maxWith) {
$xMessage = trans(":attribute la largeur doit être de {$sizes->minWith}px");
2022-06-01 13:41:24 +02:00
}
2022-07-04 10:17:34 +00:00
$yMessage = trans(
2022-07-18 08:24:42 +00:00
":attribute la hauteur doit être comprise entre {$sizes->minWith}px et {$sizes->maxWith}px"
2022-07-04 10:17:34 +00:00
);
2022-07-18 08:24:42 +00:00
if ($sizes->minHeight === $sizes->maxHeight) {
$yMessage = trans(":attribute la hauteur doit être de {$sizes->minWith}px");
2022-06-01 13:41:24 +02:00
}
2022-07-04 10:17:34 +00:00
2022-07-18 08:24:42 +00:00
if (($width < $sizes->minWith) or ($width > $sizes->maxWith)) {
2022-07-04 10:17:34 +00:00
$fail($xMessage);
}
2022-07-18 08:24:42 +00:00
if (($height < $sizes->minHeight) or ($height > $sizes->maxHeight)) {
2022-07-04 10:17:34 +00:00
$fail($yMessage);
}
} //end if
2022-06-01 13:41:24 +02:00
}
/**
* Merges products pictures fields with request data
*
2022-07-18 08:24:42 +00:00
* @param string $name
2022-06-01 13:41:24 +02:00
* @return void
*/
2022-07-18 08:24:42 +00:00
protected function mergePicture(string $name = 'picture'): void
2022-06-01 13:41:24 +02:00
{
2022-07-18 08:24:42 +00:00
$picturePath = $this->{$name};
2022-06-01 13:41:24 +02:00
if (is_string($picturePath)) {
2022-07-18 08:24:42 +00:00
$this->merge(["{$name}Validate" => []]);
2022-06-01 13:41:24 +02:00
// ! Empty picture.
if ($picturePath === 'false') {
// * Empty picture and exit
2022-07-18 08:24:42 +00:00
$this->merge([$name => []]);
2022-06-01 13:41:24 +02:00
return;
}
$decodedPath = \urldecode($picturePath);
$this->merge([
2022-07-18 08:24:42 +00:00
"{$name}Validate" => array_merge(
$this->{"{$name}Validate"},
2022-06-01 13:41:24 +02:00
[\public_path(ltrim($decodedPath, '/'))]
)
]);
$changedPicture = ltrim($decodedPath, '/');
$this->merge([
2022-07-18 08:24:42 +00:00
$name => $changedPicture
2022-06-01 13:41:24 +02:00
]);
} //end if
}
/**
* Merges products pictures fields with request data
*
2022-07-18 08:24:42 +00:00
* @param string $name
2022-06-01 13:41:24 +02:00
* @return void
*/
2022-07-18 08:24:42 +00:00
protected function mergePictures(string $name = 'pictures'): void
2022-06-01 13:41:24 +02:00
{
2022-07-18 08:24:42 +00:00
$pictures = $this->{$name};
2022-06-01 13:41:24 +02:00
if (is_array($pictures)) {
2022-07-18 08:24:42 +00:00
$this->merge(["{$name}Validate" => []]);
2022-06-01 13:41:24 +02:00
foreach ($pictures as $key => $picturePath) {
// ! Empty picture.
if ($picturePath === 'false') {
// * Empty picture and exit
2022-07-18 08:24:42 +00:00
$this->merge([$name => []]);
2022-06-01 13:41:24 +02:00
return;
}
$decodedPath = \urldecode($picturePath);
$this->merge([
2022-07-18 08:24:42 +00:00
"{$name}Validate" => array_merge(
$this->{"{$name}Validate"},
2022-06-01 13:41:24 +02:00
[\public_path(ltrim($decodedPath, '/'))]
)
]);
2022-07-18 08:24:42 +00:00
$changedPicture = $this->{$name};
2022-06-01 13:41:24 +02:00
$changedPicture[$key] = ltrim($decodedPath, '/');
$this->merge([
2022-07-18 08:24:42 +00:00
$name => $changedPicture
2022-06-01 13:41:24 +02:00
]);
}
} //end if
}
}