Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
@docker-compose exec -e XDEBUG_MODE=coverage -ti php vendor/bin/phpunit --coverage-html var

sh:
@docker-compose exec -ti php /bin/sh
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
9 changes: 8 additions & 1 deletion src/ORM/Query/AST/ArrayExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion src/ORM/Query/AST/Functions/ArrayAgg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -26,6 +27,8 @@
*/
final class ArrayAgg extends AbstractAggregateWithFilterFunction implements TypedExpression
{
use VariadicTokenTrait;

private bool $distinct = false;

private Node $expr;
Expand All @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/ORM/Query/AST/Functions/Cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -19,6 +20,8 @@
*/
final class Cast extends FunctionNode
{
use VariadicTokenTrait;

public Node $source;

public string $type;
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/ORM/Query/AST/Functions/Extract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -20,6 +21,7 @@
*/
class Extract extends FunctionNode
{
use VariadicTokenTrait;
private string $expr;

private Node $from;
Expand All @@ -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();
Expand Down
9 changes: 6 additions & 3 deletions src/ORM/Query/AST/Functions/JsonGetArrayElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand All @@ -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);
Expand Down
9 changes: 6 additions & 3 deletions src/ORM/Query/AST/Functions/JsonGetArrayElementAsText.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand All @@ -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'));
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/ORM/Trait/VariadicTokenTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Pfilsx\PostgreSQLDoctrine\ORM\Trait;

use Doctrine\Common\Lexer\Token;

/**
* @internal
*/
trait VariadicTokenTrait
{
protected function getTokenField($token, string $field): mixed
{
if ($token === null) {
return null;
}

if (\class_exists(Token::class) && $token instanceof Token) {
return $token->$field;
}

return $token[$field] ?? null;
}
}