*/ public const array ALLOWED_EXTENSIONS = ['jpg', 'jpeg', 'png']; /** @var list */ public const array ALLOWED_MIME_TYPES = ['image/jpeg', 'image/png']; public function __construct( private readonly AppAdminAuthService $authService, ) { } public function authorize(): bool { return $this->authService->check(); } #[\Override] protected function prepareForValidation(): void { $this->merge([ 'status' => $this->boolean('status'), ]); } /** @return array|string> */ public function rules(): array { return [ 'name' => ['required', 'string', 'min:2', 'max:45'], 'description' => ['nullable', 'string', 'min:2'], 'picture_alt' => ['required_with:picture', 'string', 'min:4', 'max:75'], 'picture_title' => ['required_with:picture', 'string', 'min:4', 'max:75'], 'picture' => [ Rule::imageFile() ->extensions(self::ALLOWED_EXTENSIONS) ->rules([ 'mimetypes:' . implode(',', self::ALLOWED_MIME_TYPES), new FileExtensionMatchesMimeType(), ]) ->max(self::MAX_FILE_SIZE_KB) ->dimensions( Rule::dimensions() ->width(static::PICTURE_WIDTH) ->height(static::PICTURE_HEIGHT) ), ], 'status' => ['present', 'boolean:strict'], ]; } #[\Override] public function attributes(): array { return [ 'name' => trans('validation.attributes.name'), 'description' => trans('validation.attributes.description'), 'status' => trans('validation.custom.attributes.status'), 'picture' => trans('validation.attributes.image'), 'picture_alt' => trans('validation.custom.attributes.picture_alt'), 'picture_title' => trans('validation.custom.attributes.picture_title'), ]; } public function toCreateRangeInput(): CreateRangeInput { $pictureAlt = $this->filled('picture_alt') ? $this->string('picture_alt')->value() : null; $pictureTitle = $this->filled('picture_title') ? $this->string('picture_title')->value() : null; return new CreateRangeInput( name: $this->string('name')->value(), description: $this->filled('description') ? $this->string('description')->value() : null, picture: $this->hasFile('picture') ? $this->file('picture') : null, pictureMetadata: new PictureMetadata( alt: $pictureAlt, title: $pictureTitle, ), status: $this->boolean('status'), ); } }