1<?php 2namespace TYPO3Fluid\Fluid\Core\Parser\SyntaxTree; 3 4/* 5 * This file belongs to the package "TYPO3 Fluid". 6 * See LICENSE.txt that was shipped with this package. 7 */ 8 9use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; 10use TYPO3Fluid\Fluid\Core\Variables\VariableExtractor; 11 12/** 13 * A node which handles object access. This means it handles structures like {object.accessor.bla} 14 */ 15class ObjectAccessorNode extends AbstractNode 16{ 17 18 /** 19 * Object path which will be called. Is a list like "post.name.email" 20 * 21 * @var string 22 */ 23 protected $objectPath; 24 25 /** 26 * Accessor names, one per segment in the object path. Use constants from VariableExtractor 27 * 28 * @var array 29 */ 30 protected $accessors = []; 31 32 /** 33 * Constructor. Takes an object path as input. 34 * 35 * The first part of the object path has to be a variable in the 36 * VariableProvider. 37 * 38 * @param string $objectPath An Object Path, like object1.object2.object3 39 * @param array $accessors Optional list of accessor strategies; starting from beginning of dotted path. Incomplete allowed. 40 */ 41 public function __construct($objectPath, array $accessors = []) 42 { 43 $this->objectPath = $objectPath; 44 $this->accessors = $accessors; 45 } 46 47 48 /** 49 * Internally used for building up cached templates; do not use directly! 50 * 51 * @return string 52 */ 53 public function getObjectPath() 54 { 55 return $this->objectPath; 56 } 57 58 /** 59 * @return array 60 */ 61 public function getAccessors() 62 { 63 return $this->accessors; 64 } 65 66 /** 67 * Evaluate this node and return the correct object. 68 * 69 * Handles each part (denoted by .) in $this->objectPath in the following order: 70 * - call appropriate getter 71 * - call public property, if exists 72 * - fail 73 * 74 * The first part of the object path has to be a variable in the 75 * VariableProvider. 76 * 77 * @param RenderingContextInterface $renderingContext 78 * @return mixed The evaluated object, can be any object type. 79 */ 80 public function evaluate(RenderingContextInterface $renderingContext) 81 { 82 $objectPath = strtolower($this->objectPath); 83 $variableProvider = $renderingContext->getVariableProvider(); 84 if ($objectPath === '_all') { 85 return $variableProvider->getAll(); 86 } 87 return VariableExtractor::extract($variableProvider, $this->objectPath, $this->accessors); 88 } 89} 90