From 233748a6c5f393f3f1f42d0c701a4fa3438914a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Sun, 7 Dec 2025 15:03:54 +0100 Subject: [PATCH] chore: cleanup --- .../AbstractParticipantFieldHandler.php | 66 +++++++++++++++++-- ...ticipantAdditionalServicesFieldHandler.php | 59 ----------------- .../Service/ParticipantBoardFieldHandler.php | 32 --------- .../ParticipantCoursesFieldHandler.php | 52 --------------- .../ParticipantRentalsFieldHandler.php | 32 --------- .../ParticipantSkiPassFieldHandler.php | 52 --------------- 6 files changed, 62 insertions(+), 231 deletions(-) diff --git a/src/Form/Service/Abstract/AbstractParticipantFieldHandler.php b/src/Form/Service/Abstract/AbstractParticipantFieldHandler.php index 0c9b4f8..8eef76b 100644 --- a/src/Form/Service/Abstract/AbstractParticipantFieldHandler.php +++ b/src/Form/Service/Abstract/AbstractParticipantFieldHandler.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Form\Service\Abstract; +use App\BusProNet\Model\Service; use App\Form\Model\BookingDto; use App\Form\Service\Contract\ParticipantFieldHandlerInterface; @@ -190,19 +191,22 @@ abstract class AbstractParticipantFieldHandler implements ParticipantFieldHandle * items and returning the matching object. Used by handlers that need to * validate service, pickup, or other selections against available options. * + * Normalizes the input ID to integer before comparison since Service, Pickup, + * and Room models all use integer IDs. + * * @param mixed $selectedId The submitted ID to find (string or int) - * @param object[] $items Array of objects with an `id` property + * @param object[] $items Array of objects with an `id` property (must be int) * * @return object|null The matching item, or null if not found or invalid ID */ protected function findItemById(mixed $selectedId, array $items): ?object { - if (null === $selectedId || (false === is_string($selectedId) && false === is_int($selectedId))) { + $id = $this->normalizeIntValue($selectedId); + + if (null === $id) { return null; } - $id = (int) $selectedId; - foreach ($items as $item) { if ($item->id === $id) { return $item; @@ -211,4 +215,58 @@ abstract class AbstractParticipantFieldHandler implements ParticipantFieldHandle return null; } + + /** + * Finds a Service in available services by matching the selected value. + * + * Handles various input types that may come from form submissions: + * - Service object: matches by object identity or ID + * - Integer/numeric string: matches by ID + * + * @param mixed $selectedService The selected service (Service object, int, or numeric string) + * @param Service[] $availableServices Array of available Service objects + * + * @return Service|null The matching Service, or null if not found + */ + protected function findServiceInAvailableServices(mixed $selectedService, array $availableServices): ?Service + { + foreach ($availableServices as $availableService) { + if ($this->servicesMatch($selectedService, $availableService)) { + return $availableService; + } + } + + return null; + } + + /** + * Checks if a selected service matches an available service. + * + * Compares services by object identity first, then by ID. Handles Service objects, + * integers, and numeric strings from form submissions. + * + * @param mixed $selectedService The selected service to compare + * @param Service $availableService The available service to compare against + * + * @return bool True if the services match, false otherwise + */ + protected 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 (int or numeric string from form) + if (is_numeric($selectedService)) { + return (int) $selectedService === $availableService->id; + } + + return false; + } } diff --git a/src/Form/Service/ParticipantAdditionalServicesFieldHandler.php b/src/Form/Service/ParticipantAdditionalServicesFieldHandler.php index 3d578cf..d6d4180 100644 --- a/src/Form/Service/ParticipantAdditionalServicesFieldHandler.php +++ b/src/Form/Service/ParticipantAdditionalServicesFieldHandler.php @@ -184,63 +184,4 @@ class ParticipantAdditionalServicesFieldHandler extends AbstractParticipantField // Validate service against participant's age return $ageEvaluator->isServiceAvailableForParticipant($service, $bookingDto, $participantIndex); } - - /** - * Finds a selected service in the list of available services. - * - * This method handles different representations of services (objects, IDs, etc.) - * and locates the corresponding service in the available services array. - * - * @param mixed $selectedService The selected service 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) { - // Handle different comparison scenarios - if ($this->servicesMatch($selectedService, $availableService)) { - return $availableService; - } - } - - return null; - } - - /** - * Determines if a selected service matches an available service. - * - * This method handles various service representation formats that might - * come from form submissions (objects, IDs, arrays, etc.). - * - * @param mixed $selectedService The selected service from form data - * @param Service $availableService The available service to compare against - * - * @return bool True if the services 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; - } } diff --git a/src/Form/Service/ParticipantBoardFieldHandler.php b/src/Form/Service/ParticipantBoardFieldHandler.php index 76c2c38..faac2f0 100644 --- a/src/Form/Service/ParticipantBoardFieldHandler.php +++ b/src/Form/Service/ParticipantBoardFieldHandler.php @@ -110,36 +110,4 @@ class ParticipantBoardFieldHandler extends AbstractParticipantFieldHandler 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; - } } diff --git a/src/Form/Service/ParticipantCoursesFieldHandler.php b/src/Form/Service/ParticipantCoursesFieldHandler.php index efd87c1..7c521f9 100644 --- a/src/Form/Service/ParticipantCoursesFieldHandler.php +++ b/src/Form/Service/ParticipantCoursesFieldHandler.php @@ -170,56 +170,4 @@ class ParticipantCoursesFieldHandler extends AbstractParticipantFieldHandler // Validate service against participant's age return $ageEvaluator->isServiceAvailableForParticipant($service, $bookingDto, $participantIndex); } - - /** - * Finds a selected course in the list of available courses. - * - * @param mixed $selectedService The selected course 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 course matches an available course. - * - * @param mixed $selectedService The selected course from form data - * @param Service $availableService The available course to compare against - * - * @return bool True if the courses 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; - } } diff --git a/src/Form/Service/ParticipantRentalsFieldHandler.php b/src/Form/Service/ParticipantRentalsFieldHandler.php index b266994..b6b995b 100644 --- a/src/Form/Service/ParticipantRentalsFieldHandler.php +++ b/src/Form/Service/ParticipantRentalsFieldHandler.php @@ -147,38 +147,6 @@ class ParticipantRentalsFieldHandler extends AbstractParticipantFieldHandler 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; - } - /** * Filters rentals to only include those matching the skipass duration. * diff --git a/src/Form/Service/ParticipantSkiPassFieldHandler.php b/src/Form/Service/ParticipantSkiPassFieldHandler.php index 1b1ba6b..a0b5c8c 100644 --- a/src/Form/Service/ParticipantSkiPassFieldHandler.php +++ b/src/Form/Service/ParticipantSkiPassFieldHandler.php @@ -151,56 +151,4 @@ class ParticipantSkiPassFieldHandler extends AbstractParticipantFieldHandler 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; - } }