FormView.php 3.5 KB

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