From: Tobias Schultze Date: Wed, 31 Oct 2012 16:41:29 +0000 (+0100) Subject: refactored getAttribute X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=9ea16ec643cb6de5792508e07a4207bcaaf89f88;p=web%2Fkonrad%2Ftwig.git refactored getAttribute --- diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 9d60833..507c612 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -336,10 +336,10 @@ abstract class Twig_Template implements Twig_TemplateInterface */ 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])) ) { @@ -350,7 +350,7 @@ abstract class Twig_Template implements Twig_TemplateInterface return $object[$arrayItem]; } - if (Twig_TemplateInterface::ARRAY_CALL === $type) { + if (Twig_TemplateInterface::ARRAY_CALL === $type || !is_object($object)) { if ($isDefinedTest) { return false; } @@ -364,7 +364,7 @@ abstract class Twig_Template implements Twig_TemplateInterface } 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()); } } } @@ -378,13 +378,7 @@ abstract class Twig_Template implements Twig_TemplateInterface 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); diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index 636ae60..050c37f 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -42,11 +42,11 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase 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), );