wip: age based filtering of options
This commit is contained in:
@@ -67,12 +67,14 @@ services:
|
||||
arguments:
|
||||
$choiceListFactory: '@form.choice_list_factory.default'
|
||||
|
||||
# Participant Field Handler Registry with hybrid handler configuration
|
||||
# Participant Field Handler Registry - all handlers now instantiate dependencies directly
|
||||
App\Form\Service\ParticipantFieldHandlerRegistry:
|
||||
arguments:
|
||||
$handlers:
|
||||
# Simple handlers (no dependencies) - use class names
|
||||
# All handlers now have no external dependencies and can be instantiated directly
|
||||
- 'App\Form\Service\ParticipantDateOfBirthFieldHandler'
|
||||
- 'App\Form\Service\ParticipantAssignedRoomFieldHandler'
|
||||
# Complex handlers (with dependencies) would use service references like:
|
||||
# - '@participant.complex.handler'
|
||||
- 'App\Form\Service\ParticipantAdditionalServicesFieldHandler'
|
||||
- 'App\Form\Service\ParticipantCoursesFieldHandler'
|
||||
- 'App\Form\Service\ParticipantBoardFieldHandler'
|
||||
- 'App\Form\Service\ParticipantRentalsFieldHandler'
|
||||
|
||||
@@ -19,7 +19,7 @@ class AgeConstraintResult
|
||||
public readonly ?int $birthYearFrom = null,
|
||||
public readonly ?int $birthYearTo = null,
|
||||
public readonly array $metadata = [],
|
||||
public readonly ?string $rawData = null
|
||||
public readonly ?string $rawData = null,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -37,4 +37,4 @@ class AgeConstraintResult
|
||||
{
|
||||
return false === $this->hasAgeConstraints() && false === $this->hasBirthYearConstraints();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,4 +42,4 @@ interface AgeConstraintParserInterface
|
||||
* @return string The constraint type identifier (e.g., 'birth_year', 'grade_level')
|
||||
*/
|
||||
public function getConstraintType(): string;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,4 +112,4 @@ class AgeConstraintParserRegistry
|
||||
|
||||
return min($current, $new); // Most restrictive maximum
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ class BirthYearConstraintParser implements AgeConstraintParserInterface
|
||||
public function parse(string $constraintData): AgeConstraintResult
|
||||
{
|
||||
if (false === $this->canParse($constraintData)) {
|
||||
throw new \InvalidArgumentException('Cannot parse constraint data: ' . $constraintData);
|
||||
throw new \InvalidArgumentException('Cannot parse constraint data: '.$constraintData);
|
||||
}
|
||||
|
||||
$yearData = substr($constraintData, strlen(self::BIRTH_YEAR_PREFIX));
|
||||
@@ -64,4 +64,4 @@ class BirthYearConstraintParser implements AgeConstraintParserInterface
|
||||
{
|
||||
return 'birth_year';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ class TravelParser extends AbstractParser
|
||||
{
|
||||
$this->ageConstraintRegistry = new AgeConstraintParserRegistry();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse XML node into a Travel object.
|
||||
*
|
||||
|
||||
@@ -98,7 +98,7 @@ class CreateStep2Controller extends AbstractController
|
||||
$availableRooms = $bookingCreateDto->travel->getAvailableRooms();
|
||||
$groupedSelectedRooms = $this->bookingService->groupRoomSelectionsByType($bookingCreateDto->getSelectedRooms(), $availableRooms);
|
||||
|
||||
// The DTO is now updated with the latest selection.
|
||||
// The DTO is now updated with the latest selection and submitted data has been cleaned.
|
||||
// We can now render the blocks with the fresh data.
|
||||
return $this->htmxOobResponse(
|
||||
'booking/create_step_2.html.twig',
|
||||
|
||||
@@ -55,8 +55,9 @@ class BookingCreateStep2Type extends AbstractType
|
||||
/** @var BookingCreateDto $bookingDto */
|
||||
$bookingDto = $form->getData();
|
||||
|
||||
// Process all registered participant field handlers
|
||||
$this->participantFieldHandlerRegistry->processFields($submittedData, $bookingDto);
|
||||
// Process field handlers and synchronize submitted data with cleaned DTO state
|
||||
$cleanedSubmittedData = $this->participantFieldHandlerRegistry->processFieldsAndSync($submittedData, $bookingDto);
|
||||
$event->setData($cleanedSubmittedData);
|
||||
|
||||
// Rebuild the 'participants' field with the updated DTO.
|
||||
$this->addParticipantsField($form);
|
||||
|
||||
@@ -66,4 +66,4 @@ class DateOfBirthProvidedCondition implements FieldConditionInterface
|
||||
{
|
||||
return 'Participant must provide their date of birth';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?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 board field for booking participants.
|
||||
*
|
||||
* This handler manages board/meal option selections for participants in the booking
|
||||
* creation process. It processes the board 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 ParticipantBoardFieldHandler extends AbstractParticipantFieldHandler
|
||||
{
|
||||
public function getFieldName(): string
|
||||
{
|
||||
return 'board';
|
||||
}
|
||||
|
||||
public function getDependencies(): array
|
||||
{
|
||||
return ['dateOfBirth'];
|
||||
}
|
||||
|
||||
public function processField(array $submittedData, BookingDtoInterface $bookingDto, int $participantIndex): void
|
||||
{
|
||||
$participant = $this->getParticipant($bookingDto, $participantIndex);
|
||||
if (null === $participant) {
|
||||
return;
|
||||
}
|
||||
|
||||
$selectedBoard = $this->getFieldValue($submittedData, $this->getFieldName()) ?? [];
|
||||
$availableBoard = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_BOARD);
|
||||
|
||||
$validSelections = $this->filterValidServiceSelections(
|
||||
$selectedBoard,
|
||||
$availableBoard,
|
||||
$bookingDto,
|
||||
$participantIndex
|
||||
);
|
||||
|
||||
$participant->board = $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)) {
|
||||
$validSelections[] = $selectedService;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
<?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 courses field for booking participants.
|
||||
*
|
||||
* This handler manages course selections for participants in the booking
|
||||
* creation process. It processes the courses field from form submissions,
|
||||
* filters out age-inappropriate courses, and updates the participant DTO with only
|
||||
* valid selections.
|
||||
*
|
||||
* Key responsibilities:
|
||||
* - Validates course selections against age constraints
|
||||
* - Removes courses that are no longer available due to age changes
|
||||
* - Maintains data consistency during HTMX form updates
|
||||
* - Prevents form validation errors from stale course selections
|
||||
*
|
||||
* Dependencies: dateOfBirth (must be processed first for age evaluation)
|
||||
*/
|
||||
class ParticipantCoursesFieldHandler extends AbstractParticipantFieldHandler
|
||||
{
|
||||
/**
|
||||
* Returns the form field name this handler processes.
|
||||
*
|
||||
* @return string The field name 'courses'
|
||||
*/
|
||||
public function getFieldName(): string
|
||||
{
|
||||
return 'courses';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 courses field for a specific participant.
|
||||
*
|
||||
* This method extracts course selections from submitted form data,
|
||||
* validates each selection against the participant's age constraints, and
|
||||
* updates the participant DTO with only valid selections. Courses that are
|
||||
* no longer appropriate for the participant's age are automatically removed.
|
||||
*
|
||||
* @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 course selections from submitted data
|
||||
$selectedCourses = $this->getFieldValue($submittedData, $this->getFieldName()) ?? [];
|
||||
|
||||
// Get available courses from travel data
|
||||
$availableCourses = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_COURSES);
|
||||
|
||||
// Filter selections to keep only age-appropriate courses
|
||||
$validSelections = $this->filterValidServiceSelections(
|
||||
$selectedCourses,
|
||||
$availableCourses,
|
||||
$bookingDto,
|
||||
$participantIndex
|
||||
);
|
||||
|
||||
// Update participant with validated selections
|
||||
$participant->courses = $validSelections;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters course selections to keep only those valid for the participant's age.
|
||||
*
|
||||
* @param array $selectedServices List of currently selected courses
|
||||
* @param array $availableServices List of all available courses
|
||||
* @param BookingDtoInterface $bookingDto The booking DTO for context
|
||||
* @param int $participantIndex The participant index for age evaluation
|
||||
*
|
||||
* @return array Filtered array of valid course 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 course is still valid for the participant.
|
||||
*
|
||||
* @param mixed $selectedService The selected course to validate
|
||||
* @param array $availableServices Array of available courses
|
||||
* @param BookingDtoInterface $bookingDto The booking DTO for context
|
||||
* @param int $participantIndex The participant index for age evaluation
|
||||
*
|
||||
* @return bool True if the course 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 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;
|
||||
}
|
||||
}
|
||||
@@ -107,4 +107,4 @@ class ParticipantDateOfBirthFieldHandler extends AbstractParticipantFieldHandler
|
||||
// Unsupported type, return null
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +62,28 @@ class ParticipantFieldHandlerRegistry
|
||||
$this->sortedHandlers = null; // Reset cache to force re-sorting with new handler
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes all participant fields and synchronizes submitted data with cleaned DTO state.
|
||||
*
|
||||
* This method combines field processing with data synchronization to ensure that
|
||||
* the submitted form data reflects any changes made by field handlers. This is
|
||||
* particularly useful for HTMX form updates where invalid selections need to be
|
||||
* automatically cleared.
|
||||
*
|
||||
* @param array<string, mixed> $submittedData The submitted form data containing participants array
|
||||
* @param BookingDtoInterface $bookingDto The booking DTO to update with processed field values
|
||||
*
|
||||
* @return array<string, mixed> The synchronized submitted data reflecting DTO changes
|
||||
*/
|
||||
public function processFieldsAndSync(array $submittedData, BookingDtoInterface $bookingDto): array
|
||||
{
|
||||
// Process all field handlers to clean the DTO
|
||||
$this->processFields($submittedData, $bookingDto);
|
||||
|
||||
// Synchronize submitted data with the cleaned DTO state
|
||||
return $this->syncSubmittedDataWithDto($submittedData, $bookingDto);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes all participant fields from submitted form data using registered handlers.
|
||||
*
|
||||
@@ -201,4 +223,131 @@ class ParticipantFieldHandlerRegistry
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronizes submitted data with the cleaned DTO state.
|
||||
*
|
||||
* This method updates the submitted form data to reflect any changes made by
|
||||
* field handlers (such as clearing invalid service selections). This ensures
|
||||
* that the form continues processing with cleaned data rather than the original
|
||||
* submitted data that may contain invalid selections.
|
||||
*
|
||||
* The synchronization is generic and works with any field handlers by examining
|
||||
* the current DTO state and updating the corresponding submitted data fields.
|
||||
*
|
||||
* @param array<string, mixed> $submittedData The original submitted form data
|
||||
* @param BookingDtoInterface $bookingDto The DTO with cleaned data from field handlers
|
||||
*
|
||||
* @return array<string, mixed> Updated submitted data reflecting DTO state
|
||||
*/
|
||||
private function syncSubmittedDataWithDto(array $submittedData, BookingDtoInterface $bookingDto): array
|
||||
{
|
||||
// Ensure participants array exists in submitted data
|
||||
if (!isset($submittedData['participants']) || !is_array($submittedData['participants'])) {
|
||||
return $submittedData;
|
||||
}
|
||||
|
||||
// Sync each participant's data with the cleaned DTO
|
||||
foreach ($submittedData['participants'] as $index => $participantData) {
|
||||
if (!is_array($participantData)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$participant = $bookingDto->getParticipant((int) $index);
|
||||
if (null === $participant) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Update participant data to match cleaned DTO state
|
||||
$submittedData['participants'][$index] = $this->syncParticipantData($participantData, $participant);
|
||||
}
|
||||
|
||||
return $submittedData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronizes individual participant submitted data with cleaned participant DTO.
|
||||
*
|
||||
* This method examines the participant DTO and updates the submitted data to match
|
||||
* any changes made by field handlers. It automatically detects which fields have
|
||||
* been processed by checking against registered handlers.
|
||||
*
|
||||
* @param array<string, mixed> $participantData The submitted participant data
|
||||
* @param object $participant The cleaned participant DTO
|
||||
*
|
||||
* @return array<string, mixed> Updated participant data with synchronized field values
|
||||
*/
|
||||
private function syncParticipantData(array $participantData, object $participant): array
|
||||
{
|
||||
// Sync fields for all registered handlers
|
||||
foreach ($this->handlers as $fieldName => $handler) {
|
||||
if (property_exists($participant, $fieldName)) {
|
||||
$dtoValue = $participant->{$fieldName};
|
||||
$participantData[$fieldName] = $this->convertDtoValueToSubmittedFormat($dtoValue);
|
||||
}
|
||||
}
|
||||
|
||||
return $participantData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts DTO field values to the format expected in submitted form data.
|
||||
*
|
||||
* This method handles the conversion from DTO field values to the format
|
||||
* that Symfony forms expect in submitted data. It supports various data types
|
||||
* including service objects, arrays, and primitive values.
|
||||
*
|
||||
* @param mixed $dtoValue The field value from the DTO
|
||||
*
|
||||
* @return mixed The value in submitted data format
|
||||
*/
|
||||
private function convertDtoValueToSubmittedFormat(mixed $dtoValue): mixed
|
||||
{
|
||||
// Handle null values
|
||||
if (null === $dtoValue) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Handle arrays (service collections, etc.)
|
||||
if (is_array($dtoValue)) {
|
||||
$submittedFormat = [];
|
||||
|
||||
foreach ($dtoValue as $item) {
|
||||
$submittedFormat[] = $this->convertSingleValueToSubmittedFormat($item);
|
||||
}
|
||||
|
||||
return $submittedFormat;
|
||||
}
|
||||
|
||||
// Handle single values
|
||||
return $this->convertSingleValueToSubmittedFormat($dtoValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a single DTO value to submitted form format.
|
||||
*
|
||||
* @param mixed $value The value to convert
|
||||
*
|
||||
* @return mixed The converted value
|
||||
*/
|
||||
private function convertSingleValueToSubmittedFormat(mixed $value): mixed
|
||||
{
|
||||
// Handle Service objects -> convert to ID
|
||||
if ($value instanceof \App\BusProNet\Model\Service) {
|
||||
return $value->id;
|
||||
}
|
||||
|
||||
// Handle DateTimeInterface -> convert to string format
|
||||
if ($value instanceof \DateTimeInterface) {
|
||||
return $value->format('Y-m-d');
|
||||
}
|
||||
|
||||
// Handle numeric values
|
||||
if (is_numeric($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
// Handle strings and other primitive types
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ use App\Form\Model\BookingCreateDto;
|
||||
use App\Form\Model\BookingDtoInterface;
|
||||
use App\Form\Service\Abstract\AbstractFieldOptionsProvider;
|
||||
use App\Form\Service\Factory\ParticipantRoomChoiceLoaderFactory;
|
||||
use App\Form\Service\ServiceAgeEvaluator;
|
||||
|
||||
/**
|
||||
* Provides dynamic field options for participant form fields.
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
<?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';
|
||||
}
|
||||
|
||||
public function getDependencies(): array
|
||||
{
|
||||
return ['dateOfBirth'];
|
||||
}
|
||||
|
||||
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)) {
|
||||
$validSelections[] = $selectedService;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -49,12 +49,12 @@ class ServiceAgeEvaluator
|
||||
return false; // Cannot evaluate without birth date
|
||||
}
|
||||
|
||||
return match($service->ageConstraintType) {
|
||||
return match ($service->ageConstraintType) {
|
||||
'absolute_age' => $this->evaluateAbsoluteAge($service, $participant->dateOfBirth),
|
||||
'birth_year' => $this->evaluateBirthYear($service, $participant->dateOfBirth),
|
||||
'mixed' => $this->evaluateAbsoluteAge($service, $participant->dateOfBirth)
|
||||
'mixed' => $this->evaluateAbsoluteAge($service, $participant->dateOfBirth)
|
||||
&& $this->evaluateBirthYear($service, $participant->dateOfBirth),
|
||||
default => true // No constraints or unknown type
|
||||
default => true, // No constraints or unknown type
|
||||
};
|
||||
}
|
||||
|
||||
@@ -133,13 +133,13 @@ class ServiceAgeEvaluator
|
||||
*/
|
||||
public function getConstraintDescription(Service $service): string
|
||||
{
|
||||
return match($service->ageConstraintType) {
|
||||
return match ($service->ageConstraintType) {
|
||||
'absolute_age' => $this->getAbsoluteAgeDescription($service),
|
||||
'birth_year' => $this->getBirthYearDescription($service),
|
||||
'mixed' => sprintf('%s and %s',
|
||||
'mixed' => sprintf('%s and %s',
|
||||
$this->getAbsoluteAgeDescription($service),
|
||||
$this->getBirthYearDescription($service)),
|
||||
default => 'No age restrictions'
|
||||
default => 'No age restrictions',
|
||||
};
|
||||
}
|
||||
|
||||
@@ -180,6 +180,7 @@ class ServiceAgeEvaluator
|
||||
if ($service->birthYearFrom === $service->birthYearTo) {
|
||||
return sprintf('Born in %d', $service->birthYearFrom);
|
||||
}
|
||||
|
||||
return sprintf('Born %d-%d', $service->birthYearFrom, $service->birthYearTo);
|
||||
}
|
||||
|
||||
@@ -193,4 +194,4 @@ class ServiceAgeEvaluator
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user