92 lines
3.4 KiB
PHP
92 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Form\Service\Abstract;
|
|
|
|
use App\Form\Model\BookingDtoInterface;
|
|
use App\Form\Service\Contract\FieldOptionsProviderInterface;
|
|
|
|
/**
|
|
* Abstract base class for field options providers.
|
|
*
|
|
* This class contains common field options generation logic shared between
|
|
* different field options provider implementations. It provides the core
|
|
* functionality for managing field option providers and generating dynamic
|
|
* field configurations.
|
|
*/
|
|
abstract class AbstractFieldOptionsProvider implements FieldOptionsProviderInterface
|
|
{
|
|
/** @var array<string, callable> Field option providers indexed by field name */
|
|
protected array $fieldOptionProviders = [];
|
|
|
|
public function __construct()
|
|
{
|
|
$this->registerFieldOptionProviders();
|
|
}
|
|
|
|
/**
|
|
* Retrieves form field options for a specified dynamic field.
|
|
*
|
|
* This method looks up the appropriate option provider for the field
|
|
* and executes it with the current booking and participant context to
|
|
* generate dynamic field options.
|
|
*
|
|
* The returned array contains Symfony form field options that will be
|
|
* used when building the form field. These options are merged with any
|
|
* static options defined in the form type.
|
|
*
|
|
* @param string $fieldName The name of the field to configure
|
|
* @param BookingDtoInterface $bookingDto The current booking data for context (create or edit)
|
|
* @param int $participantIndex The index of the participant being configured
|
|
*
|
|
* @return array<string, mixed> Symfony form field options, or empty array if field not supported
|
|
*/
|
|
public function getFieldOptions(string $fieldName, BookingDtoInterface $bookingDto, int $participantIndex): array
|
|
{
|
|
// Check if we have a provider for this field
|
|
if (!isset($this->fieldOptionProviders[$fieldName])) {
|
|
return [];
|
|
}
|
|
|
|
// Execute the provider with current context to generate dynamic options
|
|
return $this->fieldOptionProviders[$fieldName]($bookingDto, $participantIndex);
|
|
}
|
|
|
|
/**
|
|
* Checks whether a field has option provider support.
|
|
*
|
|
* This method allows form builders to determine if a field can be
|
|
* dynamically configured by this service. It's useful for deciding
|
|
* whether to use static field options or dynamic configuration.
|
|
*
|
|
* @param string $fieldName The name of the field to check
|
|
*
|
|
* @return bool True if the field has registered option providers, false otherwise
|
|
*/
|
|
public function hasFieldOptions(string $fieldName): bool
|
|
{
|
|
return isset($this->fieldOptionProviders[$fieldName]);
|
|
}
|
|
|
|
/**
|
|
* Registers field option providers during service initialization.
|
|
*
|
|
* This method must be implemented by concrete classes to define their
|
|
* specific field option providers. Each provider is a callable that
|
|
* receives the booking DTO and participant index and returns appropriate
|
|
* Symfony form field options.
|
|
*
|
|
* Example implementation:
|
|
*
|
|
* protected function registerFieldOptionProviders(): void
|
|
* {
|
|
* $this->fieldOptionProviders['fieldName'] = fn($bookingDto, $participantIndex) => [
|
|
* 'label' => 'Field Label',
|
|
* 'choices' => $this->generateChoicesFor($bookingDto, $participantIndex),
|
|
* ];
|
|
* }
|
|
*/
|
|
abstract protected function registerFieldOptionProviders(): void;
|
|
}
|