*/
protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false)
{
- $arrayItem = is_bool($item) || is_float($item) ? (int) $item : $item;
-
// array
if (Twig_TemplateInterface::METHOD_CALL !== $type) {
+ $arrayItem = is_bool($item) || is_float($item) ? (int) $item : $item;
+
if ((is_array($object) && array_key_exists($arrayItem, $object))
|| ($object instanceof ArrayAccess && isset($object[$arrayItem]))
) {
return $object[$arrayItem];
}
- if (Twig_TemplateInterface::ARRAY_CALL === $type) {
+ if (Twig_TemplateInterface::ARRAY_CALL === $type || !is_object($object)) {
if ($isDefinedTest) {
return false;
}
} elseif (is_array($object)) {
throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $arrayItem, implode(', ', array_keys($object))), -1, $this->getTemplateName());
} else {
- throw new Twig_Error_Runtime(sprintf('Impossible to access a key ("%s") on a "%s" variable', $arrayItem, gettype($object)), -1, $this->getTemplateName());
+ throw new Twig_Error_Runtime(sprintf('Impossible to access an item ("%s") on a "%s" variable', $item, gettype($object)), -1, $this->getTemplateName());
}
}
}
return null;
}
- if (Twig_TemplateInterface::METHOD_CALL === $type) {
- throw new Twig_Error_Runtime(sprintf('Impossible to invoke a method ("%s") on a "%s" variable', $item, gettype($object)), -1, $this->getTemplateName());
- } elseif (is_array($object)) {
- throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $arrayItem, implode(', ', array_keys($object))), -1, $this->getTemplateName());
- } else {
- throw new Twig_Error_Runtime(sprintf('Impossible to access an item ("%s") on a "%s" variable', $item, gettype($object)), -1, $this->getTemplateName());
- }
+ throw new Twig_Error_Runtime(sprintf('Impossible to invoke a method ("%s") on a "%s" variable', $item, gettype($object)), -1, $this->getTemplateName());
}
$class = get_class($object);
public function getAttributeExceptions()
{
$tests = array(
- array('{{ string["a"] }}', 'Impossible to access a key ("a") on a "string" variable in "%s" at line 1', false),
+ array('{{ string["a"] }}', 'Impossible to access an item ("a") on a "string" variable in "%s" at line 1', false),
array('{{ array["a"] }}', 'Key "a" for array with keys "foo" does not exist in "%s" at line 1', false),
array('{{ array_access["a"] }}', 'Key "a" in object (with ArrayAccess) of type "Twig_TemplateArrayAccessObject" does not exist in "%s" at line 1', false),
- array('{{ string.a }}', 'Item "a" for "foo" does not exist in "%s" at line 1', false),
- array('{{ array.a }}', 'Item "a" for "Array" does not exist in "%s" at line 1', false),
+ array('{{ string.a }}', 'Impossible to access an item ("a") on a "string" variable in "%s" at line 1', false),
+ array('{{ array.a }}', 'Key "a" for array with keys "foo" does not exist in "%s" at line 1', false),
array('{{ array_access.a }}', 'Method "a" for object "Twig_TemplateArrayAccessObject" does not exist in "%s" at line 1', false),
array('{% macro foo(obj) %}{{ obj.missing_method() }}{% endmacro %}{{ _self.foo(array_access) }}', 'Method "missing_method" for object "Twig_TemplateArrayAccessObject" does not exist in "%s" at line 1', false),
);