Skip to content

Commit

Permalink
Closes #388 [2] (#401)
Browse files Browse the repository at this point in the history
* Closes #388

* Fixed test
  • Loading branch information
bytestream authored Jul 15, 2020
1 parent c7e4acd commit 3555932
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Node/GetAttrNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace TwigBridge\Node;

use ArrayAccess;
use Twig\Compiler;
use Twig\Environment;
use Twig\Error\RuntimeError;
Expand Down Expand Up @@ -129,7 +130,12 @@ public static function attribute(
$sandboxed = false,
int $lineno = -1
) {
if (Template::METHOD_CALL !== $type and is_a($object, 'Illuminate\Database\Eloquent\Model')) {
// Twig doesn't support sandboxing on objects that implement ArrayAccess
// https://github.com/twigphp/Twig/issues/106#issuecomment-583737
// https://github.com/twigphp/Twig/pull/1863
//
// https://github.com/twigphp/Twig/issues/2878
if (Template::METHOD_CALL !== $type and $object instanceof ArrayAccess) {
// We can't easily find out if an attribute actually exists, so return true
if ($isDefinedTest) {
return true;
Expand All @@ -139,8 +145,8 @@ public static function attribute(
$env->getExtension(SandboxExtension::class)->checkPropertyAllowed($object, $item);
}

// Call the attribute, the Model object does the rest of the magic
return $object->$item;
// Call the attribute, the object does the rest of the magic
return isset($object[$item]) ? $object[$item] : null;
}

return \twig_get_attribute(
Expand Down

0 comments on commit 3555932

Please sign in to comment.