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' => [ '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($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'), ]; } }