TYPO3 API  SVNRelease
TemplateVariableContainer.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*                                                                        *
00004  * This script belongs to the FLOW3 package "Fluid".                      *
00005  *                                                                        *
00006  * It is free software; you can redistribute it and/or modify it under    *
00007  * the terms of the GNU Lesser General Public License as published by the *
00008  * Free Software Foundation, either version 3 of the License, or (at your *
00009  * option) any later version.                                             *
00010  *                                                                        *
00011  * This script is distributed in the hope that it will be useful, but     *
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00013  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
00014  * General Public License for more details.                               *
00015  *                                                                        *
00016  * You should have received a copy of the GNU Lesser General Public       *
00017  * License along with the script.                                         *
00018  * If not, see http://www.gnu.org/licenses/lgpl.html                      *
00019  *                                                                        *
00020  * The TYPO3 project - inspiring people to share!                         *
00021  *                                                                        */
00022 
00023 /**
00024  * VariableContainer which stores template variables.
00025  * Is used in two contexts:
00026  *
00027  * 1) Holds the current variables in the template
00028  * 2) Holds variables being set during Parsing (set in view helpers implementing the PostParse facet)
00029  *
00030  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00031  * @api
00032  */
00033 class Tx_Fluid_Core_ViewHelper_TemplateVariableContainer implements ArrayAccess {
00034 
00035     /**
00036      * List of reserved words that can't be used as variable identifiers in Fluid templates
00037      * @var array
00038      */
00039     static protected $reservedVariableNames = array('true', 'false', 'on', 'off', 'yes', 'no');
00040 
00041     /**
00042      * Variables stored in context
00043      * @var array
00044      */
00045     protected $variables = array();
00046 
00047     /**
00048      * Constructor. Can take an array, and initializes the variables with it.
00049      *
00050      * @param array $variableArray
00051      * @author Sebastian Kurfürst <sebastian@typo3.org>
00052      * @api
00053      */
00054     public function __construct(array $variableArray = array()) {
00055         $this->variables = $variableArray;
00056     }
00057 
00058     /**
00059      * Add a variable to the context
00060      *
00061      * @param string $identifier Identifier of the variable to add
00062      * @param mixed $value The variable's value
00063      * @return void
00064      * @author Sebastian Kurfürst <sebastian@typo3.org>
00065      * @author Bastian Waidelich <bastian@typo3.org>
00066      * @api
00067      */
00068     public function add($identifier, $value) {
00069         if (array_key_exists($identifier, $this->variables)) throw new Tx_Fluid_Core_ViewHelper_Exception_InvalidVariableException('Duplicate variable declarations!', 1224479063);
00070         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);
00071         $this->variables[$identifier] = $value;
00072     }
00073 
00074     /**
00075      * Get a variable from the context. Throws exception if variable is not found in context.
00076      *
00077      * @param string $identifier
00078      * @return variable The variable identified by $identifier
00079      * @author Sebastian Kurfürst <sebastian@typo3.org>
00080      * @api
00081      */
00082     public function get($identifier) {
00083         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);
00084         return $this->variables[$identifier];
00085     }
00086 
00087     /**
00088      * Remove a variable from context. Throws exception if variable is not found in context.
00089      *
00090      * @param string $identifier The identifier to remove
00091      * @return void
00092      * @author Sebastian Kurfürst <sebastian@typo3.org>
00093      * @api
00094      */
00095     public function remove($identifier) {
00096         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);
00097         unset($this->variables[$identifier]);
00098     }
00099 
00100     /**
00101      * Returns an array of all identifiers available in the context.
00102      *
00103      * @return array Array of identifier strings
00104      * @author Sebastian Kurfürst <sebastian@typo3.org>
00105      */
00106     public function getAllIdentifiers() {
00107         return array_keys($this->variables);
00108     }
00109 
00110     /**
00111      * Returns the variables array.
00112      *
00113      * @return array Identifiers and values of all variables
00114      * @author Robert Lemke <robert@typo3.org>
00115      */
00116     public function getAll() {
00117         return $this->variables;
00118     }
00119 
00120     /**
00121      * Checks if this property exists in the VariableContainer.
00122      *
00123      * @param string $identifier
00124      * @return boolean TRUE if $identifier exists, FALSE otherwise
00125      * @author Sebastian Kurfürst <sebastian@typo3.org>
00126      * @api
00127      */
00128     public function exists($identifier) {
00129         return array_key_exists($identifier, $this->variables);
00130     }
00131 
00132     /**
00133      * Clean up for serializing.
00134      *
00135      * @return array
00136      * @author Sebastian Kurfürst <sebastian@typo3.org>
00137      */
00138     public function __sleep() {
00139         return array('variables');
00140     }
00141 
00142     /**
00143      * Adds a variable to the context.
00144      *
00145      * @param string $identifier Identifier of the variable to add
00146      * @param mixed $value The variable's value
00147      * @return void
00148      * @author Sebastian Kurfürst <sebastian@typo3.org>
00149      */
00150     public function offsetSet($identifier, $value) {
00151         return $this->add($identifier, $value);
00152     }
00153 
00154     /**
00155      * Remove a variable from context. Throws exception if variable is not found in context.
00156      *
00157      * @param string $identifier The identifier to remove
00158      * @return void
00159      * @author Sebastian Kurfürst <sebastian@typo3.org>
00160      */
00161     public function offsetUnset($identifier) {
00162         return $this->remove($identifier);
00163     }
00164 
00165     /**
00166      * Checks if this property exists in the VariableContainer.
00167      *
00168      * @param string $identifier
00169      * @return boolean TRUE if $identifier exists, FALSE otherwise
00170      * @author Sebastian Kurfürst <sebastian@typo3.org>
00171      */
00172     public function offsetExists($identifier) {
00173         return $this->exists($identifier);
00174     }
00175 
00176     /**
00177      * Get a variable from the context. Throws exception if variable is not found in context.
00178      *
00179      * @param string $identifier
00180      * @return variable The variable identified by $identifier
00181      * @author Sebastian Kurfürst <sebastian@typo3.org>
00182      */
00183     public function offsetGet($identifier) {
00184         return $this->get($identifier);
00185     }
00186 }
00187 ?>