TYPO3 CMS  TYPO3_7-6
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  protected static $reservedVariableNames = ['true', 'false', 'on', 'off', 'yes', 'no', '_all'];
31 
37  protected $variables = [];
38 
45  public function __construct(array $variableArray = [])
46  {
47  $this->variables = $variableArray;
48  }
49 
59  public function add($identifier, $value)
60  {
61  if (array_key_exists($identifier, $this->variables)) {
62  throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException('Duplicate variable declaration, "' . $identifier . '" already set!', 1224479063);
63  }
64  if (in_array(strtolower($identifier), self::$reservedVariableNames)) {
65  throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException('"' . $identifier . '" is a reserved variable name and cannot be used as variable identifier.', 1256730379);
66  }
67  $this->variables[$identifier] = $value;
68  }
69 
80  public function get($identifier)
81  {
82  if ($identifier === '_all') {
83  return $this->variables;
84  }
85  if (!array_key_exists($identifier, $this->variables)) {
86  throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException('Tried to get a variable "' . $identifier . '" which is not stored in the context!', 1224479370);
87  }
88  return $this->variables[$identifier];
89  }
90 
99  public function remove($identifier)
100  {
101  if (!array_key_exists($identifier, $this->variables)) {
102  throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException('Tried to remove a variable "' . $identifier . '" which is not stored in the context!', 1224479372);
103  }
104  unset($this->variables[$identifier]);
105  }
106 
112  public function getAllIdentifiers()
113  {
114  return array_keys($this->variables);
115  }
116 
122  public function getAll()
123  {
124  return $this->variables;
125  }
126 
134  public function exists($identifier)
135  {
136  if ($identifier === '_all') {
137  return true;
138  }
139 
140  return array_key_exists($identifier, $this->variables);
141  }
142 
148  public function __sleep()
149  {
150  return ['variables'];
151  }
152 
160  public function offsetSet($identifier, $value)
161  {
162  $this->add($identifier, $value);
163  }
164 
171  public function offsetUnset($identifier)
172  {
173  $this->remove($identifier);
174  }
175 
182  public function offsetExists($identifier)
183  {
184  return $this->exists($identifier);
185  }
186 
193  public function offsetGet($identifier)
194  {
195  return $this->get($identifier);
196  }
197 
205  public function getOrNull($variableName)
206  {
207  if ($variableName === '_all') {
208  return $this->variables;
209  }
210 
211  return isset($this->variables[$variableName]) ? $this->variables[$variableName] : null;
212  }
213 }