vendor/symfony/form/FormBuilder.php line 41

  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.     public function add(FormBuilderInterface|string $childstring $type null, array $options = []): static
  41.     {
  42.         if ($this->locked) {
  43.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  44.         }
  45.         if ($child instanceof FormBuilderInterface) {
  46.             $this->children[$child->getName()] = $child;
  47.             // In case an unresolved child with the same name exists
  48.             unset($this->unresolvedChildren[$child->getName()]);
  49.             return $this;
  50.         }
  51.         if (!\is_string($child) && !\is_int($child)) {
  52.             throw new UnexpectedTypeException($child'string or Symfony\Component\Form\FormBuilderInterface');
  53.         }
  54.         // Add to "children" to maintain order
  55.         $this->children[$child] = null;
  56.         $this->unresolvedChildren[$child] = [$type$options];
  57.         return $this;
  58.     }
  59.     public function create(string $namestring $type null, array $options = []): FormBuilderInterface
  60.     {
  61.         if ($this->locked) {
  62.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  63.         }
  64.         if (null === $type && null === $this->getDataClass()) {
  65.             $type TextType::class;
  66.         }
  67.         if (null !== $type) {
  68.             return $this->getFormFactory()->createNamedBuilder($name$typenull$options);
  69.         }
  70.         return $this->getFormFactory()->createBuilderForProperty($this->getDataClass(), $namenull$options);
  71.     }
  72.     public function get(string $name): FormBuilderInterface
  73.     {
  74.         if ($this->locked) {
  75.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  76.         }
  77.         if (isset($this->unresolvedChildren[$name])) {
  78.             return $this->resolveChild($name);
  79.         }
  80.         if (isset($this->children[$name])) {
  81.             return $this->children[$name];
  82.         }
  83.         throw new InvalidArgumentException(sprintf('The child with the name "%s" does not exist.'$name));
  84.     }
  85.     public function remove(string $name): static
  86.     {
  87.         if ($this->locked) {
  88.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  89.         }
  90.         unset($this->unresolvedChildren[$name], $this->children[$name]);
  91.         return $this;
  92.     }
  93.     public function has(string $name): bool
  94.     {
  95.         if ($this->locked) {
  96.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  97.         }
  98.         return isset($this->unresolvedChildren[$name]) || isset($this->children[$name]);
  99.     }
  100.     public function all(): array
  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.         $this->resolveChildren();
  106.         return $this->children;
  107.     }
  108.     public function count(): int
  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 \count($this->children);
  114.     }
  115.     public function getFormConfig(): FormConfigInterface
  116.     {
  117.         /** @var $config self */
  118.         $config parent::getFormConfig();
  119.         $config->children = [];
  120.         $config->unresolvedChildren = [];
  121.         return $config;
  122.     }
  123.     public function getForm(): FormInterface
  124.     {
  125.         if ($this->locked) {
  126.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  127.         }
  128.         $this->resolveChildren();
  129.         $form = new Form($this->getFormConfig());
  130.         foreach ($this->children as $child) {
  131.             // Automatic initialization is only supported on root forms
  132.             $form->add($child->setAutoInitialize(false)->getForm());
  133.         }
  134.         if ($this->getAutoInitialize()) {
  135.             // Automatically initialize the form if it is configured so
  136.             $form->initialize();
  137.         }
  138.         return $form;
  139.     }
  140.     /**
  141.      * @return \Traversable<string, FormBuilderInterface>
  142.      */
  143.     public function getIterator(): \Traversable
  144.     {
  145.         if ($this->locked) {
  146.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  147.         }
  148.         return new \ArrayIterator($this->all());
  149.     }
  150.     /**
  151.      * Converts an unresolved child into a {@link FormBuilderInterface} instance.
  152.      */
  153.     private function resolveChild(string $name): FormBuilderInterface
  154.     {
  155.         [$type$options] = $this->unresolvedChildren[$name];
  156.         unset($this->unresolvedChildren[$name]);
  157.         return $this->children[$name] = $this->create($name$type$options);
  158.     }
  159.     /**
  160.      * Converts all unresolved children into {@link FormBuilder} instances.
  161.      */
  162.     private function resolveChildren()
  163.     {
  164.         foreach ($this->unresolvedChildren as $name => $info) {
  165.             $this->children[$name] = $this->create($name$info[0], $info[1]);
  166.         }
  167.         $this->unresolvedChildren = [];
  168.     }
  169. }