ldc-com-app-bo/app/Http/Requests/Bo/Documents/StoreDocumentRequest.php
2025-06-17 12:29:15 +02:00

113 lines
3.9 KiB
PHP

<?php
namespace App\Http\Requests\Bo\Documents;
use App\Enums\Documents\LangEnum;
use App\Enums\Documents\TypeEnum;
use App\Models\Document;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Enum;
use Uwil\LaravelCommon\Helpers\FileHelper;
class StoreDocumentRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize(): bool
{
return Gate::check('create', Document::class);
}
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation(): void
{
$this->merge([
'status' => $this->status ? true : false,
]);
// * Sanitize Path
if (!empty($this->path)) {
$filename = $this->path;
$refMethod = new \ReflectionMethod(FileHelper::class, 'sanitizeFileName');
$refMethod->setAccessible(true);
$filename = \pathinfo(
$refMethod->invoke(null, htmlspecialchars($filename), false),
\PATHINFO_BASENAME
);
$this->merge([
'path' => $filename
]);
}
if (Document::query()->where('id', $this->document_id)->exists() && $this->type) {
$this->merge([
'directory_id' => Document::query()->where('id', $this->document_id)->value('directory_id'),
'type' => TypeEnum::make($this->type, 'name')->name(),
]);
}
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
/** @var \Illuminate\Filesystem\FilesystemAdapter */
$disk = Storage::disk('docs');
return [
'directory_id' => 'required|integer',
'document_id' => 'sometimes:directory_id|nullable|integer',
'uuid' => [
'required',
'uuid',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
function ($attribute, $value, $fail) use ($disk) {
if ($this->type and \array_column(TypeEnum::toArray(), 'name')) {
$extension = TypeEnum::make($this->type, 'name')->name();
$value = "{$value}.{$extension}";
}
if (!$disk->has(sprintf('%s/%s', \tenant()->getKey(), $value))) {
$path = $this->path ?? $this->value ?? 'not found';
$fail(\trans('Le fichier :filename n\'est pas présent dans le système de fichier', [
'filename' => $path
]));
}
}
],
'title' => 'required|min:3|max:255',
'type' => ['required', 'string', Rule::in(array_column(TypeEnum::toArray(), 'name'))],
'path' => 'required|min:3|max:255',
'lang' => ['required', new Enum(LangEnum::class)],
'status' => 'required|boolean'
];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes(): array
{
return [
'directory_id' => \trans('models.classes.directory'),
'document_id' => \trans('models.classes.document'),
'title' => \trans('validation.attributes.title'),
'uuid' => \trans('models.classes.document'),
'type' => \trans('validation.custom.type'),
'path' => \trans('validation.custom.path'),
'lang' => \trans('validation.custom.lang'),
'status' => \trans('validation.custom.status'),
];
}
}