Returns the attribute value for a given array/object.
mixed $object The object or array from where to get the item:
mixed $item The item to get from the array or object:
array $arguments An array of arguments to pass if the item is an object method:
string $type The type of attribute (@see Twig_TemplateInterface):
Boolean $isDefinedTest Whether this is only a defined check:
Boolean $ignoreStrictCheck Whether to ignore the strict attribute check or not:
mixed The attribute value, or a Boolean when $isDefinedTest is true, or null when the attribute is not set and $ignoreStrictCheck is true
Twig_Error_Runtime if the attribute does not exist and Twig is running in strict mode and $isDefinedTest is false
protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false) {
$item = ctype_digit((string) $item) ? (int) $item : (string) $item;
// array
if (Twig_TemplateInterface::METHOD_CALL !== $type) {
if (is_array($object) && array_key_exists($item, $object) || $object instanceof ArrayAccess && isset($object[$item])) {
if ($isDefinedTest) {
return true;
}
return $object[$item];
}
if (Twig_TemplateInterface::ARRAY_CALL === $type) {
if ($isDefinedTest) {
return false;
}
if ($ignoreStrictCheck || !$this->env
->isStrictVariables()) {
return null;
}
if (is_object($object)) {
throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)));
}
elseif (is_array($object)) {
throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))));
}
else {
throw new Twig_Error_Runtime(sprintf('Impossible to access a key ("%s") on a "%s" variable', $item, gettype($object)));
}
}
}
if (!is_object($object)) {
if ($isDefinedTest) {
return false;
}
if ($ignoreStrictCheck || !$this->env
->isStrictVariables()) {
return null;
}
throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, is_array($object) ? 'Array' : $object));
}
$class = get_class($object);
// object property
if (Twig_TemplateInterface::METHOD_CALL !== $type) {
if (isset($object->{$item}) || array_key_exists($item, $object)) {
if ($isDefinedTest) {
return true;
}
if ($this->env
->hasExtension('sandbox')) {
$this->env
->getExtension('sandbox')
->checkPropertyAllowed($object, $item);
}
return $object->{$item};
}
}
// object method
if (!isset(self::$cache[$class]['methods'])) {
self::$cache[$class]['methods'] = array_change_key_case(array_flip(get_class_methods($object)));
}
$lcItem = strtolower($item);
if (isset(self::$cache[$class]['methods'][$lcItem])) {
$method = $item;
}
elseif (isset(self::$cache[$class]['methods']['get' . $lcItem])) {
$method = 'get' . $item;
}
elseif (isset(self::$cache[$class]['methods']['is' . $lcItem])) {
$method = 'is' . $item;
}
elseif (isset(self::$cache[$class]['methods']['__call'])) {
$method = $item;
}
else {
if ($isDefinedTest) {
return false;
}
if ($ignoreStrictCheck || !$this->env
->isStrictVariables()) {
return null;
}
throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)));
}
if ($isDefinedTest) {
return true;
}
if ($this->env
->hasExtension('sandbox')) {
$this->env
->getExtension('sandbox')
->checkMethodAllowed($object, $method);
}
$ret = call_user_func_array(array(
$object,
$method,
), $arguments);
// hack to be removed when macro calls are refactored
if ($object instanceof Twig_TemplateInterface) {
return $ret === '' ? '' : new Twig_Markup($ret, $this->env
->getCharset());
}
return $ret;
}