115 lines
3.8 KiB
PHP
115 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Bo\Documents;
|
|
|
|
use App\Enums\Documents\LangEnum;
|
|
use App\Enums\Documents\TypeEnum;
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Models\Document;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Validation\Rules\Enum;
|
|
|
|
class StoreDocumentRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return auth('backend')->check();
|
|
}
|
|
|
|
/**
|
|
* 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(FileStorageHelper::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')->value(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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' => [
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
|
'required', 'uuid', function ($attribute, $value, $fail) use ($disk) {
|
|
if ($this->type and \in_array($this->type, TypeEnum::toValues())) {
|
|
$extension = TypeEnum::make($this->type, 'value')->name();
|
|
$value = "{$value}.{$extension}";
|
|
}
|
|
if (!$disk->has($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',
|
|
new Enum(TypeEnum::class)
|
|
],
|
|
'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('dossier à associer'),
|
|
'document_id' => \trans('document (FR) à associer'),
|
|
'title' => \trans('titre'),
|
|
'uuid' => \trans('document'),
|
|
'type' => \trans('type du document'),
|
|
'path' => \trans('chemin du document'),
|
|
'lang' => \trans('langue du document'),
|
|
'status' => \trans('état de publication du document'),
|
|
];
|
|
}
|
|
}
|