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 src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
*/
class Arr extends BaseVariadicFunction
{
protected string $commonNodeMapping = 'StringPrimary';
protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function customizeFunction(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class ArrayToJson extends BaseVariadicFunction
{
use BooleanValidationTrait;

protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function customizeFunction(): void
{
$this->setFunctionPrototype('array_to_json(%s)');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
*/
abstract class BaseComparisonFunction extends BaseVariadicFunction
{
protected string $commonNodeMapping = 'SimpleArithmeticExpression';
protected function getNodeMappingPattern(): array
{
return ['SimpleArithmeticExpression'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,37 @@
*/
abstract class BaseVariadicFunction extends BaseFunction
{
protected string $commonNodeMapping = 'StringPrimary';
/**
* @return array<int|string, string>
*/
abstract protected function getNodeMappingPattern(): array;

/**
* @throws ParserException
*/
protected function feedParserWithNodes(Parser $parser): void
{
foreach ($this->getNodeMappingPattern() as $nodeMappingPattern) {
try {
$this->feedParserWithNodesForNodeMappingPattern($parser, $nodeMappingPattern);

break;
} catch (ParserException) {
// swallow and continue with next pattern
}
}

$this->validateArguments(...$this->nodes); // @phpstan-ignore-line
}

private function feedParserWithNodesForNodeMappingPattern(Parser $parser, string $nodeMappingPattern): void
{
$nodeMapping = \explode(',', $nodeMappingPattern);
$lexer = $parser->getLexer();

try {
// @phpstan-ignore-next-line
$this->nodes[] = $parser->{$this->commonNodeMapping}();
$this->nodes[] = $parser->{$nodeMapping[0]}();
$lookaheadType = DoctrineLexer::getLookaheadType($lexer);
if ($lookaheadType === null) {
throw ParserException::missingLookaheadType();
Expand All @@ -40,18 +59,18 @@ protected function feedParserWithNodes(Parser $parser): void
}

$shouldUseLexer = DoctrineOrm::isPre219();

$isNodeMappingASimplePattern = \count($nodeMapping) === 1;
$nodeIndex = 1;
while (($shouldUseLexer ? Lexer::T_CLOSE_PARENTHESIS : TokenType::T_CLOSE_PARENTHESIS) !== $lookaheadType) {
if (($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA) === $lookaheadType) {
$parser->match($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA);
// @phpstan-ignore-next-line
$this->nodes[] = $parser->{$this->commonNodeMapping}();
$this->nodes[] = $parser->{$nodeMapping[$isNodeMappingASimplePattern ? 0 : $nodeIndex]}();
$nodeIndex++;
}

$lookaheadType = DoctrineLexer::getLookaheadType($lexer);
}

$this->validateArguments(...$this->nodes); // @phpstan-ignore-line
}

public function getSql(SqlWalker $sqlWalker): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class DateAdd extends BaseVariadicFunction
{
use TimezoneValidationTrait;

protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function customizeFunction(): void
{
$this->setFunctionPrototype('date_add(%s)');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class DateSubtract extends BaseVariadicFunction
{
use TimezoneValidationTrait;

protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function customizeFunction(): void
{
$this->setFunctionPrototype('date_subtract(%s)');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
*/
class JsonBuildObject extends BaseVariadicFunction
{
protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function customizeFunction(): void
{
$this->setFunctionPrototype('json_build_object(%s)');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
*/
class JsonbBuildObject extends BaseVariadicFunction
{
protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function customizeFunction(): void
{
$this->setFunctionPrototype('jsonb_build_object(%s)');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class JsonbInsert extends BaseVariadicFunction
{
use BooleanValidationTrait;

protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function customizeFunction(): void
{
$this->setFunctionPrototype('jsonb_insert(%s)');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class JsonbPathExists extends BaseVariadicFunction
{
use BooleanValidationTrait;

protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function customizeFunction(): void
{
$this->setFunctionPrototype('jsonb_path_exists(%s)');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class JsonbPathMatch extends BaseVariadicFunction
{
use BooleanValidationTrait;

protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function customizeFunction(): void
{
$this->setFunctionPrototype('jsonb_path_match(%s)');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class JsonbPathQuery extends BaseVariadicFunction
{
use BooleanValidationTrait;

protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function customizeFunction(): void
{
$this->setFunctionPrototype('jsonb_path_query(%s)');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class JsonbPathQueryArray extends BaseVariadicFunction
{
use BooleanValidationTrait;

protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function customizeFunction(): void
{
$this->setFunctionPrototype('jsonb_path_query_array(%s)');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class JsonbPathQueryFirst extends BaseVariadicFunction
{
use BooleanValidationTrait;

protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function customizeFunction(): void
{
$this->setFunctionPrototype('jsonb_path_query_first(%s)');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class JsonbSet extends BaseVariadicFunction
{
use BooleanValidationTrait;

protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function customizeFunction(): void
{
$this->setFunctionPrototype('jsonb_set(%s)');
Expand Down
5 changes: 4 additions & 1 deletion src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
*/
class Row extends BaseVariadicFunction
{
protected string $commonNodeMapping = 'InParameter';
protected function getNodeMappingPattern(): array
{
return ['InParameter'];
}

protected function customizeFunction(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class RowToJson extends BaseVariadicFunction
{
use BooleanValidationTrait;

protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function customizeFunction(): void
{
$this->setFunctionPrototype('row_to_json(%s)');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
*/
class ToTsquery extends BaseVariadicFunction
{
protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function customizeFunction(): void
{
$this->setFunctionPrototype('to_tsquery(%s)');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
*/
class ToTsvector extends BaseVariadicFunction
{
protected string $commonNodeMapping = 'StringExpression';
protected function getNodeMappingPattern(): array
{
return ['StringExpression'];
}

protected function customizeFunction(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
*/
class Unaccent extends BaseVariadicFunction
{
protected string $commonNodeMapping = 'StringPrimary';
protected function getNodeMappingPattern(): array
{
return ['StringPrimary'];
}

protected function customizeFunction(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\Parser;
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\BaseComparisonFunction;
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception\ParserException;
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception\InvalidArgumentForVariadicFunctionException;

abstract class BaseComparisonFunctionTestCase extends TestCase
{
Expand All @@ -20,7 +20,7 @@ abstract protected function createFixture(): BaseComparisonFunction;
*/
public function throws_an_exception_when_lexer_is_not_populated_with_a_lookahead_type(): void
{
$this->expectException(ParserException::class);
$this->expectException(InvalidArgumentForVariadicFunctionException::class);

$em = $this->createMock(EntityManager::class);
$em->expects($this->any())
Expand Down
Loading