TYPO3 API  SVNRelease
ViewHelperVariableContainer.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  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00025  * @api
00026  */
00027 class Tx_Fluid_Core_ViewHelper_ViewHelperVariableContainer {
00028 
00029     /**
00030      * Two-dimensional object array storing the values. The first dimension is the fully qualified ViewHelper name,
00031      * and the second dimension is the identifier for the data the ViewHelper wants to store.
00032      * @var array
00033      */
00034     protected $objects = array();
00035 
00036     /**
00037      *
00038      * @var Tx_Fluid_View_AbstractTemplateView
00039      */
00040     protected $view;
00041 
00042     /**
00043      * Add a variable to the Variable Container. Make sure that $viewHelperName is ALWAYS set
00044      * to your fully qualified ViewHelper Class Name
00045      *
00046      * In case the value is already inside, an exception is thrown.
00047      *
00048      * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like Tx_Fluid_ViewHelpers_ForViewHelper)
00049      * @param string $key Key of the data
00050      * @param object $value The value to store
00051      * @return void
00052      * @throws Tx_Fluid_Core_ViewHelper_Exception_InvalidVariableException if there was no key with the specified name
00053      * @author Sebastian Kurfürst <sebastian@typo3.org>
00054      * @api
00055      */
00056     public function add($viewHelperName, $key, $value) {
00057         if ($this->exists($viewHelperName, $key)) throw new Tx_Fluid_Core_ViewHelper_Exception_InvalidVariableException('The key "' . $viewHelperName . '->' . $key . '" was already stored and you cannot override it.', 1243352010);
00058         $this->addOrUpdate($viewHelperName, $key, $value);
00059     }
00060 
00061     /**
00062      * Add a variable to the Variable Container. Make sure that $viewHelperName is ALWAYS set
00063      * to your fully qualified ViewHelper Class Name.
00064      * In case the value is already inside, it is silently overridden.
00065      *
00066      * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like Tx_Fluid_ViewHelpers_ForViewHelper)
00067      * @param string $key Key of the data
00068      * @param object $value The value to store
00069      * @return void
00070      * @author Sebastian Kurfürst <sebastian@typo3.org>
00071      */
00072     public function addOrUpdate($viewHelperName, $key, $value) {
00073         if (!isset($this->objects[$viewHelperName])) {
00074             $this->objects[$viewHelperName] = array();
00075         }
00076         $this->objects[$viewHelperName][$key] = $value;
00077     }
00078 
00079     /**
00080      * Gets a variable which is stored
00081      *
00082      * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like Tx_Fluid_ViewHelpers_ForViewHelper)
00083      * @param string $key Key of the data
00084      * @return object The object stored
00085      * @throws Tx_Fluid_Core_ViewHelper_Exception_InvalidVariableException if there was no key with the specified name
00086      * @author Sebastian Kurfürst <sebastian@typo3.org>
00087      * @api
00088      */
00089     public function get($viewHelperName, $key) {
00090         if (!$this->exists($viewHelperName, $key)) throw new Tx_Fluid_Core_ViewHelper_Exception_InvalidVariableException('No value found for key "' . $viewHelperName . '->' . $key . '"', 1243325768);
00091         return $this->objects[$viewHelperName][$key];
00092     }
00093 
00094     /**
00095      * Determine whether there is a variable stored for the given key
00096      *
00097      * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like Tx_Fluid_ViewHelpers_ForViewHelper)
00098      * @param string $key Key of the data
00099      * @return boolean TRUE if a value for the given ViewHelperName / Key is stored, FALSE otherwise.
00100      * @author Sebastian Kurfürst <sebastian@typo3.org>
00101      * @api
00102      */
00103     public function exists($viewHelperName, $key) {
00104         return isset($this->objects[$viewHelperName][$key]);
00105     }
00106 
00107     /**
00108      * Remove a value from the variable container
00109      *
00110      * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like Tx_Fluid_ViewHelpers_ForViewHelper)
00111      * @param string $key Key of the data to remove
00112      * @return void
00113      * @throws Tx_Fluid_Core_ViewHelper_Exception_InvalidVariableException if there was no key with the specified name
00114      * @author Sebastian Kurfürst <sebastian@typo3.org>
00115      * @api
00116      */
00117     public function remove($viewHelperName, $key) {
00118         if (!$this->exists($viewHelperName, $key)) throw new Tx_Fluid_Core_ViewHelper_Exception_InvalidVariableException('No value found for key "' . $viewHelperName . '->' . $key . '", thus the key cannot be removed.', 1243352249);
00119         unset($this->objects[$viewHelperName][$key]);
00120     }
00121 
00122     /**
00123      * Set the view to pass it to ViewHelpers.
00124      *
00125      * @param Tx_Fluid_View_AbstractTemplateView $view View to set
00126      * @return void
00127      * @author Sebastian Kurfürst <sebastian@typo3.org>
00128      */
00129     public function setView(Tx_Fluid_View_AbstractTemplateView $view) {
00130         $this->view = $view;
00131     }
00132 
00133     /**
00134      * Get the view.
00135      *
00136      * !!! This is NOT a public API and might still change!!!
00137      *
00138      * @return Tx_Fluid_View_AbstractTemplateView The View
00139      * @author Sebastian Kurfürst <sebastian@typo3.org>
00140      */
00141     public function getView() {
00142         return $this->view;
00143     }
00144 
00145     /**
00146      * Clean up for serializing.
00147      *
00148      * @return array
00149      * @author Sebastian Kurfürst <sebastian@typo3.org>
00150      */
00151     public function __sleep() {
00152         return array('objects');
00153     }
00154 }
00155 ?>