vendor/symfony/form/FormBuilder.php line 183

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.         // Add to "children" to maintain order
  58.         $this->children[$child] = null;
  59.         $this->unresolvedChildren[$child] = [$type$options];
  60.         return $this;
  61.     }
  62.     /**
  63.      * {@inheritdoc}
  64.      */
  65.     public function create(string $namestring $type null, array $options = []): FormBuilderInterface
  66.     {
  67.         if ($this->locked) {
  68.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  69.         }
  70.         if (null === $type && null === $this->getDataClass()) {
  71.             $type TextType::class;
  72.         }
  73.         if (null !== $type) {
  74.             return $this->getFormFactory()->createNamedBuilder($name$typenull$options);
  75.         }
  76.         return $this->getFormFactory()->createBuilderForProperty($this->getDataClass(), $namenull$options);
  77.     }
  78.     /**
  79.      * {@inheritdoc}
  80.      */
  81.     public function get(string $name): FormBuilderInterface
  82.     {
  83.         if ($this->locked) {
  84.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  85.         }
  86.         if (isset($this->unresolvedChildren[$name])) {
  87.             return $this->resolveChild($name);
  88.         }
  89.         if (isset($this->children[$name])) {
  90.             return $this->children[$name];
  91.         }
  92.         throw new InvalidArgumentException(sprintf('The child with the name "%s" does not exist.'$name));
  93.     }
  94.     /**
  95.      * {@inheritdoc}
  96.      */
  97.     public function remove(string $name): static
  98.     {
  99.         if ($this->locked) {
  100.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  101.         }
  102.         unset($this->unresolvedChildren[$name], $this->children[$name]);
  103.         return $this;
  104.     }
  105.     /**
  106.      * {@inheritdoc}
  107.      */
  108.     public function has(string $name): bool
  109.     {
  110.         if ($this->locked) {
  111.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  112.         }
  113.         return isset($this->unresolvedChildren[$name]) || isset($this->children[$name]);
  114.     }
  115.     /**
  116.      * {@inheritdoc}
  117.      */
  118.     public function all(): array
  119.     {
  120.         if ($this->locked) {
  121.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  122.         }
  123.         $this->resolveChildren();
  124.         return $this->children;
  125.     }
  126.     public function count(): int
  127.     {
  128.         if ($this->locked) {
  129.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  130.         }
  131.         return \count($this->children);
  132.     }
  133.     /**
  134.      * {@inheritdoc}
  135.      */
  136.     public function getFormConfig(): FormConfigInterface
  137.     {
  138.         /** @var $config self */
  139.         $config parent::getFormConfig();
  140.         $config->children = [];
  141.         $config->unresolvedChildren = [];
  142.         return $config;
  143.     }
  144.     /**
  145.      * {@inheritdoc}
  146.      */
  147.     public function getForm(): FormInterface
  148.     {
  149.         if ($this->locked) {
  150.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  151.         }
  152.         $this->resolveChildren();
  153.         $form = new Form($this->getFormConfig());
  154.         foreach ($this->children as $child) {
  155.             // Automatic initialization is only supported on root forms
  156.             $form->add($child->setAutoInitialize(false)->getForm());
  157.         }
  158.         if ($this->getAutoInitialize()) {
  159.             // Automatically initialize the form if it is configured so
  160.             $form->initialize();
  161.         }
  162.         return $form;
  163.     }
  164.     /**
  165.      * {@inheritdoc}
  166.      *
  167.      * @return \Traversable<string, FormBuilderInterface>
  168.      */
  169.     public function getIterator(): \Traversable
  170.     {
  171.         if ($this->locked) {
  172.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  173.         }
  174.         return new \ArrayIterator($this->all());
  175.     }
  176.     /**
  177.      * Converts an unresolved child into a {@link FormBuilderInterface} instance.
  178.      */
  179.     private function resolveChild(string $name): FormBuilderInterface
  180.     {
  181.         [$type$options] = $this->unresolvedChildren[$name];
  182.         unset($this->unresolvedChildren[$name]);
  183.         return $this->children[$name] = $this->create($name$type$options);
  184.     }
  185.     /**
  186.      * Converts all unresolved children into {@link FormBuilder} instances.
  187.      */
  188.     private function resolveChildren()
  189.     {
  190.         foreach ($this->unresolvedChildren as $name => $info) {
  191.             $this->children[$name] = $this->create($name$info[0], $info[1]);
  192.         }
  193.         $this->unresolvedChildren = [];
  194.     }
  195. }