2438f9c2dcd95b0b1d21249fef632eb1a015cec9
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / Core / ViewHelper / TemplateVariableContainer.php
1 <?php
2
3 /* *
4 * This script belongs to the FLOW3 package "Fluid". *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU Lesser General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or (at your *
9 * option) any later version. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
14 * General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with the script. *
18 * If not, see http://www.gnu.org/licenses/lgpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 /**
24 * VariableContainer which stores template variables.
25 * Is used in two contexts:
26 *
27 * 1) Holds the current variables in the template
28 * 2) Holds variables being set during Parsing (set in view helpers implementing the PostParse facet)
29 *
30 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
31 * @api
32 */
33 class Tx_Fluid_Core_ViewHelper_TemplateVariableContainer implements ArrayAccess {
34
35 /**
36 * List of reserved words that can't be used as variable identifiers in Fluid templates
37 * @var array
38 */
39 static protected $reservedVariableNames = array('true', 'false', 'on', 'off', 'yes', 'no');
40
41 /**
42 * Variables stored in context
43 * @var array
44 */
45 protected $variables = array();
46
47 /**
48 * Constructor. Can take an array, and initializes the variables with it.
49 *
50 * @param array $variableArray
51 * @author Sebastian Kurfürst <sebastian@typo3.org>
52 * @api
53 */
54 public function __construct(array $variableArray = array()) {
55 $this->variables = $variableArray;
56 }
57
58 /**
59 * Add a variable to the context
60 *
61 * @param string $identifier Identifier of the variable to add
62 * @param mixed $value The variable's value
63 * @return void
64 * @author Sebastian Kurfürst <sebastian@typo3.org>
65 * @author Bastian Waidelich <bastian@typo3.org>
66 * @api
67 */
68 public function add($identifier, $value) {
69 if (array_key_exists($identifier, $this->variables)) throw new Tx_Fluid_Core_ViewHelper_Exception_InvalidVariableException('Duplicate variable declarations!', 1224479063);
70 if (in_array(strtolower($identifier), self::$reservedVariableNames)) throw new Tx_Fluid_Core_ViewHelper_Exception_InvalidVariableException('"' . $identifier . '" is a reserved variable name and can\'t be used as variable identifier.', 1256730379);
71 $this->variables[$identifier] = $value;
72 }
73
74 /**
75 * Get a variable from the context. Throws exception if variable is not found in context.
76 *
77 * @param string $identifier
78 * @return variable The variable identified by $identifier
79 * @author Sebastian Kurfürst <sebastian@typo3.org>
80 * @api
81 */
82 public function get($identifier) {
83 if (!array_key_exists($identifier, $this->variables)) throw new Tx_Fluid_Core_ViewHelper_Exception_InvalidVariableException('Tried to get a variable "' . $identifier . '" which is not stored in the context!', 1224479370);
84 return $this->variables[$identifier];
85 }
86
87 /**
88 * Remove a variable from context. Throws exception if variable is not found in context.
89 *
90 * @param string $identifier The identifier to remove
91 * @return void
92 * @author Sebastian Kurfürst <sebastian@typo3.org>
93 * @api
94 */
95 public function remove($identifier) {
96 if (!array_key_exists($identifier, $this->variables)) throw new Tx_Fluid_Core_ViewHelper_Exception_InvalidVariableException('Tried to remove a variable "' . $identifier . '" which is not stored in the context!', 1224479372);
97 unset($this->variables[$identifier]);
98 }
99
100 /**
101 * Returns an array of all identifiers available in the context.
102 *
103 * @return array Array of identifier strings
104 * @author Sebastian Kurfürst <sebastian@typo3.org>
105 */
106 public function getAllIdentifiers() {
107 return array_keys($this->variables);
108 }
109
110 /**
111 * Returns the variables array.
112 *
113 * @return array Identifiers and values of all variables
114 * @author Robert Lemke <robert@typo3.org>
115 */
116 public function getAll() {
117 return $this->variables;
118 }
119
120 /**
121 * Checks if this property exists in the VariableContainer.
122 *
123 * @param string $identifier
124 * @return boolean TRUE if $identifier exists, FALSE otherwise
125 * @author Sebastian Kurfürst <sebastian@typo3.org>
126 * @api
127 */
128 public function exists($identifier) {
129 return array_key_exists($identifier, $this->variables);
130 }
131
132 /**
133 * Clean up for serializing.
134 *
135 * @return array
136 * @author Sebastian Kurfürst <sebastian@typo3.org>
137 */
138 public function __sleep() {
139 return array('variables');
140 }
141
142 /**
143 * Adds a variable to the context.
144 *
145 * @param string $identifier Identifier of the variable to add
146 * @param mixed $value The variable's value
147 * @return void
148 * @author Sebastian Kurfürst <sebastian@typo3.org>
149 */
150 public function offsetSet($identifier, $value) {
151 return $this->add($identifier, $value);
152 }
153
154 /**
155 * Remove a variable from context. Throws exception if variable is not found in context.
156 *
157 * @param string $identifier The identifier to remove
158 * @return void
159 * @author Sebastian Kurfürst <sebastian@typo3.org>
160 */
161 public function offsetUnset($identifier) {
162 return $this->remove($identifier);
163 }
164
165 /**
166 * Checks if this property exists in the VariableContainer.
167 *
168 * @param string $identifier
169 * @return boolean TRUE if $identifier exists, FALSE otherwise
170 * @author Sebastian Kurfürst <sebastian@typo3.org>
171 */
172 public function offsetExists($identifier) {
173 return $this->exists($identifier);
174 }
175
176 /**
177 * Get a variable from the context. Throws exception if variable is not found in context.
178 *
179 * @param string $identifier
180 * @return variable The variable identified by $identifier
181 * @author Sebastian Kurfürst <sebastian@typo3.org>
182 */
183 public function offsetGet($identifier) {
184 return $this->get($identifier);
185 }
186 }
187 ?>