mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-04 14:36:52 +00:00
bug #2813 fix key exists check for non ArrayObject objects (xabbuh)
This PR was merged into the 1.x branch.
Discussion
----------
fix key exists check for non ArrayObject objects
fixes #2810
Commits
-------
6df989ba fix key exists check for non ArrayObject objects
This commit is contained in:
@@ -508,7 +508,9 @@ abstract class Twig_Template implements Twig_TemplateInterface
|
||||
if (self::METHOD_CALL !== $type) {
|
||||
$arrayItem = is_bool($item) || is_float($item) ? (int) $item : $item;
|
||||
|
||||
if ((is_array($object) || $object instanceof ArrayAccess) && (isset($object[$arrayItem]) || array_key_exists($arrayItem, $object))) {
|
||||
if (((is_array($object) || $object instanceof ArrayObject) && (isset($object[$arrayItem]) || array_key_exists($arrayItem, $object)))
|
||||
|| ($object instanceof ArrayAccess && isset($object[$arrayItem]))
|
||||
) {
|
||||
if ($isDefinedTest) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -418,6 +418,11 @@ class Twig_Tests_TemplateTest extends \PHPUnit\Framework\TestCase
|
||||
[false, null, $methodAndPropObject, 'c', [], $arrayType],
|
||||
]);
|
||||
|
||||
$arrayAccess = new Twig_TemplateArrayAccess();
|
||||
$tests = array_merge($tests, [
|
||||
[true, ['foo' => 'bar'], $arrayAccess, 'vars', [], $anyType],
|
||||
]);
|
||||
|
||||
// tests when input is not an array or object
|
||||
$tests = array_merge($tests, [
|
||||
[false, null, 42, 'a', [], $anyType, 'Impossible to access an attribute ("a") on a integer variable ("42") in "index.twig".'],
|
||||
@@ -720,6 +725,34 @@ class Twig_TemplateMethodAndPropObject
|
||||
}
|
||||
}
|
||||
|
||||
class Twig_TemplateArrayAccess implements ArrayAccess
|
||||
{
|
||||
public $vars = [
|
||||
'foo' => 'bar',
|
||||
];
|
||||
private $children = [];
|
||||
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return array_key_exists($offset, $this->children);
|
||||
}
|
||||
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
return $this->children[$offset];
|
||||
}
|
||||
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
$this->children[$offset] = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
unset($this->children[$offset]);
|
||||
}
|
||||
}
|
||||
|
||||
class Twig_TemplateMagicMethodObject
|
||||
{
|
||||
public function __call($method, $arguments)
|
||||
|
||||
Reference in New Issue
Block a user