TYPO3 CMS  TYPO3_6-2
TemplateVariableContainer.php
Go to the documentation of this file.
1 <?php
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 
23 class TemplateVariableContainer implements \ArrayAccess {
24 
30  static protected $reservedVariableNames = array('true', 'false', 'on', 'off', 'yes', 'no', '_all');
31 
37  protected $variables = array();
38 
45  public function __construct(array $variableArray = array()) {
46  $this->variables = $variableArray;
47  }
48 
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 
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 
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 
108  public function getAllIdentifiers() {
109  return array_keys($this->variables);
110  }
111 
117  public function getAll() {
118  return $this->variables;
119  }
120 
128  public function exists($identifier) {
129  if ($identifier === '_all') {
130  return TRUE;
131  }
132 
133  return array_key_exists($identifier, $this->variables);
134  }
135 
141  public function __sleep() {
142  return array('variables');
143  }
144 
152  public function offsetSet($identifier, $value) {
153  $this->add($identifier, $value);
154  }
155 
162  public function offsetUnset($identifier) {
163  $this->remove($identifier);
164  }
165 
172  public function offsetExists($identifier) {
173  return $this->exists($identifier);
174  }
175 
182  public function offsetGet($identifier) {
183  return $this->get($identifier);
184  }
185 }