vendor/symfony/form/FormBuilder.php line 191

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. class FormBuilder extends FormConfigBuilder implements \IteratorAggregateFormBuilderInterface
  22. {
  23.     /**
  24.      * The children of the form builder.
  25.      *
  26.      * @var FormBuilderInterface[]
  27.      */
  28.     private $children = [];
  29.     /**
  30.      * The data of children who haven't been converted to form builders yet.
  31.      *
  32.      * @var array
  33.      */
  34.     private $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($child$type null, array $options = [])
  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($name$type null, array $options = [])
  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($name)
  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($name)
  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($name)
  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()
  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.     /**
  130.      * @return int
  131.      */
  132.     #[\ReturnTypeWillChange]
  133.     public function count()
  134.     {
  135.         if ($this->locked) {
  136.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  137.         }
  138.         return \count($this->children);
  139.     }
  140.     /**
  141.      * {@inheritdoc}
  142.      */
  143.     public function getFormConfig()
  144.     {
  145.         /** @var $config self */
  146.         $config parent::getFormConfig();
  147.         $config->children = [];
  148.         $config->unresolvedChildren = [];
  149.         return $config;
  150.     }
  151.     /**
  152.      * {@inheritdoc}
  153.      */
  154.     public function getForm()
  155.     {
  156.         if ($this->locked) {
  157.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  158.         }
  159.         $this->resolveChildren();
  160.         $form = new Form($this->getFormConfig());
  161.         foreach ($this->children as $child) {
  162.             // Automatic initialization is only supported on root forms
  163.             $form->add($child->setAutoInitialize(false)->getForm());
  164.         }
  165.         if ($this->getAutoInitialize()) {
  166.             // Automatically initialize the form if it is configured so
  167.             $form->initialize();
  168.         }
  169.         return $form;
  170.     }
  171.     /**
  172.      * {@inheritdoc}
  173.      *
  174.      * @return FormBuilderInterface[]|\Traversable
  175.      */
  176.     #[\ReturnTypeWillChange]
  177.     public function getIterator()
  178.     {
  179.         if ($this->locked) {
  180.             throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  181.         }
  182.         return new \ArrayIterator($this->all());
  183.     }
  184.     /**
  185.      * Converts an unresolved child into a {@link FormBuilderInterface} instance.
  186.      */
  187.     private function resolveChild(string $name): FormBuilderInterface
  188.     {
  189.         [$type$options] = $this->unresolvedChildren[$name];
  190.         unset($this->unresolvedChildren[$name]);
  191.         return $this->children[$name] = $this->create($name$type$options);
  192.     }
  193.     /**
  194.      * Converts all unresolved children into {@link FormBuilder} instances.
  195.      */
  196.     private function resolveChildren()
  197.     {
  198.         foreach ($this->unresolvedChildren as $name => $info) {
  199.             $this->children[$name] = $this->create($name$info[0], $info[1]);
  200.         }
  201.         $this->unresolvedChildren = [];
  202.     }
  203. }