ldc-cocotte/app/Http/Requests/Bo/Shops/IndexShopRequest.php

67 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests\Bo\Shops;
use Domain\Models\ValueObjects\Entities\PickupExceptionId;
use Illuminate\Foundation\Http\FormRequest;
use Infrastructure\Adapters\Services\AppAdminAuthService;
use Shared\Helpers\UuidHelper;
class IndexShopRequest extends FormRequest
{
public const string SESSION_KEY = 'shops.filter.pickup-exception';
public function __construct(
private readonly AppAdminAuthService $authService
) {
}
public function authorize(): bool
{
return $this->authService->check();
}
#[\Override]
protected function prepareForValidation(): void
{
if ($this->has('pickup-exception')) {
$filterValue = $this->string('pickup-exception');
if ($filterValue->isEmpty()) {
$this->session()->forget(self::SESSION_KEY);
} else {
$this->session()->put(self::SESSION_KEY, (string) $filterValue);
}
}
}
public function toPickupExceptionId(): ?PickupExceptionId
{
$raw = $this->session()->get(self::SESSION_KEY);
if ($raw === null || ! is_string($raw) || $raw === '' || ! UuidHelper::isValid($raw)) {
return null;
}
$uuid = UuidHelper::fromString($raw);
if (! UuidHelper::isV7($uuid)) {
return null;
}
return new PickupExceptionId($uuid);
}
public function getPickupExceptionId(): ?PickupExceptionId
{
return $this->toPickupExceptionId();
}
/** @return array<string, mixed> */
public function rules(): array
{
return [
'pickup-exception' => ['sometimes', 'nullable', 'string', 'uuid'],
];
}
}