Fix ArrayAccess with objects as keys

This commit is contained in:
Gregor Harlan
2024-12-05 12:31:45 +01:00
parent 57bf519af2
commit 32a75b7869
2 changed files with 16 additions and 3 deletions
+1 -1
View File
@@ -1644,7 +1644,7 @@ final class CoreExtension extends AbstractExtension
// array
if (Template::METHOD_CALL !== $type) {
$arrayItem = \is_bool($item) || \is_float($item) ? (int) $item : $item = (string) $item;
$arrayItem = \is_bool($item) || \is_float($item) ? (int) $item : $item;
if ($sandboxed && $object instanceof \ArrayAccess && !\in_array($object::class, self::ARRAY_LIKE_CLASSES, true)) {
try {
+15 -2
View File
@@ -42,12 +42,19 @@ Twig supports array notation
{# ArrayAccess #}
{{ array_access['a'] }}
{# ObjectStorage #}
{{ object_storage[object] }}
{{ object_storage[object_storage]|default('bar') }}
{# array that does not exist #}
{{ does_not_exist[0]|default('ok') }}
{{ does_not_exist[0].does_not_exist_either|default('ok') }}
{{ does_not_exist[0]['does_not_exist_either']|default('ok') }}
--DATA--
return ['bar' => 'bar', 'foo' => ['bar' => 'bar'], 'array_access' => new \ArrayObject(['a' => 'b'])]
$objectStorage = new SplObjectStorage();
$object = new stdClass();
$objectStorage[$object] = 'foo';
return ['bar' => 'bar', 'foo' => ['bar' => 'bar'], 'array_access' => new \ArrayObject(['a' => 'b']), 'object_storage' => $objectStorage, 'object' => $object]
--EXPECT--
1,2
foo,bar
@@ -71,11 +78,14 @@ a,b,c,d,e
b
foo
bar
ok
ok
ok
--DATA--
return ['bar' => 'bar', 'foo' => ['bar' => 'bar'], 'array_access' => new \ArrayObject(['a' => 'b'])]
return ['bar' => 'bar', 'foo' => ['bar' => 'bar'], 'array_access' => new \ArrayObject(['a' => 'b']), 'object' => new stdClass()]
--CONFIG--
return ['strict_variables' => false]
--EXPECT--
@@ -101,6 +111,9 @@ a,b,c,d,e
b
bar
ok
ok
ok