140 lines
4.6 KiB
PHP
140 lines
4.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Form\Service;
|
|
|
|
use App\BusProNet\Constants;
|
|
use App\BusProNet\Model\Service;
|
|
use App\Form\Model\BookingDtoInterface;
|
|
use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
|
|
|
|
/**
|
|
* Handles processing of the rentals field for booking participants.
|
|
*
|
|
* This handler manages rental equipment selections for participants in the booking
|
|
* creation process. It processes the rentals field from form submissions,
|
|
* filters out age-inappropriate options, and updates the participant DTO with only
|
|
* valid selections.
|
|
*
|
|
* Dependencies: dateOfBirth (must be processed first for age evaluation)
|
|
*/
|
|
class ParticipantRentalsFieldHandler extends AbstractParticipantFieldHandler
|
|
{
|
|
public function getFieldName(): string
|
|
{
|
|
return 'rentals';
|
|
}
|
|
|
|
/**
|
|
* Determines if this handler should process the field based on submitted data.
|
|
*
|
|
* For service selection fields like rentals, we always need to process
|
|
* to handle cases where all selections are cleared (field not present in data).
|
|
* This ensures the participant DTO is updated with an empty array when no
|
|
* services are selected.
|
|
*
|
|
* @param array<string, mixed> $submittedData The submitted participant form data
|
|
* @param int $participantIndex The index of the participant being processed
|
|
*
|
|
* @return bool Always returns true for service selection fields
|
|
*/
|
|
public function shouldProcess(array $submittedData, int $participantIndex): bool
|
|
{
|
|
return true; // Always process to handle deselection cases
|
|
}
|
|
|
|
public function processField(array $submittedData, BookingDtoInterface $bookingDto, int $participantIndex): void
|
|
{
|
|
$participant = $this->getParticipant($bookingDto, $participantIndex);
|
|
if (null === $participant) {
|
|
return;
|
|
}
|
|
|
|
$selectedRentals = $this->getFieldValue($submittedData, $this->getFieldName()) ?? [];
|
|
$availableRentals = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_RENTALS, true, true);
|
|
|
|
$validSelections = $this->filterValidServiceSelections(
|
|
$selectedRentals,
|
|
$availableRentals,
|
|
$bookingDto,
|
|
$participantIndex
|
|
);
|
|
|
|
$participant->rentals = $validSelections;
|
|
}
|
|
|
|
private function filterValidServiceSelections(
|
|
array $selectedServices,
|
|
array $availableServices,
|
|
BookingDtoInterface $bookingDto,
|
|
int $participantIndex,
|
|
): array {
|
|
$validSelections = [];
|
|
|
|
foreach ($selectedServices as $selectedService) {
|
|
if ($this->isServiceValidForParticipant($selectedService, $availableServices, $bookingDto, $participantIndex)) {
|
|
// Convert the selected service ID/data back to the actual Service object
|
|
$serviceObject = $this->findServiceInAvailableServices($selectedService, $availableServices);
|
|
if (null !== $serviceObject) {
|
|
$validSelections[] = $serviceObject;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $validSelections;
|
|
}
|
|
|
|
private function isServiceValidForParticipant(
|
|
mixed $selectedService,
|
|
array $availableServices,
|
|
BookingDtoInterface $bookingDto,
|
|
int $participantIndex,
|
|
): bool {
|
|
$service = $this->findServiceInAvailableServices($selectedService, $availableServices);
|
|
|
|
if (null === $service) {
|
|
return false;
|
|
}
|
|
|
|
$ageEvaluator = new ServiceAgeEvaluator();
|
|
if (!$ageEvaluator->canEvaluate($service)) {
|
|
return true;
|
|
}
|
|
|
|
return $ageEvaluator->isServiceAvailableForParticipant($service, $bookingDto, $participantIndex);
|
|
}
|
|
|
|
private function findServiceInAvailableServices(mixed $selectedService, array $availableServices): ?Service
|
|
{
|
|
foreach ($availableServices as $availableService) {
|
|
if ($this->servicesMatch($selectedService, $availableService)) {
|
|
return $availableService;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private function servicesMatch(mixed $selectedService, Service $availableService): bool
|
|
{
|
|
if ($selectedService === $availableService) {
|
|
return true;
|
|
}
|
|
|
|
if ($selectedService instanceof Service) {
|
|
return $selectedService->id === $availableService->id;
|
|
}
|
|
|
|
if (is_numeric($selectedService)) {
|
|
return (int) $selectedService === $availableService->id;
|
|
}
|
|
|
|
if (is_string($selectedService)) {
|
|
return $selectedService === (string) $availableService->id;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|