FormBuilder.php 7.1 KB

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