diff --git a/Node/NullCoalesceNode.php b/Node/NullCoalesceNode.php index 1cc5eb0..025bb1a 100644 --- a/Node/NullCoalesceNode.php +++ b/Node/NullCoalesceNode.php @@ -39,7 +39,7 @@ public function compile(Compiler $compiler): void public function evaluate(array $functions, array $values): mixed { if ($this->nodes['expr1'] instanceof GetAttrNode) { - $this->nodes['expr1']->attributes['is_null_coalesce'] = true; + $this->addNullCoalesceAttributeToGetAttrNodes($this->nodes['expr1']); } return $this->nodes['expr1']->evaluate($functions, $values) ?? $this->nodes['expr2']->evaluate($functions, $values); @@ -49,4 +49,17 @@ public function toArray(): array { return ['(', $this->nodes['expr1'], ') ?? (', $this->nodes['expr2'], ')']; } + + private function addNullCoalesceAttributeToGetAttrNodes(Node $node): void + { + if (!$node instanceof GetAttrNode) { + return; + } + + $node->attributes['is_null_coalesce'] = true; + + foreach ($node->nodes as $node) { + $this->addNullCoalesceAttributeToGetAttrNodes($node); + } + } } diff --git a/Tests/ExpressionLanguageTest.php b/Tests/ExpressionLanguageTest.php index bef2395..0e2e964 100644 --- a/Tests/ExpressionLanguageTest.php +++ b/Tests/ExpressionLanguageTest.php @@ -424,6 +424,9 @@ public function bar() yield ['foo["bar"]["baz"] ?? "default"', ['bar' => null]]; yield ['foo["bar"].baz ?? "default"', ['bar' => null]]; yield ['foo.bar().baz ?? "default"', $foo]; + yield ['foo.bar.baz.bam ?? "default"', (object) ['bar' => null]]; + yield ['foo?.bar?.baz?.qux ?? "default"', (object) ['bar' => null]]; + yield ['foo[123][456][789] ?? "default"', [123 => []]]; } /**