mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-15 11:56:50 +00:00
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) {
|
if (self::METHOD_CALL !== $type) {
|
||||||
$arrayItem = is_bool($item) || is_float($item) ? (int) $item : $item;
|
$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) {
|
if ($isDefinedTest) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -418,6 +418,11 @@ class Twig_Tests_TemplateTest extends \PHPUnit\Framework\TestCase
|
|||||||
[false, null, $methodAndPropObject, 'c', [], $arrayType],
|
[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 when input is not an array or object
|
||||||
$tests = array_merge($tests, [
|
$tests = array_merge($tests, [
|
||||||
[false, null, 42, 'a', [], $anyType, 'Impossible to access an attribute ("a") on a integer variable ("42") in "index.twig".'],
|
[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
|
class Twig_TemplateMagicMethodObject
|
||||||
{
|
{
|
||||||
public function __call($method, $arguments)
|
public function __call($method, $arguments)
|
||||||
|
|||||||
Reference in New Issue
Block a user