diff --git a/src/Core/Parser/SyntaxTree/ObjectAccessorNode.php b/src/Core/Parser/SyntaxTree/ObjectAccessorNode.php index 616b1e7b4..a43fdc284 100644 --- a/src/Core/Parser/SyntaxTree/ObjectAccessorNode.php +++ b/src/Core/Parser/SyntaxTree/ObjectAccessorNode.php @@ -62,29 +62,42 @@ public function getObjectPath(): string */ public function evaluate(RenderingContextInterface $renderingContext): mixed { - $objectPath = strtolower($this->objectPath); $variableProvider = $renderingContext->getVariableProvider(); - if ($objectPath === '_all') { - return $variableProvider->getAll(); - } - return $variableProvider->getByPath($this->objectPath); + return match (strtolower($this->objectPath)) { + '_all' => $variableProvider->getAll(), + 'true' => true, + 'false' => false, + 'null' => null, + default => $variableProvider->getByPath($this->objectPath), + }; } public function convert(TemplateCompiler $templateCompiler): array { - $path = $this->objectPath; - if ($path === '_all') { - return [ + return match (strtolower($this->objectPath)) { + '_all' => [ 'initialization' => '', 'execution' => '$renderingContext->getVariableProvider()->getAll()', - ]; - } - return [ - 'initialization' => '', - 'execution' => sprintf( - '$renderingContext->getVariableProvider()->getByPath(\'%s\')', - $path, - ), - ]; + ], + 'true' => [ + 'initialization' => '', + 'execution' => 'true', + ], + 'false' => [ + 'initialization' => '', + 'execution' => 'false', + ], + 'null' => [ + 'initialization' => '', + 'execution' => 'null', + ], + default => [ + 'initialization' => '', + 'execution' => sprintf( + '$renderingContext->getVariableProvider()->getByPath(\'%s\')', + $this->objectPath, + ), + ], + }; } } diff --git a/src/Core/Variables/InvalidVariableIdentifierException.php b/src/Core/Variables/InvalidVariableIdentifierException.php new file mode 100644 index 000000000..792c17d5b --- /dev/null +++ b/src/Core/Variables/InvalidVariableIdentifierException.php @@ -0,0 +1,12 @@ +disallowedIdentifiers)) { + throw new InvalidVariableIdentifierException('Invalid variable identifier: ' . $identifier, 1723131119); + } $this->variables[$identifier] = $value; } diff --git a/tests/Unit/Core/Parser/SyntaxTree/ObjectAccessorNodeTest.php b/tests/Unit/Core/Parser/SyntaxTree/ObjectAccessorNodeTest.php index d01db785b..5edc70cb8 100644 --- a/tests/Unit/Core/Parser/SyntaxTree/ObjectAccessorNodeTest.php +++ b/tests/Unit/Core/Parser/SyntaxTree/ObjectAccessorNodeTest.php @@ -28,6 +28,9 @@ public static function getEvaluateTestValues(): array [['foo' => ['bar' => 'test'], 'dynamic' => 'bar'], 'foo.{dynamic}', 'test'], [['foo' => ['bar' => 'test'], 'dynamic' => ['sub' => 'bar']], 'foo.{dynamic.sub}', 'test'], [['foo' => ['bar' => 'test'], 'dynamic' => ['sub' => 'bar'], 'baz' => 'sub'], 'foo.{dynamic.{baz}}', 'test'], + [[], 'true', true], + [[], 'false', false], + [[], 'null', null], ]; } @@ -43,7 +46,7 @@ public function testEvaluateGetsExpectedValue(array $variables, string $path, $e $variableContainer = new StandardVariableProvider($variables); $renderingContext->expects(self::any())->method('getVariableProvider')->willReturn($variableContainer); $value = $node->evaluate($renderingContext); - self::assertEquals($expected, $value); + self::assertSame($expected, $value); } #[Test] diff --git a/tests/Unit/Core/Variables/StandardVariableProviderTest.php b/tests/Unit/Core/Variables/StandardVariableProviderTest.php index 4839a9659..3ed261179 100644 --- a/tests/Unit/Core/Variables/StandardVariableProviderTest.php +++ b/tests/Unit/Core/Variables/StandardVariableProviderTest.php @@ -12,6 +12,7 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; +use TYPO3Fluid\Fluid\Core\Variables\InvalidVariableIdentifierException; use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider; use TYPO3Fluid\Fluid\Tests\Unit\Core\Variables\Fixtures\StandardVariableProviderModelFixture; @@ -434,4 +435,13 @@ public function getByPathReturnsExpectedValues(array $variables, string $path, $ $result = $subject->getByPath($path); self::assertEquals($expected, $result); } + + #[Test] + public function exceptionIsThrownForInvalidVariableIdentifier(): void + { + self::expectException(InvalidVariableIdentifierException::class); + self::expectExceptionCode(1723131119); + $subject = new StandardVariableProvider(); + $subject->add('TrUe', false); + } }