Files
myep/src/Form/Service/ParticipantVegFieldHandler.php
T

139 lines
5.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Form\Service;
use App\BusProNet\Constants;
use App\BusProNet\Model\Service;
use App\Form\Model\BookingDto;
use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
/**
* Handles processing of the veg (vegetarian/vegan) field for booking participants.
*
* This handler manages dietary preference selection for participants in the booking
* process. It processes the veg field from form submissions, validates age-appropriate
* options if constraints exist, and updates the participant DTO with the valid selection.
*
* The field is rendered as radio buttons (mutually exclusive single selection) since
* vegetarian and vegan options should not be combined.
*
* Dependencies: dateOfBirth (for potential future age constraints)
*/
class ParticipantVegFieldHandler extends AbstractParticipantFieldHandler
{
/**
* Returns the form field name this handler processes.
*
* @return string The field name 'veg'
*/
public function getFieldName(): string
{
return 'veg';
}
/**
* 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'];
}
/**
* Determines if this handler should process the field based on submitted data.
*
* For service selection fields, we always need to process to handle cases where
* the selection is cleared (field not present in data). This ensures the participant
* DTO is updated with null when no option is selected.
*
* @param array<string, mixed> $submittedData The submitted participant form data
* @param string $mode The booking mode (BookingDto::MODE_CREATE or MODE_EDIT)
* @param int $participantIndex The index of the participant being processed
*
* @return bool Always returns true for service selection fields
*/
public function shouldProcess(array $submittedData, string $mode, int $participantIndex): bool
{
return true;
}
/**
* Processes the veg field for a specific participant.
*
* This method extracts the dietary preference selection from submitted form data,
* validates the selection against any age constraints if present, and updates the
* participant DTO with the valid selection.
*
* @param array<string, mixed> $submittedData The submitted participant form data
* @param BookingDto $bookingDto The booking DTO to update (create or edit)
* @param int $participantIndex The index of the participant being processed
*/
public function processField(array $submittedData, BookingDto $bookingDto, int $participantIndex): void
{
$participant = $this->getParticipant($bookingDto, $participantIndex);
if (null === $participant) {
return;
}
$selectedVeg = $this->getFieldValue($submittedData, $this->getFieldName());
// Get available veg options from travel data (includes booked options in edit mode)
$availableVegOptions = $this->getAvailableServicesWithBooked(
$bookingDto,
$participantIndex,
Constants::TOKEN_VEG,
true
);
$validSelection = null;
if (null !== $selectedVeg) {
if ($this->isServiceValidForParticipant($selectedVeg, $availableVegOptions, $bookingDto, $participantIndex)) {
$validSelection = $this->findServiceInAvailableServices($selectedVeg, $availableVegOptions);
}
}
$participant->veg = $validSelection;
}
/**
* Validates if a selected veg option is still valid for the participant.
*
* This method checks age constraints if they exist for the service.
*
* @param mixed $selectedService The selected veg option to validate
* @param array<int, Service> $availableServices Array of available veg options
* @param BookingDto $bookingDto The booking DTO for context
* @param int $participantIndex The participant index for age evaluation
*
* @return bool True if the option is valid for the participant, false otherwise
*/
private function isServiceValidForParticipant(
mixed $selectedService,
array $availableServices,
BookingDto $bookingDto,
int $participantIndex,
): bool {
$service = $this->findServiceInAvailableServices($selectedService, $availableServices);
if (null === $service) {
return false;
}
$ageEvaluator = new ServiceAgeEvaluator();
if ($ageEvaluator->canEvaluate($service)) {
if (false === $ageEvaluator->isServiceAvailableForParticipant($service, $bookingDto, $participantIndex)) {
return false;
}
}
return true;
}
}