Files
myep/src/Form/Service/Condition/DateOfBirthProvidedCondition.php
T

77 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Form\Service\Condition;
use App\Form\Model\BookingDto;
use App\Form\Service\Contract\FieldConditionInterface;
/**
* Condition that evaluates whether a participant has provided their date of birth.
*
* This condition checks if a participant has a valid date of birth set, which is
* required for evaluating age-based service constraints and showing age-dependent
* form fields. When no birth date is provided, age-restricted fields should be
* hidden until this prerequisite is met.
*
* The condition is commonly used to control field visibility, ensuring that
* age-dependent services and options are only displayed when the participant's
* age can be calculated.
*/
class DateOfBirthProvidedCondition implements FieldConditionInterface
{
/**
* Evaluates if the participant has provided their date of birth.
*
* Checks if the participant exists and has a non-null dateOfBirth property.
* This is a prerequisite for showing age-dependent form fields and services.
*
* @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 for this condition)
*
* @return bool True if the participant has provided their date of birth, false otherwise
*/
public function evaluate(BookingDto $bookingDto, int $participantIndex, array $formData): bool
{
$participant = $bookingDto->getParticipant($participantIndex);
$result = null !== $participant && null !== $participant->dateOfBirth;
error_log(sprintf('[DOB Condition] Participant %d: dateOfBirth=%s, result=%s',
$participantIndex,
$participant?->dateOfBirth?->format('Y-m-d') ?? 'NULL',
$result ? 'TRUE' : 'FALSE'
));
return $result;
}
/**
* Returns field names that this condition depends on.
*
* The date of birth condition depends on the participant's dateOfBirth field.
* When this field changes, any conditions based on birth date availability
* should be re-evaluated.
*
* @return string[] Array containing 'dateOfBirth' field name
*/
public function getDependentFields(): array
{
return ['dateOfBirth'];
}
/**
* Returns a human-readable description of this condition.
*
* Provides a clear description of what this condition checks for,
* useful for debugging and understanding field state logic.
*
* @return string Description of the date of birth requirement
*/
public function getDescription(): string
{
return 'Participant must provide their date of birth';
}
}