vendor/symfony/form/FormBuilder.php line 193

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\EventDispatcherInterface;
  12. use Symfony\Component\Form\Exception\BadMethodCallException;
  13. use Symfony\Component\Form\Exception\InvalidArgumentException;
  14. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  15. use Symfony\Component\Form\Extension\Core\Type\TextType;
  16. /**
  17.  * A builder for creating {@link Form} instances.
  18.  *
  19.  * @author Bernhard Schussek <bschussek@gmail.com>
  20.  *
  21.  * @implements \IteratorAggregate<string, FormBuilderInterface>
  22.  */
  23. class FormBuilder extends FormConfigBuilder implements \IteratorAggregateFormBuilderInterface
  24. {
  25.     /**
  26.      * The children of the form builder.
  27.      *
  28.      * @var FormBuilderInterface[]
  29.      */
  30.     private array $children = [];
  31.     /**
  32.      * The data of children who haven't been converted to form builders yet.
  33.      */
  34.     private array $unresolvedChildren = [];
  35.     public function __construct(?string $name, ?string $dataClassEventDispatcherInterface $dispatcherFormFactoryInterface $factory, array $options = [])
  36.     {
  37.         parent::__construct($name$dataClass$dispatcher$options);
  38.         $this->setFormFactory($factory);
  39.     }
  40.     /**
  41.      * {@inheritdoc}
  42.      */
  43.     public function add(FormBuilderInterface|string $childstring $type null, array $options = []): static
  44.     {
  45.         if ($this->locked) {
  46.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  47.         }
  48.         if ($child instanceof FormBuilderInterface) {
  49.             $this->children[$child->getName()] = $child;
  50.             // In case an unresolved child with the same name exists
  51.             unset($this->unresolvedChildren[$child->getName()]);
  52.             return $this;
  53.         }
  54.         if (!\is_string($child) && !\is_int($child)) {
  55.             throw new UnexpectedTypeException($child'string or Symfony\Component\Form\FormBuilderInterface');
  56.         }
  57.         if (null !== $type && !\is_string($type)) {
  58.             throw new UnexpectedTypeException($type'string or null');
  59.         }
  60.         // Add to "children" to maintain order
  61.         $this->children[$child] = null;
  62.         $this->unresolvedChildren[$child] = [$type$options];
  63.         return $this;
  64.     }
  65.     /**
  66.      * {@inheritdoc}
  67.      */
  68.     public function create(string $namestring $type null, array $options = []): FormBuilderInterface
  69.     {
  70.         if ($this->locked) {
  71.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  72.         }
  73.         if (null === $type && null === $this->getDataClass()) {
  74.             $type TextType::class;
  75.         }
  76.         if (null !== $type) {
  77.             return $this->getFormFactory()->createNamedBuilder($name$typenull$options);
  78.         }
  79.         return $this->getFormFactory()->createBuilderForProperty($this->getDataClass(), $namenull$options);
  80.     }
  81.     /**
  82.      * {@inheritdoc}
  83.      */
  84.     public function get(string $name): FormBuilderInterface
  85.     {
  86.         if ($this->locked) {
  87.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  88.         }
  89.         if (isset($this->unresolvedChildren[$name])) {
  90.             return $this->resolveChild($name);
  91.         }
  92.         if (isset($this->children[$name])) {
  93.             return $this->children[$name];
  94.         }
  95.         throw new InvalidArgumentException(sprintf('The child with the name "%s" does not exist.'$name));
  96.     }
  97.     /**
  98.      * {@inheritdoc}
  99.      */
  100.     public function remove(string $name): static
  101.     {
  102.         if ($this->locked) {
  103.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  104.         }
  105.         unset($this->unresolvedChildren[$name], $this->children[$name]);
  106.         return $this;
  107.     }
  108.     /**
  109.      * {@inheritdoc}
  110.      */
  111.     public function has(string $name): bool
  112.     {
  113.         if ($this->locked) {
  114.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  115.         }
  116.         return isset($this->unresolvedChildren[$name]) || isset($this->children[$name]);
  117.     }
  118.     /**
  119.      * {@inheritdoc}
  120.      */
  121.     public function all(): array
  122.     {
  123.         if ($this->locked) {
  124.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  125.         }
  126.         $this->resolveChildren();
  127.         return $this->children;
  128.     }
  129.     public function count(): int
  130.     {
  131.         if ($this->locked) {
  132.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  133.         }
  134.         return \count($this->children);
  135.     }
  136.     /**
  137.      * {@inheritdoc}
  138.      */
  139.     public function getFormConfig(): FormConfigInterface
  140.     {
  141.         /** @var $config self */
  142.         $config parent::getFormConfig();
  143.         $config->children = [];
  144.         $config->unresolvedChildren = [];
  145.         return $config;
  146.     }
  147.     /**
  148.      * {@inheritdoc}
  149.      */
  150.     public function getForm(): FormInterface
  151.     {
  152.         if ($this->locked) {
  153.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  154.         }
  155.         $this->resolveChildren();
  156.         $form = new Form($this->getFormConfig());
  157.         foreach ($this->children as $child) {
  158.             // Automatic initialization is only supported on root forms
  159.             $form->add($child->setAutoInitialize(false)->getForm());
  160.         }
  161.         if ($this->getAutoInitialize()) {
  162.             // Automatically initialize the form if it is configured so
  163.             $form->initialize();
  164.         }
  165.         return $form;
  166.     }
  167.     /**
  168.      * {@inheritdoc}
  169.      *
  170.      * @return \Traversable<string, FormBuilderInterface>
  171.      */
  172.     public function getIterator(): \Traversable
  173.     {
  174.         if ($this->locked) {
  175.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  176.         }
  177.         return new \ArrayIterator($this->all());
  178.     }
  179.     /**
  180.      * Converts an unresolved child into a {@link FormBuilderInterface} instance.
  181.      */
  182.     private function resolveChild(string $name): FormBuilderInterface
  183.     {
  184.         [$type$options] = $this->unresolvedChildren[$name];
  185.         unset($this->unresolvedChildren[$name]);
  186.         return $this->children[$name] = $this->create($name$type$options);
  187.     }
  188.     /**
  189.      * Converts all unresolved children into {@link FormBuilder} instances.
  190.      */
  191.     private function resolveChildren()
  192.     {
  193.         foreach ($this->unresolvedChildren as $name => $info) {
  194.             $this->children[$name] = $this->create($name$info[0], $info[1]);
  195.         }
  196.         $this->unresolvedChildren = [];
  197.     }
  198. }