vendor/symfony/form/ResolvedFormType.php line 116

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Form;
  11. use Symfony\Component\EventDispatcher\EventDispatcher;
  12. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  13. use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
  14. use Symfony\Component\OptionsResolver\OptionsResolver;
  15. /**
  16.  * A wrapper for a form type and its extensions.
  17.  *
  18.  * @author Bernhard Schussek <bschussek@gmail.com>
  19.  */
  20. class ResolvedFormType implements ResolvedFormTypeInterface
  21. {
  22.     private FormTypeInterface $innerType;
  23.     /**
  24.      * @var FormTypeExtensionInterface[]
  25.      */
  26.     private array $typeExtensions;
  27.     private ?ResolvedFormTypeInterface $parent;
  28.     private OptionsResolver $optionsResolver;
  29.     /**
  30.      * @param FormTypeExtensionInterface[] $typeExtensions
  31.      */
  32.     public function __construct(FormTypeInterface $innerType, array $typeExtensions = [], ResolvedFormTypeInterface $parent null)
  33.     {
  34.         foreach ($typeExtensions as $extension) {
  35.             if (!$extension instanceof FormTypeExtensionInterface) {
  36.                 throw new UnexpectedTypeException($extensionFormTypeExtensionInterface::class);
  37.             }
  38.         }
  39.         $this->innerType $innerType;
  40.         $this->typeExtensions $typeExtensions;
  41.         $this->parent $parent;
  42.     }
  43.     /**
  44.      * {@inheritdoc}
  45.      */
  46.     public function getBlockPrefix(): string
  47.     {
  48.         return $this->innerType->getBlockPrefix();
  49.     }
  50.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     public function getParent(): ?ResolvedFormTypeInterface
  54.     {
  55.         return $this->parent;
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      */
  60.     public function getInnerType(): FormTypeInterface
  61.     {
  62.         return $this->innerType;
  63.     }
  64.     /**
  65.      * {@inheritdoc}
  66.      */
  67.     public function getTypeExtensions(): array
  68.     {
  69.         return $this->typeExtensions;
  70.     }
  71.     /**
  72.      * {@inheritdoc}
  73.      */
  74.     public function createBuilder(FormFactoryInterface $factorystring $name, array $options = []): FormBuilderInterface
  75.     {
  76.         try {
  77.             $options $this->getOptionsResolver()->resolve($options);
  78.         } catch (ExceptionInterface $e) {
  79.             throw new $e(sprintf('An error has occurred resolving the options of the form "%s": 'get_debug_type($this->getInnerType())).$e->getMessage(), $e->getCode(), $e);
  80.         }
  81.         // Should be decoupled from the specific option at some point
  82.         $dataClass $options['data_class'] ?? null;
  83.         $builder $this->newBuilder($name$dataClass$factory$options);
  84.         $builder->setType($this);
  85.         return $builder;
  86.     }
  87.     /**
  88.      * {@inheritdoc}
  89.      */
  90.     public function createView(FormInterface $formFormView $parent null): FormView
  91.     {
  92.         return $this->newView($parent);
  93.     }
  94.     /**
  95.      * {@inheritdoc}
  96.      */
  97.     public function buildForm(FormBuilderInterface $builder, array $options)
  98.     {
  99.         $this->parent?->buildForm($builder$options);
  100.         $this->innerType->buildForm($builder$options);
  101.         foreach ($this->typeExtensions as $extension) {
  102.             $extension->buildForm($builder$options);
  103.         }
  104.     }
  105.     /**
  106.      * {@inheritdoc}
  107.      */
  108.     public function buildView(FormView $viewFormInterface $form, array $options)
  109.     {
  110.         $this->parent?->buildView($view$form$options);
  111.         $this->innerType->buildView($view$form$options);
  112.         foreach ($this->typeExtensions as $extension) {
  113.             $extension->buildView($view$form$options);
  114.         }
  115.     }
  116.     /**
  117.      * {@inheritdoc}
  118.      */
  119.     public function finishView(FormView $viewFormInterface $form, array $options)
  120.     {
  121.         $this->parent?->finishView($view$form$options);
  122.         $this->innerType->finishView($view$form$options);
  123.         foreach ($this->typeExtensions as $extension) {
  124.             /* @var FormTypeExtensionInterface $extension */
  125.             $extension->finishView($view$form$options);
  126.         }
  127.     }
  128.     /**
  129.      * {@inheritdoc}
  130.      */
  131.     public function getOptionsResolver(): OptionsResolver
  132.     {
  133.         if (!isset($this->optionsResolver)) {
  134.             if (null !== $this->parent) {
  135.                 $this->optionsResolver = clone $this->parent->getOptionsResolver();
  136.             } else {
  137.                 $this->optionsResolver = new OptionsResolver();
  138.             }
  139.             $this->innerType->configureOptions($this->optionsResolver);
  140.             foreach ($this->typeExtensions as $extension) {
  141.                 $extension->configureOptions($this->optionsResolver);
  142.             }
  143.         }
  144.         return $this->optionsResolver;
  145.     }
  146.     /**
  147.      * Creates a new builder instance.
  148.      *
  149.      * Override this method if you want to customize the builder class.
  150.      */
  151.     protected function newBuilder(string $name, ?string $dataClassFormFactoryInterface $factory, array $options): FormBuilderInterface
  152.     {
  153.         if ($this->innerType instanceof ButtonTypeInterface) {
  154.             return new ButtonBuilder($name$options);
  155.         }
  156.         if ($this->innerType instanceof SubmitButtonTypeInterface) {
  157.             return new SubmitButtonBuilder($name$options);
  158.         }
  159.         return new FormBuilder($name$dataClass, new EventDispatcher(), $factory$options);
  160.     }
  161.     /**
  162.      * Creates a new view instance.
  163.      *
  164.      * Override this method if you want to customize the view class.
  165.      */
  166.     protected function newView(FormView $parent null): FormView
  167.     {
  168.         return new FormView($parent);
  169.     }
  170. }