vendor/symfony/form/ResolvedFormType.php line 138

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 $innerType;
  23.     /**
  24.      * @var FormTypeExtensionInterface[]
  25.      */
  26.     private array $typeExtensions;
  27.     private $parent;
  28.     private $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.         if (null !== $this->parent) {
  100.             $this->parent->buildForm($builder$options);
  101.         }
  102.         $this->innerType->buildForm($builder$options);
  103.         foreach ($this->typeExtensions as $extension) {
  104.             $extension->buildForm($builder$options);
  105.         }
  106.     }
  107.     /**
  108.      * {@inheritdoc}
  109.      */
  110.     public function buildView(FormView $viewFormInterface $form, array $options)
  111.     {
  112.         if (null !== $this->parent) {
  113.             $this->parent->buildView($view$form$options);
  114.         }
  115.         $this->innerType->buildView($view$form$options);
  116.         foreach ($this->typeExtensions as $extension) {
  117.             $extension->buildView($view$form$options);
  118.         }
  119.     }
  120.     /**
  121.      * {@inheritdoc}
  122.      */
  123.     public function finishView(FormView $viewFormInterface $form, array $options)
  124.     {
  125.         if (null !== $this->parent) {
  126.             $this->parent->finishView($view$form$options);
  127.         }
  128.         $this->innerType->finishView($view$form$options);
  129.         foreach ($this->typeExtensions as $extension) {
  130.             /* @var FormTypeExtensionInterface $extension */
  131.             $extension->finishView($view$form$options);
  132.         }
  133.     }
  134.     /**
  135.      * {@inheritdoc}
  136.      */
  137.     public function getOptionsResolver(): OptionsResolver
  138.     {
  139.         if (!isset($this->optionsResolver)) {
  140.             if (null !== $this->parent) {
  141.                 $this->optionsResolver = clone $this->parent->getOptionsResolver();
  142.             } else {
  143.                 $this->optionsResolver = new OptionsResolver();
  144.             }
  145.             $this->innerType->configureOptions($this->optionsResolver);
  146.             foreach ($this->typeExtensions as $extension) {
  147.                 $extension->configureOptions($this->optionsResolver);
  148.             }
  149.         }
  150.         return $this->optionsResolver;
  151.     }
  152.     /**
  153.      * Creates a new builder instance.
  154.      *
  155.      * Override this method if you want to customize the builder class.
  156.      */
  157.     protected function newBuilder(string $name, ?string $dataClassFormFactoryInterface $factory, array $options): FormBuilderInterface
  158.     {
  159.         if ($this->innerType instanceof ButtonTypeInterface) {
  160.             return new ButtonBuilder($name$options);
  161.         }
  162.         if ($this->innerType instanceof SubmitButtonTypeInterface) {
  163.             return new SubmitButtonBuilder($name$options);
  164.         }
  165.         return new FormBuilder($name$dataClass, new EventDispatcher(), $factory$options);
  166.     }
  167.     /**
  168.      * Creates a new view instance.
  169.      *
  170.      * Override this method if you want to customize the view class.
  171.      */
  172.     protected function newView(FormView $parent null): FormView
  173.     {
  174.         return new FormView($parent);
  175.     }
  176. }