ldc-com-app-bo/app/Http/Requests/Bo/Documents/StoreDocumentRequest.php

114 lines
3.9 KiB
PHP
Raw Permalink Normal View History

2023-03-31 17:38:44 +02:00
<?php
2023-04-05 17:20:30 +02:00
namespace App\Http\Requests\Bo\Documents;
2023-03-31 17:38:44 +02:00
2023-04-05 17:20:30 +02:00
use App\Enums\Documents\LangEnum;
use App\Enums\Documents\TypeEnum;
2023-10-03 15:44:44 +02:00
use App\Models\Document;
2023-03-31 17:38:44 +02:00
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;
2023-03-31 17:38:44 +02:00
class StoreDocumentRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize(): bool
2023-03-31 17:38:44 +02:00
{
return Gate::check('create', Document::class);
2023-03-31 17:38:44 +02:00
}
2023-07-13 08:34:54 +00:00
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation(): void
2023-07-13 08:34:54 +00:00
{
$this->merge([
'status' => $this->status ? true : false,
2023-07-13 08:34:54 +00:00
]);
// * 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'),
2025-06-17 12:29:15 +02:00
'type' => TypeEnum::make($this->type, 'name')->name(),
]);
}
2023-07-13 08:34:54 +00:00
}
2023-03-31 17:38:44 +02:00
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
2023-03-31 17:38:44 +02:00
{
/** @var \Illuminate\Filesystem\FilesystemAdapter */
$disk = Storage::disk('docs');
2023-03-31 17:38:44 +02:00
return [
2023-10-03 15:44:44 +02:00
'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'
2023-03-31 17:38:44 +02:00
];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes(): array
2023-03-31 17:38:44 +02:00
{
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'),
2023-03-31 17:38:44 +02:00
];
}
}