feat: special treatment of participants with age 0-2 (babies)

This commit is contained in:
Björn Fromme
2026-03-16 11:59:12 +01:00
parent cd1ffc3e9a
commit 2e40cbc7c7
12 changed files with 615 additions and 15 deletions
@@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
namespace App\Form\Service\Condition;
use App\BusProNet\Constants;
use App\Form\Model\BookingDto;
use App\Form\Service\Contract\FieldConditionInterface;
/**
* Condition that evaluates if a participant is at or under baby age (0-2 years).
*
* This condition checks if a participant's age at travel start date is at or under
* the BABY_MAX_AGE threshold (2 years). It's used to conditionally hide fields
* that are not applicable to babies, such as ski pass selection.
*
* Age is calculated at the travel start date, consistent with other age-dependent
* services in the system (insurance, ski pass, etc.).
*/
class BabyAgeCondition implements FieldConditionInterface
{
/**
* Evaluates if the participant is at or under baby age.
*
* Calculates the participant's age at travel start date and checks if it's
* at or under BABY_MAX_AGE (2 years). Returns false if the participant has
* no date of birth set.
*
* @param BookingDto $bookingDto The current booking data
* @param int $participantIndex The index of the participant being evaluated
* @param array<string, mixed> $formData Current form data (unused)
*
* @return bool True if the participant is at or under baby age, false otherwise
*/
public function evaluate(BookingDto $bookingDto, int $participantIndex, array $formData): bool
{
$participant = $bookingDto->getParticipant($participantIndex);
if (null === $participant || null === $participant->dateOfBirth) {
return false;
}
$age = $participant->getAge($bookingDto->travel->dateFrom);
if (null === $age) {
return false;
}
return $age <= Constants::BABY_MAX_AGE;
}
/**
* Returns field names that affect baby age calculation.
*
* The condition depends on the participant's date of birth field.
* When this field changes, any conditions based on baby age should be re-evaluated.
*
* @return string[] Array containing 'dateOfBirth' field name
*/
public function getDependentFields(): array
{
return ['dateOfBirth'];
}
/**
* Returns a human-readable description of the condition.
*
* @return string Description of the baby age condition
*/
public function getDescription(): string
{
return sprintf('Participant is at or under baby age (%d years)', Constants::BABY_MAX_AGE);
}
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace App\Form\Service\Condition;
use App\Form\Model\BookingDto;
use App\Form\Service\Contract\FieldConditionInterface;
/**
* Condition that checks if the booking has multiple participants.
*
* This is used to hide fields that are only relevant when there are
* multiple participants, such as the bulk insurance assignment option.
*/
class MultipleParticipantsCondition implements FieldConditionInterface
{
/**
* Evaluates if the booking has more than one participant.
*
* @param BookingDto $bookingDto The current booking data
* @param int $participantIndex The index of the participant being evaluated (unused)
* @param array<string, mixed> $formData Current form data (unused)
*
* @return bool True if the booking has 2+ participants, false otherwise
*/
public function evaluate(BookingDto $bookingDto, int $participantIndex, array $formData): bool
{
return count($bookingDto->participants) > 1;
}
public function getDependentFields(): array
{
return [];
}
public function getDescription(): string
{
return 'Checks if the booking has multiple participants (2+)';
}
}
+16 -4
View File
@@ -8,12 +8,14 @@ use App\BusProNet\Utility\DirectionMapper;
use App\Form\Service\Abstract\AbstractFieldStateProvider;
use App\Form\Service\Condition\ApplicantCondition;
use App\Form\Service\Condition\AuthenticatedUserPersonalDataCondition;
use App\Form\Service\Condition\BabyAgeCondition;
use App\Form\Service\Condition\BookingEligibilityCondition;
use App\Form\Service\Condition\BulkInsuranceBookingCondition;
use App\Form\Service\Condition\CompositeCondition;
use App\Form\Service\Condition\DateOfBirthProvidedCondition;
use App\Form\Service\Condition\FieldValueCondition;
use App\Form\Service\Condition\FinalBookingOnlyCondition;
use App\Form\Service\Condition\MultipleParticipantsCondition;
use App\Form\Service\Condition\RentalSelectionCondition;
use App\Form\Service\Condition\RoomSelectionCondition;
use App\Form\Service\Condition\ServiceSubTypeCondition;
@@ -142,6 +144,9 @@ class CreateFieldStateProvider extends AbstractFieldStateProvider
// Hide age-dependent fields when no date of birth is provided
$dateOfBirthProvidedCondition = new DateOfBirthProvidedCondition();
// Baby age condition - hide ski pass for babies (0-2 years)
$babyAgeCondition = new BabyAgeCondition();
// Age-dependent service fields are hidden until birth date is provided OR when participant is ineligible
$this->fieldStateConditions['courses'] = [
'hidden' => CompositeCondition::or(
@@ -157,11 +162,15 @@ class CreateFieldStateProvider extends AbstractFieldStateProvider
),
];
// Skipass field - hidden when participant is ineligible OR when no date of birth
// Skipass field - hidden when:
// - No date of birth provided
// - Participant is ineligible (no skipasses for their age)
// - Participant is baby age (0-2 years) - babies don't need ski passes
$this->fieldStateConditions['skiPass'] = [
'hidden' => CompositeCondition::or(
CompositeCondition::not($dateOfBirthProvidedCondition),
$bookingEligibilityCondition
$bookingEligibilityCondition,
$babyAgeCondition
),
];
@@ -184,11 +193,14 @@ class CreateFieldStateProvider extends AbstractFieldStateProvider
// Bulk insurance booking conditions
$bulkInsuranceBookingCondition = new BulkInsuranceBookingCondition();
// Show bulk insurance booking checkbox ONLY for applicant (index 0) and when insurance field is visible
// Show bulk insurance booking checkbox ONLY for applicant (index 0) when:
// - Date of birth is provided
// - There are multiple participants (bulk assignment is meaningless for single participant)
$this->fieldStateConditions['bulkInsuranceBooking'] = [
'hidden' => CompositeCondition::or(
CompositeCondition::not($dateOfBirthProvidedCondition), // Hide until date of birth provided
CompositeCondition::not(new ApplicantCondition()) // Hide for non-applicants
CompositeCondition::not(new ApplicantCondition()), // Hide for non-applicants
CompositeCondition::not(new MultipleParticipantsCondition()) // Hide when only one participant
),
];
@@ -16,7 +16,6 @@ use App\Form\Service\Factory\ParticipantRoomChoiceLoaderFactory;
use App\Service\BookingPriceCalculatorService;
use App\Service\InsuranceService;
use App\Service\ServiceAvailabilityCalculator;
use App\Validator\Constraints\RoomSelection;
/**
* Provides dynamic field options for participant form fields.
@@ -580,9 +579,9 @@ class ParticipantFieldOptionsProvider extends AbstractFieldOptionsProvider
return false;
}
// Baby rooms only available for participants 2 years or younger
if (RoomSelection::BABY_ROOM_CODE === $room->code) {
return $age <= 2;
// Baby rooms only available for participants at or under BABY_MAX_AGE
if (Constants::BABY_ROOM_CODE === $room->code) {
return $age <= Constants::BABY_MAX_AGE;
}
// All other rooms available for all ages