wip: age based filtering of options

This commit is contained in:
Björn Fromme
2026-03-16 11:59:09 +01:00
parent b038d9a41f
commit 7003c0d81b
17 changed files with 845 additions and 23 deletions
@@ -0,0 +1,223 @@
<?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 additionalServices field for booking participants.
*
* This handler manages additional service selections for participants in the booking
* creation process. It processes the additionalServices field from form submissions,
* filters out age-inappropriate services, and updates the participant DTO with only
* valid selections.
*
* Key responsibilities:
* - Validates additional service selections against age constraints
* - Removes services that are no longer available due to age changes
* - Maintains data consistency during HTMX form updates
* - Prevents form validation errors from stale service selections
*
* Dependencies: dateOfBirth (must be processed first for age evaluation)
*/
class ParticipantAdditionalServicesFieldHandler extends AbstractParticipantFieldHandler
{
/**
* Returns the form field name this handler processes.
*
* @return string The field name 'additionalServices'
*/
public function getFieldName(): string
{
return 'additionalServices';
}
/**
* Returns the field dependencies for proper processing order.
*
* This handler depends on dateOfBirth being processed first because
* age evaluation requires the participant's birth date to be available.
*
* @return string[] Array containing 'dateOfBirth' dependency
*/
public function getDependencies(): array
{
return ['dateOfBirth'];
}
/**
* Processes the additionalServices field for a specific participant.
*
* This method extracts additional service selections from submitted form data,
* validates each selection against the participant's age constraints, and
* updates the participant DTO with only valid selections. Services that are
* no longer appropriate for the participant's age are automatically removed.
*
* Processing steps:
* 1. Safely retrieves the participant object from the DTO
* 2. Extracts current service selections from submitted data
* 3. Gets available additional services from travel data
* 4. Validates each selection against age constraints
* 5. Updates participant with filtered valid selections
*
* @param array<string, mixed> $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 service selections from submitted data
$selectedServices = $this->getFieldValue($submittedData, $this->getFieldName()) ?? [];
// Get available additional services from travel data
$availableServices = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_ADDITIONAL);
// Filter selections to keep only age-appropriate services
$validSelections = $this->filterValidServiceSelections(
$selectedServices,
$availableServices,
$bookingDto,
$participantIndex
);
// Update participant with validated selections
$participant->additionalServices = $validSelections;
}
/**
* Filters service selections to keep only those valid for the participant's age.
*
* This method validates each selected service against the available services
* and the participant's age constraints. Services that are no longer available
* or appropriate for the participant's age are filtered out.
*
* @param array $selectedServices List of currently selected services
* @param array $availableServices List of all available additional services
* @param BookingDtoInterface $bookingDto The booking DTO for context
* @param int $participantIndex The participant index for age evaluation
*
* @return array Filtered array of valid service selections
*/
private function filterValidServiceSelections(
array $selectedServices,
array $availableServices,
BookingDtoInterface $bookingDto,
int $participantIndex,
): array {
$validSelections = [];
foreach ($selectedServices as $selectedService) {
if ($this->isServiceValidForParticipant($selectedService, $availableServices, $bookingDto, $participantIndex)) {
$validSelections[] = $selectedService;
}
}
return $validSelections;
}
/**
* Validates if a selected service is still valid for the participant.
*
* This method checks if a selected service exists in the available services
* and meets the age constraints for the current participant.
*
* @param mixed $selectedService The selected service to validate
* @param array $availableServices Array of available services
* @param BookingDtoInterface $bookingDto The booking DTO for context
* @param int $participantIndex The participant index for age evaluation
*
* @return bool True if the service 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)) {
return true; // No age restrictions, service is valid
}
// 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;
}
}