vendor/symfony/form/ResolvedFormType.php line 128

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.     /**
  23.      * @var FormTypeInterface
  24.      */
  25.     private $innerType;
  26.     /**
  27.      * @var FormTypeExtensionInterface[]
  28.      */
  29.     private $typeExtensions;
  30.     /**
  31.      * @var ResolvedFormTypeInterface|null
  32.      */
  33.     private $parent;
  34.     /**
  35.      * @var OptionsResolver
  36.      */
  37.     private $optionsResolver;
  38.     public function __construct(FormTypeInterface $innerType, array $typeExtensions = [], ResolvedFormTypeInterface $parent null)
  39.     {
  40.         foreach ($typeExtensions as $extension) {
  41.             if (!$extension instanceof FormTypeExtensionInterface) {
  42.                 throw new UnexpectedTypeException($extension'Symfony\Component\Form\FormTypeExtensionInterface');
  43.             }
  44.         }
  45.         $this->innerType $innerType;
  46.         $this->typeExtensions $typeExtensions;
  47.         $this->parent $parent;
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      */
  52.     public function getBlockPrefix()
  53.     {
  54.         return $this->innerType->getBlockPrefix();
  55.     }
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     public function getParent()
  60.     {
  61.         return $this->parent;
  62.     }
  63.     /**
  64.      * {@inheritdoc}
  65.      */
  66.     public function getInnerType()
  67.     {
  68.         return $this->innerType;
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public function getTypeExtensions()
  74.     {
  75.         return $this->typeExtensions;
  76.     }
  77.     /**
  78.      * {@inheritdoc}
  79.      */
  80.     public function createBuilder(FormFactoryInterface $factorystring $name, array $options = [])
  81.     {
  82.         try {
  83.             $options $this->getOptionsResolver()->resolve($options);
  84.         } catch (ExceptionInterface $e) {
  85.             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);
  86.         }
  87.         // Should be decoupled from the specific option at some point
  88.         $dataClass $options['data_class'] ?? null;
  89.         $builder $this->newBuilder($name$dataClass$factory$options);
  90.         $builder->setType($this);
  91.         return $builder;
  92.     }
  93.     /**
  94.      * {@inheritdoc}
  95.      */
  96.     public function createView(FormInterface $formFormView $parent null)
  97.     {
  98.         return $this->newView($parent);
  99.     }
  100.     /**
  101.      * Configures a form builder for the type hierarchy.
  102.      */
  103.     public function buildForm(FormBuilderInterface $builder, array $options)
  104.     {
  105.         if (null !== $this->parent) {
  106.             $this->parent->buildForm($builder$options);
  107.         }
  108.         $this->innerType->buildForm($builder$options);
  109.         foreach ($this->typeExtensions as $extension) {
  110.             $extension->buildForm($builder$options);
  111.         }
  112.     }
  113.     /**
  114.      * Configures a form view for the type hierarchy.
  115.      *
  116.      * This method is called before the children of the view are built.
  117.      */
  118.     public function buildView(FormView $viewFormInterface $form, array $options)
  119.     {
  120.         if (null !== $this->parent) {
  121.             $this->parent->buildView($view$form$options);
  122.         }
  123.         $this->innerType->buildView($view$form$options);
  124.         foreach ($this->typeExtensions as $extension) {
  125.             $extension->buildView($view$form$options);
  126.         }
  127.     }
  128.     /**
  129.      * Finishes a form view for the type hierarchy.
  130.      *
  131.      * This method is called after the children of the view have been built.
  132.      */
  133.     public function finishView(FormView $viewFormInterface $form, array $options)
  134.     {
  135.         if (null !== $this->parent) {
  136.             $this->parent->finishView($view$form$options);
  137.         }
  138.         $this->innerType->finishView($view$form$options);
  139.         foreach ($this->typeExtensions as $extension) {
  140.             /* @var FormTypeExtensionInterface $extension */
  141.             $extension->finishView($view$form$options);
  142.         }
  143.     }
  144.     /**
  145.      * Returns the configured options resolver used for this type.
  146.      *
  147.      * @return \Symfony\Component\OptionsResolver\OptionsResolver The options resolver
  148.      */
  149.     public function getOptionsResolver()
  150.     {
  151.         if (null === $this->optionsResolver) {
  152.             if (null !== $this->parent) {
  153.                 $this->optionsResolver = clone $this->parent->getOptionsResolver();
  154.             } else {
  155.                 $this->optionsResolver = new OptionsResolver();
  156.             }
  157.             $this->innerType->configureOptions($this->optionsResolver);
  158.             foreach ($this->typeExtensions as $extension) {
  159.                 $extension->configureOptions($this->optionsResolver);
  160.             }
  161.         }
  162.         return $this->optionsResolver;
  163.     }
  164.     /**
  165.      * Creates a new builder instance.
  166.      *
  167.      * Override this method if you want to customize the builder class.
  168.      *
  169.      * @return FormBuilderInterface The new builder instance
  170.      */
  171.     protected function newBuilder(string $name, ?string $dataClassFormFactoryInterface $factory, array $options)
  172.     {
  173.         if ($this->innerType instanceof ButtonTypeInterface) {
  174.             return new ButtonBuilder($name$options);
  175.         }
  176.         if ($this->innerType instanceof SubmitButtonTypeInterface) {
  177.             return new SubmitButtonBuilder($name$options);
  178.         }
  179.         return new FormBuilder($name$dataClass, new EventDispatcher(), $factory$options);
  180.     }
  181.     /**
  182.      * Creates a new view instance.
  183.      *
  184.      * Override this method if you want to customize the view class.
  185.      *
  186.      * @return FormView A new view instance
  187.      */
  188.     protected function newView(FormView $parent null)
  189.     {
  190.         return new FormView($parent);
  191.     }
  192. }