FormView.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. class FormView implements \ArrayAccess, \IteratorAggregate, \Countable
  12. {
  13. private $vars = array(
  14. 'value' => null,
  15. 'attr' => array(),
  16. );
  17. private $parent;
  18. private $children = array();
  19. /**
  20. * Is the form attached to this renderer rendered?
  21. *
  22. * Rendering happens when either the widget or the row method was called.
  23. * Row implicitly includes widget, however certain rendering mechanisms
  24. * have to skip widget rendering when a row is rendered.
  25. *
  26. * @var Boolean
  27. */
  28. private $rendered = false;
  29. /**
  30. * @param string $name
  31. * @param mixed $value
  32. *
  33. * @return FormView The current view
  34. */
  35. public function set($name, $value)
  36. {
  37. $this->vars[$name] = $value;
  38. return $this;
  39. }
  40. /**
  41. * @param $name
  42. * @return Boolean
  43. */
  44. public function has($name)
  45. {
  46. return array_key_exists($name, $this->vars);
  47. }
  48. /**
  49. * @param $name
  50. * @param $default
  51. *
  52. * @return mixed
  53. */
  54. public function get($name, $default = null)
  55. {
  56. if (false === $this->has($name)) {
  57. return $default;
  58. }
  59. return $this->vars[$name];
  60. }
  61. /**
  62. * @return array
  63. */
  64. public function all()
  65. {
  66. return $this->vars;
  67. }
  68. /**
  69. * Alias of all so it is possible to do `form.vars.foo`
  70. *
  71. * @return array
  72. */
  73. public function getVars()
  74. {
  75. return $this->all();
  76. }
  77. /**
  78. * Sets the value for an attribute.
  79. *
  80. * @param string $name The name of the attribute
  81. * @param string $value The value
  82. *
  83. * @return FormView The current view
  84. */
  85. public function setAttribute($name, $value)
  86. {
  87. $this->vars['attr'][$name] = $value;
  88. return $this;
  89. }
  90. /**
  91. * Returns whether the attached form is rendered.
  92. *
  93. * @return Boolean Whether the form is rendered
  94. */
  95. public function isRendered()
  96. {
  97. $hasChildren = 0 < count($this->children);
  98. if (true === $this->rendered || !$hasChildren) {
  99. return $this->rendered;
  100. }
  101. if ($hasChildren) {
  102. foreach ($this->children as $child) {
  103. if (!$child->isRendered()) {
  104. return false;
  105. }
  106. }
  107. return $this->rendered = true;
  108. }
  109. return false;
  110. }
  111. /**
  112. * Marks the attached form as rendered
  113. *
  114. * @return FormView The current view
  115. */
  116. public function setRendered()
  117. {
  118. $this->rendered = true;
  119. return $this;
  120. }
  121. /**
  122. * Sets the parent view.
  123. *
  124. * @param FormView $parent The parent view
  125. *
  126. * @return FormView The current view
  127. */
  128. public function setParent(FormView $parent = null)
  129. {
  130. $this->parent = $parent;
  131. return $this;
  132. }
  133. /**
  134. * Returns the parent view.
  135. *
  136. * @return FormView The parent view
  137. */
  138. public function getParent()
  139. {
  140. return $this->parent;
  141. }
  142. /**
  143. * Returns whether this view has a parent.
  144. *
  145. * @return Boolean Whether this view has a parent
  146. */
  147. public function hasParent()
  148. {
  149. return null !== $this->parent;
  150. }
  151. /**
  152. * Sets the children view.
  153. *
  154. * @param array $children The children as instances of FormView
  155. *
  156. * @return FormView The current view
  157. */
  158. public function setChildren(array $children)
  159. {
  160. $this->children = $children;
  161. return $this;
  162. }
  163. /**
  164. * Returns the children.
  165. *
  166. * @return array The children as instances of FormView
  167. */
  168. public function getChildren()
  169. {
  170. return $this->children;
  171. }
  172. /**
  173. * Returns a given child.
  174. *
  175. * @param string name The name of the child
  176. *
  177. * @return FormView The child view
  178. */
  179. public function getChild($name)
  180. {
  181. return $this->children[$name];
  182. }
  183. /**
  184. * Returns whether this view has children.
  185. *
  186. * @return Boolean Whether this view has children
  187. */
  188. public function hasChildren()
  189. {
  190. return count($this->children) > 0;
  191. }
  192. /**
  193. * Returns a child by name (implements \ArrayAccess).
  194. *
  195. * @param string $name The child name
  196. *
  197. * @return FormView The child view
  198. */
  199. public function offsetGet($name)
  200. {
  201. return $this->getChild($name);
  202. }
  203. /**
  204. * Returns whether the given child exists (implements \ArrayAccess).
  205. *
  206. * @param string $name The child name
  207. *
  208. * @return Boolean Whether the child view exists
  209. */
  210. public function offsetExists($name)
  211. {
  212. return isset($this->children[$name]);
  213. }
  214. /**
  215. * Implements \ArrayAccess.
  216. *
  217. * @throws \BadMethodCallException always as setting a child by name is not allowed
  218. */
  219. public function offsetSet($name, $value)
  220. {
  221. throw new \BadMethodCallException('Not supported');
  222. }
  223. /**
  224. * Removes a child (implements \ArrayAccess).
  225. *
  226. * @param string $name The child name
  227. */
  228. public function offsetUnset($name)
  229. {
  230. unset($this->children[$name]);
  231. }
  232. /**
  233. * Returns an iterator to iterate over children (implements \IteratorAggregate)
  234. *
  235. * @return \ArrayIterator The iterator
  236. */
  237. public function getIterator()
  238. {
  239. return new \ArrayIterator($this->children);
  240. }
  241. /**
  242. * Implements \Countable.
  243. *
  244. * @return integer The number of children views
  245. */
  246. public function count()
  247. {
  248. return count($this->children);
  249. }
  250. }