2025-09-26 15:08:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Traits\Requests;
|
|
|
|
|
|
2026-02-10 15:43:47 +01:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2025-11-17 17:02:22 +01:00
|
|
|
|
2025-09-26 15:08:37 +02:00
|
|
|
trait HasInertiaPicture
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Prepare the data for validation.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-11-12 14:32:50 +01:00
|
|
|
protected function mergeInertiaPicture(): void
|
|
|
|
|
{
|
2026-02-12 17:14:08 +01:00
|
|
|
if (empty($this->convertedFiles)) {
|
2025-11-12 14:32:50 +01:00
|
|
|
return;
|
2025-09-26 15:08:37 +02:00
|
|
|
}
|
2025-11-12 14:32:50 +01:00
|
|
|
$original = $this->all();
|
2026-02-10 15:43:47 +01:00
|
|
|
$merged = self::arrayMergeRecursiveDistinct($original, $this->convertedFiles);
|
2025-11-12 14:32:50 +01:00
|
|
|
$this->replace($merged);
|
|
|
|
|
}
|
2025-09-26 15:08:37 +02:00
|
|
|
|
2025-11-17 17:02:22 +01:00
|
|
|
/**
|
|
|
|
|
* Merge recursive data.
|
|
|
|
|
*
|
|
|
|
|
* @param array $array1
|
|
|
|
|
* @param array $array2
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2025-11-12 14:32:50 +01:00
|
|
|
protected static function arrayMergeRecursiveDistinct(array $array1, array $array2): array
|
|
|
|
|
{
|
|
|
|
|
$merged = $array1;
|
|
|
|
|
|
|
|
|
|
foreach ($array2 as $key => $value) {
|
|
|
|
|
if (isset($merged[$key]) && is_array($merged[$key]) && is_array($value)) {
|
|
|
|
|
$merged[$key] = self::arrayMergeRecursiveDistinct($merged[$key], $value);
|
|
|
|
|
} else {
|
|
|
|
|
$merged[$key] = $value;
|
|
|
|
|
}
|
2025-09-26 15:08:37 +02:00
|
|
|
}
|
|
|
|
|
|
2025-11-12 14:32:50 +01:00
|
|
|
return $merged;
|
2025-09-26 15:08:37 +02:00
|
|
|
}
|
|
|
|
|
|
2025-11-17 17:02:22 +01:00
|
|
|
/**
|
|
|
|
|
* Set picture rules (UploadedFile or string path).
|
2025-11-18 09:27:44 +01:00
|
|
|
*
|
|
|
|
|
* @param string $rules
|
2026-02-12 17:14:08 +01:00
|
|
|
* @return array<mixed>
|
2025-11-17 17:02:22 +01:00
|
|
|
*/
|
|
|
|
|
protected function pictureRules(string $rules): array
|
|
|
|
|
{
|
|
|
|
|
$rules = explode("|", $rules);
|
|
|
|
|
return [
|
|
|
|
|
function ($attribute, $value, $fail) use ($rules) {
|
|
|
|
|
$isRequired = in_array('required', $rules);
|
|
|
|
|
$isNullable = in_array('nullable', $rules);
|
2025-11-12 14:32:50 +01:00
|
|
|
|
2025-11-17 17:02:22 +01:00
|
|
|
// Check required value.
|
|
|
|
|
if ($isRequired && $value === null) {
|
2026-02-03 14:33:08 +01:00
|
|
|
$label = $this->attributes()[$attribute] ?? $attribute;
|
|
|
|
|
$fail(trans('validation.required', ['attribute' => $label]));
|
2025-11-17 17:02:22 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Authorized nullable value.
|
|
|
|
|
if ($isNullable && $value === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UploadedFile validations.
|
|
|
|
|
if ($value instanceof UploadedFile) {
|
|
|
|
|
// Filter unused rules for validator.
|
|
|
|
|
// The “required” and “nullable” rules are not the same as those for online validation.
|
|
|
|
|
$validatorRules = array_filter($rules, fn($r) => $r !== 'required' && $r !== 'nullable');
|
2025-11-18 09:27:44 +01:00
|
|
|
|
2025-11-17 17:02:22 +01:00
|
|
|
$validator = \Validator::make(
|
|
|
|
|
[$attribute => $value],
|
|
|
|
|
[$attribute => implode('|', $validatorRules)]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
|
$fail($validator->errors()->first($attribute));
|
2025-09-26 15:08:37 +02:00
|
|
|
}
|
2025-11-17 17:02:22 +01:00
|
|
|
|
|
|
|
|
return;
|
2025-09-26 15:08:37 +02:00
|
|
|
}
|
2025-11-17 17:02:22 +01:00
|
|
|
|
|
|
|
|
// String path validations.
|
|
|
|
|
if (is_string($value)) {
|
|
|
|
|
// Validate the path.
|
2026-02-10 15:43:47 +01:00
|
|
|
// phpcs:ignore Generic.Files.LineLength
|
2025-11-17 17:02:22 +01:00
|
|
|
$pattern = '/^storage\/modelfiles\/[a-zA-Z0-9_]+\/[0-9]{2}\/[0-9]{2}\/[0-9]{2}\/[a-zA-Z0-9]{2}\/[a-zA-Z0-9_-]+/';
|
|
|
|
|
if (!preg_match($pattern, $value)) {
|
|
|
|
|
$fail(trans('validation.file_path', ['attribute' => $this->attributes()[$attribute]]));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate the extension.
|
|
|
|
|
$mimesRule = collect($rules)->first(fn($r) => str_starts_with($r, 'mimes:'));
|
|
|
|
|
if ($mimesRule) {
|
|
|
|
|
$allowedExtensions = explode(',', str_replace('mimes:', '', $mimesRule));
|
2026-02-10 15:43:47 +01:00
|
|
|
$ext = strtolower(pathinfo($value, PATHINFO_EXTENSION));
|
2025-11-17 17:02:22 +01:00
|
|
|
if (!in_array($ext, $allowedExtensions)) {
|
|
|
|
|
$fail(trans('validation.mimetypes', [
|
|
|
|
|
'attribute' => $this->attributes()[$attribute],
|
|
|
|
|
'values' => implode(', ', $allowedExtensions),
|
|
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
2026-02-10 15:43:47 +01:00
|
|
|
} //end if
|
2025-11-17 17:02:22 +01:00
|
|
|
|
|
|
|
|
// Unmanaged case (not null, not UploadedFile, not string).
|
|
|
|
|
$fail("Invalide type for $attribute.");
|
|
|
|
|
}
|
|
|
|
|
];
|
2025-09-26 15:08:37 +02:00
|
|
|
}
|
|
|
|
|
}
|