diff --git a/Makefile b/Makefile index f1479a4..cea513a 100644 --- a/Makefile +++ b/Makefile @@ -25,4 +25,7 @@ test-deprecations: @docker-compose exec -ti php vendor/bin/phpunit --display-deprecations test-with-coverage: - @docker-compose exec -e XDEBUG_MODE=coverage -ti php vendor/bin/phpunit --coverage-html var \ No newline at end of file + @docker-compose exec -e XDEBUG_MODE=coverage -ti php vendor/bin/phpunit --coverage-html var + +sh: + @docker-compose exec -ti php /bin/sh \ No newline at end of file diff --git a/composer.json b/composer.json index 011f08d..5ebd33f 100644 --- a/composer.json +++ b/composer.json @@ -36,13 +36,10 @@ }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.13", - "doctrine/orm": "^2.13.0", + "doctrine/orm": "^2.13", "symfony/serializer": ">=5.4", "symfony/property-info": ">=5.4", "phpunit/phpunit": "^10.0", "symfony/property-access": ">=5.4" - }, - "conflict": { - "doctrine/orm": ">=2.18.0" } } diff --git a/src/ORM/Query/AST/ArrayExpression.php b/src/ORM/Query/AST/ArrayExpression.php index d54947e..71a94fa 100644 --- a/src/ORM/Query/AST/ArrayExpression.php +++ b/src/ORM/Query/AST/ArrayExpression.php @@ -4,6 +4,7 @@ namespace Pfilsx\PostgreSQLDoctrine\ORM\Query\AST; +use Doctrine\Common\Lexer\Token; use Doctrine\ORM\Query\AST\Node; use Doctrine\ORM\Query\Lexer; use Doctrine\ORM\Query\Parser; @@ -34,7 +35,13 @@ public static function parse(Parser $parser): self \assert($lexer->lookahead !== null); $nodes = []; - switch ($lexer->lookahead['type']) { + if (\class_exists(Token::class) && $lexer->lookahead instanceof Token) { + $type = $lexer->lookahead->type; + } else { + $type = $lexer->lookahead['type'] ?? null; + } + + switch ($type) { case Lexer::T_INPUT_PARAMETER: $nodes[] = $parser->InputParameter(); diff --git a/src/ORM/Query/AST/Functions/AbstractAggregateWithFilterFunction.php b/src/ORM/Query/AST/Functions/AbstractAggregateWithFilterFunction.php index ce967cf..d8fc90e 100644 --- a/src/ORM/Query/AST/Functions/AbstractAggregateWithFilterFunction.php +++ b/src/ORM/Query/AST/Functions/AbstractAggregateWithFilterFunction.php @@ -9,9 +9,12 @@ use Doctrine\ORM\Query\Parser; use Doctrine\ORM\Query\SqlWalker; use Pfilsx\PostgreSQLDoctrine\ORM\Query\AST\FilterExpression; +use Pfilsx\PostgreSQLDoctrine\ORM\Trait\VariadicTokenTrait; abstract class AbstractAggregateWithFilterFunction extends FunctionNode { + use VariadicTokenTrait; + private const FILTER_IDENTIFIER = 'FILTER'; private ?FilterExpression $filterExpression = null; @@ -25,7 +28,7 @@ public function parse(Parser $parser): void return; } - $lookaheadValue = $lexer->lookahead['value'] ?? null; + $lookaheadValue = $this->getTokenField($lexer->lookahead, 'value'); if (!\is_string($lookaheadValue) || \mb_strtoupper($lookaheadValue) !== self::FILTER_IDENTIFIER) { return; diff --git a/src/ORM/Query/AST/Functions/ArrayAgg.php b/src/ORM/Query/AST/Functions/ArrayAgg.php index da49cf4..2a3b6bb 100644 --- a/src/ORM/Query/AST/Functions/ArrayAgg.php +++ b/src/ORM/Query/AST/Functions/ArrayAgg.php @@ -11,6 +11,7 @@ use Doctrine\ORM\Query\Lexer; use Doctrine\ORM\Query\Parser; use Doctrine\ORM\Query\SqlWalker; +use Pfilsx\PostgreSQLDoctrine\ORM\Trait\VariadicTokenTrait; /** * Implementation of PostgreSql ARRAY_AGG() function. @@ -26,6 +27,8 @@ */ final class ArrayAgg extends AbstractAggregateWithFilterFunction implements TypedExpression { + use VariadicTokenTrait; + private bool $distinct = false; private Node $expr; @@ -49,7 +52,7 @@ public function parseFunction(Parser $parser): void $parser->match(Lexer::T_COMMA); $parser->match(Lexer::T_STRING); - $this->returnType = $lexer->token['value']; + $this->returnType = $this->getTokenField($lexer->token, 'value'); } $parser->match(Lexer::T_CLOSE_PARENTHESIS); diff --git a/src/ORM/Query/AST/Functions/Cast.php b/src/ORM/Query/AST/Functions/Cast.php index dc6b99a..e81e5db 100644 --- a/src/ORM/Query/AST/Functions/Cast.php +++ b/src/ORM/Query/AST/Functions/Cast.php @@ -9,6 +9,7 @@ use Doctrine\ORM\Query\Lexer; use Doctrine\ORM\Query\Parser; use Doctrine\ORM\Query\SqlWalker; +use Pfilsx\PostgreSQLDoctrine\ORM\Trait\VariadicTokenTrait; /** * Implementation of PostgreSql CAST() function. @@ -19,6 +20,8 @@ */ final class Cast extends FunctionNode { + use VariadicTokenTrait; + public Node $source; public string $type; @@ -32,7 +35,7 @@ public function parse(Parser $parser): void $parser->match(Lexer::T_AS); $parser->match(Lexer::T_IDENTIFIER); - $type = $parser->getLexer()->token['value'] ?? null; + $type = $this->getTokenField($parser->getLexer()->token, 'value'); if (!\is_string($type)) { return; diff --git a/src/ORM/Query/AST/Functions/Extract.php b/src/ORM/Query/AST/Functions/Extract.php index c5facd6..9ae39d8 100644 --- a/src/ORM/Query/AST/Functions/Extract.php +++ b/src/ORM/Query/AST/Functions/Extract.php @@ -9,6 +9,7 @@ use Doctrine\ORM\Query\Lexer; use Doctrine\ORM\Query\Parser; use Doctrine\ORM\Query\SqlWalker; +use Pfilsx\PostgreSQLDoctrine\ORM\Trait\VariadicTokenTrait; /** * Implementation of PostgreSql EXTRACT function. @@ -20,6 +21,7 @@ */ class Extract extends FunctionNode { + use VariadicTokenTrait; private string $expr; private Node $from; @@ -30,7 +32,7 @@ public function parse(Parser $parser): void $parser->match(Lexer::T_OPEN_PARENTHESIS); $parser->match(Lexer::T_IDENTIFIER); - $this->expr = $parser->getLexer()->token['value']; + $this->expr = $this->getTokenField($parser->getLexer()->token, 'value'); $parser->match(Lexer::T_FROM); $this->from = $parser->StringPrimary(); diff --git a/src/ORM/Query/AST/Functions/JsonGetArrayElement.php b/src/ORM/Query/AST/Functions/JsonGetArrayElement.php index 2ee588a..9619e8b 100644 --- a/src/ORM/Query/AST/Functions/JsonGetArrayElement.php +++ b/src/ORM/Query/AST/Functions/JsonGetArrayElement.php @@ -7,6 +7,7 @@ use Doctrine\ORM\Query\AST\Literal; use Doctrine\ORM\Query\Lexer; use Doctrine\ORM\Query\Parser; +use Pfilsx\PostgreSQLDoctrine\ORM\Trait\VariadicTokenTrait; /** * Implementation of PostgreSql JSON(B) array field retrieval by index. @@ -15,8 +16,10 @@ * * @example JSON_GET_ARRAY_ELEMENT(entity.field, 1) */ -final class JsonGetArrayElement extends JsonGetField +class JsonGetArrayElement extends JsonGetField { + use VariadicTokenTrait; + public function parse(Parser $parser): void { $parser->match(Lexer::T_IDENTIFIER); @@ -25,14 +28,14 @@ public function parse(Parser $parser): void $parser->match(Lexer::T_COMMA); $parser->match(Lexer::T_INTEGER); - $this->path[] = new Literal(Literal::NUMERIC, $parser->getLexer()->token['value']); + $this->path[] = new Literal(Literal::NUMERIC, $this->getTokenField($parser->getLexer()->token, 'value')); if (!$parser->getLexer()->isNextToken(Lexer::T_CLOSE_PARENTHESIS)) { while ($parser->getLexer()->isNextToken(Lexer::T_COMMA)) { $parser->match(Lexer::T_COMMA); $parser->match(Lexer::T_INTEGER); - $this->path[] = new Literal(Literal::NUMERIC, $parser->getLexer()->token['value']); + $this->path[] = new Literal(Literal::NUMERIC, $this->getTokenField($parser->getLexer()->token, 'value')); } } $parser->match(Lexer::T_CLOSE_PARENTHESIS); diff --git a/src/ORM/Query/AST/Functions/JsonGetArrayElementAsText.php b/src/ORM/Query/AST/Functions/JsonGetArrayElementAsText.php index 0fd727d..094e135 100644 --- a/src/ORM/Query/AST/Functions/JsonGetArrayElementAsText.php +++ b/src/ORM/Query/AST/Functions/JsonGetArrayElementAsText.php @@ -7,6 +7,7 @@ use Doctrine\ORM\Query\AST\Literal; use Doctrine\ORM\Query\Lexer; use Doctrine\ORM\Query\Parser; +use Pfilsx\PostgreSQLDoctrine\ORM\Trait\VariadicTokenTrait; /** * Implementation of PostgreSql JSON(B) array field retrieval by index. @@ -15,8 +16,10 @@ * * @example JSON_GET_ARRAY_ELEMENT_AS_TEXT(entity.field, 1) */ -final class JsonGetArrayElementAsText extends JsonGetFieldAsText +class JsonGetArrayElementAsText extends JsonGetFieldAsText { + use VariadicTokenTrait; + public function parse(Parser $parser): void { $parser->match(Lexer::T_IDENTIFIER); @@ -26,14 +29,14 @@ public function parse(Parser $parser): void $parser->match(Lexer::T_COMMA); $parser->match(Lexer::T_INTEGER); - $this->path[] = new Literal(Literal::NUMERIC, $parser->getLexer()->token['value']); + $this->path[] = new Literal(Literal::NUMERIC, $this->getTokenField($parser->getLexer()->token, 'value')); if (!$parser->getLexer()->isNextToken(Lexer::T_CLOSE_PARENTHESIS)) { while ($parser->getLexer()->isNextToken(Lexer::T_COMMA)) { $parser->match(Lexer::T_COMMA); $parser->match(Lexer::T_INTEGER); - $this->path[] = new Literal(Literal::NUMERIC, $parser->getLexer()->token['value']); + $this->path[] = new Literal(Literal::NUMERIC, $this->getTokenField($parser->getLexer()->token, 'value')); } } diff --git a/src/ORM/Trait/VariadicTokenTrait.php b/src/ORM/Trait/VariadicTokenTrait.php new file mode 100644 index 0000000..f607236 --- /dev/null +++ b/src/ORM/Trait/VariadicTokenTrait.php @@ -0,0 +1,26 @@ +$field; + } + + return $token[$field] ?? null; + } +}