$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 } /** * Processes the skiPass field for a specific participant. * * This method extracts the skipass selection from submitted form data, * validates the selection against the participant's age constraints and * travel date constraints, and updates the participant DTO with the valid * selection. If the skipass is no longer appropriate for the participant's * age or exceeds the travel date range, it is automatically cleared. * * @param array $submittedData The submitted participant form data * @param BookingDtoInterface $bookingDto The booking DTO to update (create or edit) * @param int $participantIndex The index of the participant being processed */ public function processField(array $submittedData, BookingDtoInterface $bookingDto, int $participantIndex): void { // Safely get the participant object, returning early if not found $participant = $this->getParticipant($bookingDto, $participantIndex); if (null === $participant) { return; } // Extract current skipass selection from submitted data $selectedSkiPass = $this->getFieldValue($submittedData, $this->getFieldName()); // Get available skipasses from travel data (with date filtering) $availableSkipasses = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_SKI_PASS, true, true); // For single selection, validate the selected skipass and convert ID to Service object $validSelection = null; if (null !== $selectedSkiPass) { if ($this->isServiceValidForParticipant($selectedSkiPass, $availableSkipasses, $bookingDto, $participantIndex)) { // Convert the submitted ID back to the Service object $validSelection = $this->findServiceInAvailableServices($selectedSkiPass, $availableSkipasses); } } // Update participant with validated selection $participant->skiPass = $validSelection; } /** * Validates if a selected skipass is still valid for the participant. * * This method checks both age constraints (via birth year ranges) and * date constraints (skipass dates must be within travel dates). * * @param mixed $selectedService The selected skipass to validate * @param array $availableServices Array of available skipasses * @param BookingDtoInterface $bookingDto The booking DTO for context * @param int $participantIndex The participant index for age evaluation * * @return bool True if the skipass is valid for the participant, false otherwise */ private function isServiceValidForParticipant( mixed $selectedService, array $availableServices, BookingDtoInterface $bookingDto, int $participantIndex, ): bool { // Find the service in available services $service = $this->findServiceInAvailableServices($selectedService, $availableServices); if (null === $service) { return false; // Service not found in available services } // Check if service has age constraints $ageEvaluator = new ServiceAgeEvaluator(); if ($ageEvaluator->canEvaluate($service)) { // Validate service against participant's age if (false === $ageEvaluator->isServiceAvailableForParticipant($service, $bookingDto, $participantIndex)) { return false; // Age constraints not met } } // Date constraints are already handled by the travel->getAdditionalServicesBySubTypes // method with date filtering enabled (true, true parameters), so if the service // is in the available services list, it already passed date validation. return true; // Service passed both age and date validation } /** * Finds a selected skipass in the list of available skipasses. * * @param mixed $selectedService The selected skipass to find * @param array $availableServices Array of available Service objects * * @return Service|null The found service or null if not found */ private function findServiceInAvailableServices(mixed $selectedService, array $availableServices): ?Service { foreach ($availableServices as $availableService) { if ($this->servicesMatch($selectedService, $availableService)) { return $availableService; } } return null; } /** * Determines if a selected skipass matches an available skipass. * * @param mixed $selectedService The selected skipass from form data * @param Service $availableService The available skipass to compare against * * @return bool True if the skipasses match, false otherwise */ private function servicesMatch(mixed $selectedService, Service $availableService): bool { // Direct object comparison if ($selectedService === $availableService) { return true; } // ID comparison for Service objects if ($selectedService instanceof Service) { return $selectedService->id === $availableService->id; } // ID comparison for numeric values if (is_numeric($selectedService)) { return (int) $selectedService === $availableService->id; } // String ID comparison if (is_string($selectedService)) { return $selectedService === (string) $availableService->id; } return false; } }