fixed getAttribute for array access with a boolean or float key
authorTobias Schultze <webmaster@tubo-world.de>
Wed, 31 Oct 2012 16:04:56 +0000 (17:04 +0100)
committerTobias Schultze <webmaster@tubo-world.de>
Fri, 15 Mar 2013 10:58:45 +0000 (11:58 +0100)
lib/Twig/Template.php

index c73ad4d..95e0819 100644 (file)
@@ -338,14 +338,16 @@ abstract class Twig_Template implements Twig_TemplateInterface
     {
         // array
         if (Twig_TemplateInterface::METHOD_CALL !== $type) {
-            if ((is_array($object) && array_key_exists((string) $item, $object))
-                || ($object instanceof ArrayAccess && isset($object[(string) $item]))
+            $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]))
             ) {
                 if ($isDefinedTest) {
                     return true;
                 }
 
-                return $object[(string) $item];
+                return $object[$arrayItem];
             }
 
             if (Twig_TemplateInterface::ARRAY_CALL === $type) {
@@ -358,11 +360,11 @@ abstract class Twig_Template implements Twig_TemplateInterface
                 }
 
                 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)), -1, $this->getTemplateName());
+                    throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $arrayItem, get_class($object)), -1, $this->getTemplateName());
                 } 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))), -1, $this->getTemplateName());
+                    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', $item, gettype($object)), -1, $this->getTemplateName());
+                    throw new Twig_Error_Runtime(sprintf('Impossible to access a key ("%s") on a "%s" variable', $arrayItem, gettype($object)), -1, $this->getTemplateName());
                 }
             }
         }