$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; } }