Files
WMS/Modules/Documentation/Http/Requests/StoreDocumentationDocumentRequest.php

65 lines
2.4 KiB
PHP
Executable File

<?php
namespace Modules\Documentation\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\UploadedFile;
use Illuminate\Validation\Rule;
use Modules\Documentation\Models\DocumentationDocument;
class StoreDocumentationDocumentRequest extends FormRequest
{
private const MAX_UPLOAD_BYTES = 1_048_576;
/**
* Determine whether the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules(): array
{
return [
'documentation_category_id' => ['nullable', 'integer', 'exists:documentation_categories,id'],
'title' => ['required', 'string', 'max:220'],
'slug' => ['nullable', 'string', 'max:240', Rule::unique('documentation_documents', 'slug')],
'summary' => ['nullable', 'string', 'max:500'],
'body' => ['nullable', 'string'],
'status' => ['required', Rule::in([DocumentationDocument::STATUS_DRAFT, DocumentationDocument::STATUS_PUBLISHED, DocumentationDocument::STATUS_ARCHIVED])],
'is_featured' => ['nullable', 'boolean'],
'version' => ['required', 'string', 'max:50'],
'reference' => ['nullable', 'string', 'max:100'],
'attachment' => ['nullable', 'file', $this->uploadSizeRule(), 'mimes:' . implode(',', config('documentation.allowed_upload_mimes', []))],
'attachments' => ['nullable', 'array'],
'attachments.*' => ['file', $this->uploadSizeRule(), 'mimes:' . implode(',', config('documentation.allowed_upload_mimes', []))],
];
}
/**
* Enforce the upload ceiling in bytes instead of relying on Laravel's
* file `max` rule, keeping the security boundary explicit.
*
* @return \Closure(string, mixed, \Closure(string): void): void
*/
private function uploadSizeRule(): \Closure
{
return static function (string $attribute, mixed $value, \Closure $fail): void {
if (! $value instanceof UploadedFile || ! $value->isValid()) {
return;
}
$size = $value->getSize();
if ($size === false || $size > self::MAX_UPLOAD_BYTES) {
$fail(sprintf('The %s must not be larger than 1 MB.', $attribute));
}
};
}
}