*/
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 null;
}
- throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, is_array($object) ? 'Array' : $object), -1, $this->getTemplateName());
+ 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());
+ }
}
$class = get_class($object);
// tests when input is not an array or object
$tests = array_merge($tests, array(
- array(false, null, 42, 'a', array(), $anyType, false, 'Item "a" for "42" does not exist'),
- array(false, null, "string", 'a', array(), $anyType, false, 'Item "a" for "string" does not exist'),
- array(false, null, array(), 'a', array(), $anyType, false, 'Item "a" for "Array" does not exist'),
+ array(false, null, 42, 'a', array(), $anyType, false, 'Impossible to access an item ("a") on a "integer" variable'),
+ array(false, null, "string", 'a', array(), $anyType, false, 'Impossible to access an item ("a") on a "string" variable'),
+ array(false, null, array(), 'a', array(), $anyType, false, 'Key "a" for array with keys "" does not exist'),
));
// add twig_template_get_attributes tests