ldc-cocotte/app/Http/Requests/Bo/PickupExceptions/StorePickupExceptionRequest.php

84 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests\Bo\PickupExceptions;
use App\Actions\PickupExceptions\CreatePickupExceptionInput;
use Domain\Enums\RepeatFrequency;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Enum;
use Infrastructure\Adapters\Services\AppAdminAuthService;
class StorePickupExceptionRequest extends FormRequest
{
public function __construct(
private readonly AppAdminAuthService $authService
) {
}
public function authorize(): bool
{
return $this->authService->check();
}
public function toRepeatFrequency(): RepeatFrequency
{
$val = $this->input('repeat_frequency');
return $val instanceof RepeatFrequency
? $val
: RepeatFrequency::from($this->string('repeat_frequency')->value());
}
public function toCreatePickupExceptionInput(): CreatePickupExceptionInput
{
return new CreatePickupExceptionInput(
date: $this->string('date')->value(),
label: $this->string('label')->value(),
repeatable: $this->boolean('repeatable'),
repeat_frequency: $this->toRepeatFrequency(),
);
}
#[\Override]
protected function prepareForValidation(): void
{
$this->merge([
'repeatable' => $this->boolean('repeatable'),
]);
}
/** @return array<string,mixed> */
public function rules(): array
{
return [
'date' => ['required', 'date_format:Y-m-d'],
'label' => ['required', 'string', 'max:255'],
'repeatable' => ['present', 'boolean:strict'],
'repeat_frequency' => ['nullable', 'required_if:repeatable,true', new Enum(RepeatFrequency::class)],
];
}
#[\Override]
protected function passedValidation(): void
{
if (! $this->boolean('repeatable')) {
$this->merge([
'repeat_frequency' => RepeatFrequency::WEEKLY->value,
]);
}
}
#[\Override]
public function attributes(): array
{
return [
'date' => trans('validation.attributes.date'),
'label' => trans('validation.custom.attributes.label'),
'repeatable' => trans('validation.custom.attributes.repeatable'),
'repeat_frequency' => trans('validation.custom.attributes.repeat_frequency'),
];
}
}