TYPO3 API  SVNRelease
ObjectAccessorNode.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  * A node which handles object access. This means it handles structures like {object.accessor.bla}
00025  *
00026  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00027  */
00028 class Tx_Fluid_Core_Parser_SyntaxTree_ObjectAccessorNode extends Tx_Fluid_Core_Parser_SyntaxTree_AbstractNode {
00029 
00030     /**
00031      * Object path which will be called. Is a list like "post.name.email"
00032      * @var string
00033      */
00034     protected $objectPath;
00035 
00036     /**
00037      * Constructor. Takes an object path as input.
00038      *
00039      * The first part of the object path has to be a variable in the
00040      * TemplateVariableContainer.
00041      *
00042      * @param string $objectPath An Object Path, like object1.object2.object3
00043      * @author Sebastian Kurfürst <sebastian@typo3.org>
00044      */
00045     public function __construct($objectPath) {
00046         $this->objectPath = $objectPath;
00047     }
00048 
00049     /**
00050      * Evaluate this node and return the correct object.
00051      *
00052      * Handles each part (denoted by .) in $this->objectPath in the following order:
00053      * - call appropriate getter
00054      * - call public property, if exists
00055      * - fail
00056      *
00057      * The first part of the object path has to be a variable in the
00058      * TemplateVariableContainer.
00059      *
00060      * @param Tx_Fluid_Core_Rendering_RenderingContextInterface $renderingContext
00061      * @return object The evaluated object, can be any object type.
00062      * @author Sebastian Kurfürst <sebastian@typo3.org>
00063      * @author Bastian Waidelich <bastian@typo3.org>
00064      */
00065     public function evaluate(Tx_Fluid_Core_Rendering_RenderingContextInterface $renderingContext) {
00066         return $this->getPropertyPath($renderingContext->getTemplateVariableContainer(), $this->objectPath, $renderingContext);
00067     }
00068 
00069     /**
00070      * Gets a property path from a given object or array.
00071      *
00072      * If propertyPath is "bla.blubb", then we first call getProperty($object, 'bla'),
00073      * and on the resulting object we call getProperty(..., 'blubb').
00074      *
00075      * For arrays the keys are checked likewise.
00076      *
00077      * @param mixed $subject An object or array
00078      * @param string $propertyPath
00079      * @return mixed Value of the property
00080      */
00081     protected function getPropertyPath($subject, $propertyPath, Tx_Fluid_Core_Rendering_RenderingContextInterface $renderingContext) {
00082         $propertyPathSegments = explode('.', $propertyPath);
00083         foreach ($propertyPathSegments as $pathSegment) {
00084             if (is_object($subject) && Tx_Extbase_Reflection_ObjectAccess::isPropertyGettable($subject, $pathSegment)) {
00085                 $subject = Tx_Extbase_Reflection_ObjectAccess::getProperty($subject, $pathSegment);
00086             } elseif ((is_array($subject) || $subject instanceof ArrayAccess) && isset($subject[$pathSegment])) {
00087                 $subject = $subject[$pathSegment];
00088             } else {
00089                 return NULL;
00090             }
00091 
00092             if ($subject instanceof Tx_Fluid_Core_Parser_SyntaxTree_RenderingContextAwareInterface) {
00093                 $subject->setRenderingContext($renderingContext);
00094             }
00095         }
00096         return $subject;
00097     }
00098 
00099 
00100 }
00101 ?>