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