0b4eeb84e7de7df698a0d03d57a46563900ab5ee
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / Core / Parser / SyntaxTree / ObjectAccessorNode.php
1 <?php
2
3 /* *
4 * This script belongs to the FLOW3 package "Fluid". *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU Lesser General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or (at your *
9 * option) any later version. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
14 * General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with the script. *
18 * If not, see http://www.gnu.org/licenses/lgpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 /**
24 * A node which handles object access. This means it handles structures like {object.accessor.bla}
25 *
26 * @version $Id: ObjectAccessorNode.php 3333 2009-10-21 09:52:46Z sebastian $
27 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
28 * @scope prototype
29 * @internal
30 */
31 class Tx_Fluid_Core_Parser_SyntaxTree_ObjectAccessorNode extends Tx_Fluid_Core_Parser_SyntaxTree_AbstractNode {
32
33 /**
34 * Object path which will be called. Is a list like "post.name.email"
35 * @var string
36 */
37 protected $objectPath;
38
39 /**
40 * Constructor. Takes an object path as input.
41 *
42 * The first part of the object path has to be a variable in the TemplateVariableContainer.
43 * For the further parts, it is checked if the object has a getObjectname method. If yes, this is called.
44 * If no, it is checked if a property "objectname" exists.
45 * If no, an error is thrown.
46 *
47 * @param string $objectPath An Object Path, like object1.object2.object3
48 * @author Sebastian Kurfürst <sebastian@typo3.org>
49 */
50 public function __construct($objectPath) {
51 $this->objectPath = $objectPath;
52 }
53
54 /**
55 * Evaluate this node and return the correct object.
56 *
57 * Handles each part (denoted by .) in $this->objectPath in the following order:
58 * - call appropriate getter
59 * - call public property, if exists
60 * - fail
61 *
62 * @return object The evaluated object, can be any object type.
63 * @author Sebastian Kurfürst <sebastian@typo3.org>
64 * @author Bastian Waidelich <bastian@typo3.org>
65 * @todo Depending on the context, either fail or not!!!
66 */
67 public function evaluate() {
68 $objectPathParts = explode('.', $this->objectPath);
69 $variableName = array_shift($objectPathParts);
70 if (!$this->renderingContext->getTemplateVariableContainer()->exists($variableName)) {
71 return NULL;
72 }
73 $currentObject = $this->renderingContext->getTemplateVariableContainer()->get($variableName);
74 if (count($objectPathParts) > 0) {
75 return Tx_Extbase_Reflection_ObjectAccess::getPropertyPath($currentObject, implode('.', $objectPathParts));
76 } else {
77 return $currentObject;
78 }
79 }
80 }
81 ?>