vendor/symfony/form/FormView.php line 22

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\Form\Exception\BadMethodCallException;
  12. /**
  13. * @author Bernhard Schussek <bschussek@gmail.com>
  14. *
  15. * @implements \ArrayAccess<int|string, FormView>
  16. * @implements \IteratorAggregate<int|string, FormView>
  17. */
  18. class FormView implements \ArrayAccess, \IteratorAggregate, \Countable
  19. {
  20. /**
  21. * The variables assigned to this view.
  22. */
  23. public $vars = [
  24. 'value' => null,
  25. 'attr' => [],
  26. ];
  27. /**
  28. * The parent view.
  29. */
  30. public $parent;
  31. /**
  32. * The child views.
  33. *
  34. * @var array<int|string, FormView>
  35. */
  36. public $children = [];
  37. /**
  38. * Is the form attached to this renderer rendered?
  39. *
  40. * Rendering happens when either the widget or the row method was called.
  41. * Row implicitly includes widget, however certain rendering mechanisms
  42. * have to skip widget rendering when a row is rendered.
  43. *
  44. * @var bool
  45. */
  46. private $rendered = false;
  47. private $methodRendered = false;
  48. public function __construct(?self $parent = null)
  49. {
  50. $this->parent = $parent;
  51. }
  52. /**
  53. * Returns whether the view was already rendered.
  54. *
  55. * @return bool
  56. */
  57. public function isRendered()
  58. {
  59. if (true === $this->rendered || 0 === \count($this->children)) {
  60. return $this->rendered;
  61. }
  62. foreach ($this->children as $child) {
  63. if (!$child->isRendered()) {
  64. return false;
  65. }
  66. }
  67. return $this->rendered = true;
  68. }
  69. /**
  70. * Marks the view as rendered.
  71. *
  72. * @return $this
  73. */
  74. public function setRendered()
  75. {
  76. $this->rendered = true;
  77. return $this;
  78. }
  79. /**
  80. * @return bool
  81. */
  82. public function isMethodRendered()
  83. {
  84. return $this->methodRendered;
  85. }
  86. public function setMethodRendered()
  87. {
  88. $this->methodRendered = true;
  89. }
  90. /**
  91. * Returns a child by name (implements \ArrayAccess).
  92. *
  93. * @param int|string $name The child name
  94. *
  95. * @return self
  96. */
  97. #[\ReturnTypeWillChange]
  98. public function offsetGet($name)
  99. {
  100. return $this->children[$name];
  101. }
  102. /**
  103. * Returns whether the given child exists (implements \ArrayAccess).
  104. *
  105. * @param int|string $name The child name
  106. *
  107. * @return bool
  108. */
  109. #[\ReturnTypeWillChange]
  110. public function offsetExists($name)
  111. {
  112. return isset($this->children[$name]);
  113. }
  114. /**
  115. * Implements \ArrayAccess.
  116. *
  117. * @return void
  118. *
  119. * @throws BadMethodCallException always as setting a child by name is not allowed
  120. */
  121. #[\ReturnTypeWillChange]
  122. public function offsetSet($name, $value)
  123. {
  124. throw new BadMethodCallException('Not supported.');
  125. }
  126. /**
  127. * Removes a child (implements \ArrayAccess).
  128. *
  129. * @param int|string $name The child name
  130. *
  131. * @return void
  132. */
  133. #[\ReturnTypeWillChange]
  134. public function offsetUnset($name)
  135. {
  136. unset($this->children[$name]);
  137. }
  138. /**
  139. * Returns an iterator to iterate over children (implements \IteratorAggregate).
  140. *
  141. * @return \ArrayIterator<int|string, FormView>
  142. */
  143. #[\ReturnTypeWillChange]
  144. public function getIterator()
  145. {
  146. return new \ArrayIterator($this->children);
  147. }
  148. /**
  149. * Implements \Countable.
  150. *
  151. * @return int
  152. */
  153. #[\ReturnTypeWillChange]
  154. public function count()
  155. {
  156. return \count($this->children);
  157. }
  158. }