ldc-boutique-cnc-sable/app/Http/Requests/Bo/Settings/UpdateSettingsRequest.php
Jérémy Munsch 8b96ce90be
test(casts): add tests for EmployeeSitesCast functionality
test(casts): add tests for ShopperEstablishmentCodeCast functionality

test(casts): add tests for ShopperServiceCodeCast functionality

test(casts): add tests for ShopperSiteCodeCast functionality

test(dto): update ProductDtoTest to include additional fields

test(dto): update SettingDtoTest to remove arcadiaSites assertions

test(events): update AnOrderHasBeenPlacedTest to use value objects

test(entities): update OrderTest to use value objects for employee details

test(value-objects): update EmployeeUpsertDataTest to use value objects

test(pagination): update PaginationTest to access value properties

test(services): update AppCustomerAuthServiceTest to use CrmHashcode

test(services): update CrmServiceTest to use CrmHashcode for customer retrieval
2026-10-27 09:58:45 +01:00

94 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests\Bo\Settings;
use App\Actions\Settings\UpdateSettingsInput;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Illuminate\Foundation\Http\FormRequest;
use Infrastructure\Adapters\Services\AppAdminAuthService;
class UpdateSettingsRequest extends FormRequest
{
public function __construct(
private readonly AppAdminAuthService $authService
) {
}
public function authorize(): bool
{
return $this->authService->check();
}
#[\Override]
protected function prepareForValidation(): void
{
$rawSites = $this->input('employee_sites');
if (is_string($rawSites)) {
$split = preg_split('/[\s,]+/', $rawSites) ?: [];
$filtered = array_filter(
array_map(static fn (string $s): string => strtoupper(trim($s)), $split),
static fn (string $s): bool => $s !== ''
);
$sites = array_values(array_unique($filtered));
} elseif (is_array($rawSites)) {
$filtered = array_filter(
array_map(static fn (mixed $s): string => is_string($s) ? strtoupper(trim($s)) : '', $rawSites),
static fn (string $s): bool => $s !== ''
);
$sites = array_values(array_unique($filtered));
} else {
$sites = [];
}
$this->merge([
'employee_sites' => $sites,
]);
}
/**
* @return array<string, mixed>
*/
public function rules(): array
{
return [
'employee_sites' => ['present', 'array'],
'employee_sites.*' => ['string', 'regex:/^[A-Z]{2,}$/'],
];
}
#[\Override]
public function attributes(): array
{
return [
'employee_sites' => 'Sites des salariés',
'employee_sites.*' => 'Code de site des salariés',
];
}
/**
* @return array<string, string>
*/
#[\Override]
public function messages(): array
{
return [
'employee_sites.*.regex' => 'Chaque code de site doit être composé d\'au moins 2 lettres majuscules (ex: PA, NA, LY).',
];
}
public function toUpdateSettingsInput(): UpdateSettingsInput
{
/** @var array<int, string> $sites */
$sites = $this->validated('employee_sites', []);
return new UpdateSettingsInput(
employeeSites: array_map(
static fn (string $site): ShopperSiteCode => new ShopperSiteCode($site),
$sites
)
);
}
}