[TASK] Sync CMS Fluid with Flow Fluid 1.1 (part1)
[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 /**
15 * VariableContainer which stores template variables.
16 * Is used in two contexts:
17 *
18 * 1) Holds the current variables in the template
19 * 2) Holds variables being set during Parsing (set in view helpers implementing the PostParse facet)
20 *
21 * @api
22 */
23 class TemplateVariableContainer implements \ArrayAccess {
24
25 /**
26 * List of reserved words that can't be used as variable identifiers in Fluid templates
27 *
28 * @var array
29 */
30 static protected $reservedVariableNames = array('true', 'false', 'on', 'off', 'yes', 'no', '_all');
31
32 /**
33 * Variables stored in context
34 *
35 * @var array
36 */
37 protected $variables = array();
38
39 /**
40 * Constructor. Can take an array, and initializes the variables with it.
41 *
42 * @param array $variableArray
43 * @api
44 */
45 public function __construct(array $variableArray = array()) {
46 $this->variables = $variableArray;
47 }
48
49 /**
50 * Add a variable to the context
51 *
52 * @param string $identifier Identifier of the variable to add
53 * @param mixed $value The variable's value
54 * @return void
55 * @throws Exception\InvalidVariableException
56 * @api
57 */
58 public function add($identifier, $value) {
59 if (array_key_exists($identifier, $this->variables)) {
60 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException('Duplicate variable declaration, "' . $identifier . '" already set!', 1224479063);
61 }
62 if (in_array(strtolower($identifier), self::$reservedVariableNames)) {
63 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException('"' . $identifier . '" is a reserved variable name and cannot be used as variable identifier.', 1256730379);
64 }
65 $this->variables[$identifier] = $value;
66 }
67
68 /**
69 * Get a variable from the context. Throws exception if variable is not found in context.
70 *
71 * If "_all" is given as identifier, all variables are returned in an array.
72 *
73 * @param string $identifier
74 * @return mixed The variable value identified by $identifier
75 * @throws Exception\InvalidVariableException
76 * @api
77 */
78 public function get($identifier) {
79 if ($identifier === '_all') {
80 return $this->variables;
81 }
82 if (!array_key_exists($identifier, $this->variables)) {
83 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException('Tried to get a variable "' . $identifier . '" which is not stored in the context!', 1224479370);
84 }
85 return $this->variables[$identifier];
86 }
87
88 /**
89 * Remove a variable from context. Throws exception if variable is not found in context.
90 *
91 * @param string $identifier The identifier to remove
92 * @return void
93 * @throws Exception\InvalidVariableException
94 * @api
95 */
96 public function remove($identifier) {
97 if (!array_key_exists($identifier, $this->variables)) {
98 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException('Tried to remove a variable "' . $identifier . '" which is not stored in the context!', 1224479372);
99 }
100 unset($this->variables[$identifier]);
101 }
102
103 /**
104 * Returns an array of all identifiers available in the context.
105 *
106 * @return array Array of identifier strings
107 */
108 public function getAllIdentifiers() {
109 return array_keys($this->variables);
110 }
111
112 /**
113 * Returns the variables array.
114 *
115 * @return array Identifiers and values of all variables
116 */
117 public function getAll() {
118 return $this->variables;
119 }
120
121 /**
122 * Checks if this property exists in the VariableContainer.
123 *
124 * @param string $identifier
125 * @return boolean TRUE if $identifier exists, FALSE otherwise
126 * @api
127 */
128 public function exists($identifier) {
129 if ($identifier === '_all') {
130 return TRUE;
131 }
132
133 return array_key_exists($identifier, $this->variables);
134 }
135
136 /**
137 * Clean up for serializing.
138 *
139 * @return array
140 */
141 public function __sleep() {
142 return array('variables');
143 }
144
145 /**
146 * Adds a variable to the context.
147 *
148 * @param string $identifier Identifier of the variable to add
149 * @param mixed $value The variable's value
150 * @return void
151 */
152 public function offsetSet($identifier, $value) {
153 $this->add($identifier, $value);
154 }
155
156 /**
157 * Remove a variable from context. Throws exception if variable is not found in context.
158 *
159 * @param string $identifier The identifier to remove
160 * @return void
161 */
162 public function offsetUnset($identifier) {
163 $this->remove($identifier);
164 }
165
166 /**
167 * Checks if this property exists in the VariableContainer.
168 *
169 * @param string $identifier
170 * @return boolean TRUE if $identifier exists, FALSE otherwise
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 mixed The variable identified by $identifier
181 */
182 public function offsetGet($identifier) {
183 return $this->get($identifier);
184 }
185 }
186
187 ?>