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

173 lines
4.9 KiB
PHP
Raw Permalink Normal View History

2024-01-10 14:49:46 +01:00
<?php
namespace App\Traits\Requests;
use App\Lib\Helpers\ToolboxHelper;
/**
* ! This assumes file system is using local storage
*/
trait HasPicture
{
/**
* Minimum width
*
2026-09-25 16:25:31 +02:00
* @var int
2024-01-10 14:49:46 +01:00
*/
private $minWidth = 400;
/**
* Maximum width
*
2026-09-25 16:25:31 +02:00
* @var int
2024-01-10 14:49:46 +01:00
*/
private $maxWidth = 1800;
/**
* Minimum height
*
2026-09-25 16:25:31 +02:00
* @var int
2024-01-10 14:49:46 +01:00
*/
private $minHeight = 300;
/**
* Maximum height
*
2026-09-25 16:25:31 +02:00
* @var int
2024-01-10 14:49:46 +01:00
*/
private $maxHeight = 1800;
/**
* Image rules.
2024-01-10 14:49:46 +01:00
*/
public function pictureRules(
string $name = 'picture',
?int $minWidth = null,
?int $minHeight = null,
?int $maxWidth = null,
?int $maxHeight = null,
bool $nullable = false,
?int $size = null,
?string $requiredWith = null,
?string $requiredWithout = null,
2024-04-16 16:53:38 +02:00
bool $sometimes = false,
2024-01-10 14:49:46 +01:00
): array {
$dimensions = (object) [
2026-09-25 16:25:31 +02:00
'minWidth' => $minWidth ?? $this->minWidth,
2024-01-10 14:49:46 +01:00
'minHeight' => $minHeight ?? $this->minHeight,
2026-09-25 16:25:31 +02:00
'maxWidth' => $maxWidth ?? $this->maxWidth,
'maxHeight' => $maxHeight ?? $this->maxHeight,
2024-01-10 14:49:46 +01:00
];
2026-09-25 16:25:31 +02:00
2024-01-10 14:49:46 +01:00
return [
$name => [
2024-04-16 16:53:38 +02:00
$sometimes ? 'sometimes' : '',
2026-09-25 16:25:31 +02:00
! $nullable ? 'required' : 'nullable',
2024-01-10 14:49:46 +01:00
(strlen($requiredWith)) ? 'required_with:' . $requiredWith : '',
(strlen($requiredWithout)) ? 'required_without:' . $requiredWithout : '',
($size) ? 'max:' . $size : '',
function (string $attribute, $value, callable $fail) use ($dimensions) {
2024-01-30 10:43:25 +01:00
ToolboxHelper::validatePicture($attribute, $value, $fail, $dimensions);
2026-09-25 16:25:31 +02:00
},
],
2024-01-10 14:49:46 +01:00
];
}
/**
* Images rules.
2024-01-10 14:49:46 +01:00
*/
public function picturesRules(
string $name = 'pictures',
?int $minWidth = null,
?int $minHeight = null,
?int $maxWidth = null,
2024-02-09 15:20:52 +01:00
?int $maxHeight = null,
int $limitMin = 1,
int $limitMax = 5,
bool $nullable = false
2024-01-10 14:49:46 +01:00
): array {
$sizes = (object) [
2026-09-25 16:25:31 +02:00
'minWidth' => $minWidth ?? $this->minWidth,
2024-01-10 14:49:46 +01:00
'minHeight' => $minHeight ?? $this->minHeight,
2026-09-25 16:25:31 +02:00
'maxWidth' => $maxWidth ?? $this->maxWidth,
2024-02-09 15:20:52 +01:00
'maxHeight' => $maxHeight ?? $this->maxHeight,
2024-01-10 14:49:46 +01:00
];
2026-09-25 16:25:31 +02:00
2024-01-10 14:49:46 +01:00
return [
2026-09-25 16:25:31 +02:00
$name => [! $nullable ? 'required' : 'nullable', 'array', "between:{$limitMin},{$limitMax}"],
"{$name}Validate" => "required_with:{$name}|array",
2024-01-10 14:49:46 +01:00
"{$name}Validate.*" => [
'required',
function (string $attribute, $value, callable $fail) use ($sizes) {
2024-01-30 10:43:25 +01:00
ToolboxHelper::validatePicture($attribute, $value, $fail, $sizes);
2026-09-25 16:25:31 +02:00
},
],
2024-01-10 14:49:46 +01:00
];
}
/**
* Merges products pictures fields with request data.
2024-01-10 14:49:46 +01:00
*/
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 => []]);
2026-09-25 16:25:31 +02:00
2024-01-10 14:49:46 +01:00
return;
}
$decodedPath = \urldecode($picturePath);
$this->merge([
"{$name}Validate" => array_merge(
$this->{"{$name}Validate"},
[\public_path(ltrim($decodedPath, '/'))]
2026-09-25 16:25:31 +02:00
),
2024-01-10 14:49:46 +01:00
]);
$changedPicture = ltrim($decodedPath, '/');
$this->merge([
2026-09-25 16:25:31 +02:00
$name => $changedPicture,
]);
} elseif (is_null($picturePath)) {
$this->merge([$name => null]);
2026-09-25 16:25:31 +02:00
return;
2026-09-25 16:25:31 +02:00
}// end if
2024-01-10 14:49:46 +01:00
}
/**
* Merges products pictures fields with request data.
2024-01-10 14:49:46 +01:00
*/
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 => []]);
2026-09-25 16:25:31 +02:00
2024-01-10 14:49:46 +01:00
return;
}
$decodedPath = \urldecode($picturePath);
$this->merge([
"{$name}Validate" => array_merge(
$this->{"{$name}Validate"},
[\public_path(ltrim($decodedPath, '/'))]
2026-09-25 16:25:31 +02:00
),
2024-01-10 14:49:46 +01:00
]);
$changedPicture = $this->{$name};
$changedPicture[$key] = ltrim($decodedPath, '/');
$this->merge([
2026-09-25 16:25:31 +02:00
$name => $changedPicture,
2024-01-10 14:49:46 +01:00
]);
}
2026-09-25 16:25:31 +02:00
} // end if
2024-01-10 14:49:46 +01:00
}
}