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