wip: age based filtering of options
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user