*
*
* ...
*
*
* Or to read names of variables which contain dynamic parts:
*
*
* {v:variable.get(name: 'dynamic{variableName}')}
*
* If your target object is an array with unsequential yet
* numeric indices (e.g. {123: 'value1', 513: 'value2'},
* commonly seen in reindexed UID map arrays) use
* `useRawIndex="TRUE"` to indicate you do not want your
* array/QueryResult/Iterator to be accessed by locating
* the Nth element - which is the default behavior.
*
* ```warning
* Do not try `useRawKeys="TRUE"` on QueryResult or
* ObjectStorage unless you are fully aware what you are
* doing. These particular types require an unpredictable
* index value - the SPL object hash value - when accessing
* members directly. This SPL indexing and the very common
* occurrences of QueryResult and ObjectStorage variables
* in templates is the very reason why `useRawKeys` by
* default is set to `FALSE`.
* ```
*
* @author Claus Due
* @package Vhs
* @subpackage ViewHelpers\Var
*/
class GetViewHelper extends AbstractViewHelper {
/**
* @param string $name
* @param boolean $useRawKeys
* @return mixed
*/
public function render($name, $useRawKeys = FALSE) {
if (FALSE === strpos($name, '.')) {
if (TRUE === $this->templateVariableContainer->exists($name)) {
return $this->templateVariableContainer->get($name);
}
} else {
$segments = explode('.', $name);
$lastSegment = array_shift($segments);
$templateVariableRootName = $lastSegment;
if (TRUE === $this->templateVariableContainer->exists($templateVariableRootName)) {
$templateVariableRoot = $this->templateVariableContainer->get($templateVariableRootName);
if (TRUE === $useRawKeys) {
return ObjectAccess::getPropertyPath($templateVariableRoot, implode('.', $segments));
}
try {
$value = $templateVariableRoot;
foreach ($segments as $segment) {
if (TRUE === ctype_digit($segment)) {
$segment = intval($segment);
$index = 0;
// Note: this loop approach is not a stupid solution. If you doubt this,
// attempt to feth a number at a numeric index from ObjectStorage ;)
foreach ($value as $possibleValue) {
if ($index === $segment) {
$value = $possibleValue;
break;
}
++ $index;
}
continue;
}
$value = ObjectAccess::getProperty($value, $segment);
}
return $value;
} catch (\Exception $e) {
return NULL;
}
}
}
return NULL;
}
}