1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Query\AST\Node; |
8
|
|
|
use Doctrine\ORM\Query\Lexer; |
9
|
|
|
use Doctrine\ORM\Query\Parser; |
10
|
|
|
use Doctrine\ORM\Query\SqlWalker; |
11
|
|
|
use Doctrine\ORM\Query\TokenType; |
12
|
|
|
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception\InvalidArgumentForVariadicFunctionException; |
13
|
|
|
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception\ParserException; |
14
|
|
|
use MartinGeorgiev\Utils\DoctrineLexer; |
15
|
|
|
use MartinGeorgiev\Utils\DoctrineOrm; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Martin Georgiev <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
abstract class BaseVariadicFunction extends BaseFunction |
21
|
|
|
{ |
22
|
110 |
|
protected function customizeFunction(): void |
23
|
|
|
{ |
24
|
110 |
|
$this->setFunctionPrototype(\sprintf('%s(%%s)', $this->getFunctionName())); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
abstract protected function getFunctionName(): string; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return array<string> |
31
|
|
|
*/ |
32
|
|
|
abstract protected function getNodeMappingPattern(): array; |
33
|
|
|
|
34
|
|
|
abstract protected function getMinArgumentCount(): int; |
35
|
|
|
|
36
|
|
|
abstract protected function getMaxArgumentCount(): int; |
37
|
|
|
|
38
|
116 |
|
protected function feedParserWithNodes(Parser $parser): void |
39
|
|
|
{ |
40
|
116 |
|
foreach ($this->getNodeMappingPattern() as $nodeMappingPattern) { |
41
|
|
|
try { |
42
|
116 |
|
$this->feedParserWithNodesForNodeMappingPattern($parser, $nodeMappingPattern); |
43
|
|
|
|
44
|
52 |
|
break; |
45
|
64 |
|
} catch (ParserException) { |
46
|
|
|
// swallow and continue with next pattern |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @throws InvalidArgumentForVariadicFunctionException |
53
|
|
|
* @throws ParserException |
54
|
|
|
*/ |
55
|
142 |
|
private function feedParserWithNodesForNodeMappingPattern(Parser $parser, string $nodeMappingPattern): void |
56
|
|
|
{ |
57
|
142 |
|
$nodeMapping = \explode(',', $nodeMappingPattern); |
58
|
142 |
|
$lexer = $parser->getLexer(); |
59
|
|
|
|
60
|
|
|
try { |
61
|
142 |
|
$lookaheadType = DoctrineLexer::getLookaheadType($lexer); |
62
|
142 |
|
if ($lookaheadType === null) { |
63
|
|
|
throw InvalidArgumentForVariadicFunctionException::atLeast($this->getFunctionName(), $this->getMinArgumentCount()); |
64
|
|
|
} |
65
|
|
|
|
66
|
142 |
|
$this->nodes[] = $parser->{$nodeMapping[0]}(); // @phpstan-ignore-line |
67
|
28 |
|
} catch (\Throwable $throwable) { |
68
|
28 |
|
throw ParserException::withThrowable($throwable); |
69
|
|
|
} |
70
|
|
|
|
71
|
114 |
|
$shouldUseLexer = DoctrineOrm::isPre219(); |
72
|
114 |
|
$isNodeMappingASimplePattern = \count($nodeMapping) === 1; |
73
|
114 |
|
$nodeIndex = 1; |
74
|
114 |
|
while (($shouldUseLexer ? Lexer::T_CLOSE_PARENTHESIS : TokenType::T_CLOSE_PARENTHESIS) !== $lookaheadType) { |
|
|
|
|
75
|
114 |
|
if (($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA) === $lookaheadType) { |
|
|
|
|
76
|
85 |
|
$parser->match($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA); |
77
|
|
|
|
78
|
|
|
// Check if we're about to exceed the maximum number of arguments |
79
|
|
|
// nodeIndex starts at 1 and counts up for each argument after the first |
80
|
|
|
// So when nodeIndex=1, we're about to add the 2nd argument (total: 2) |
81
|
|
|
// When nodeIndex=2, we're about to add the 3rd argument (total: 3) |
82
|
85 |
|
$foundMoreNodesThanMappingExpected = ($nodeIndex + 1) > $this->getMaxArgumentCount(); |
83
|
85 |
|
if ($foundMoreNodesThanMappingExpected) { |
84
|
22 |
|
if ($this->getMinArgumentCount() === $this->getMaxArgumentCount()) { |
85
|
1 |
|
throw InvalidArgumentForVariadicFunctionException::exactCount($this->getFunctionName(), $this->getMinArgumentCount()); |
86
|
|
|
} |
87
|
|
|
|
88
|
21 |
|
throw InvalidArgumentForVariadicFunctionException::between($this->getFunctionName(), $this->getMinArgumentCount(), $this->getMaxArgumentCount()); |
89
|
|
|
} |
90
|
|
|
|
91
|
85 |
|
$expectedNodeIndex = $isNodeMappingASimplePattern ? 0 : $nodeIndex; |
92
|
85 |
|
$argumentCountExceedsMappingPatternExpectation = !\array_key_exists($expectedNodeIndex, $nodeMapping); |
93
|
85 |
|
if ($argumentCountExceedsMappingPatternExpectation) { |
94
|
|
|
throw InvalidArgumentForVariadicFunctionException::unsupportedCombination( |
95
|
|
|
$this->getFunctionName(), |
96
|
|
|
\count($this->nodes) + 1, |
97
|
|
|
'implementation defines fewer node mappings than the actually provided argument count' |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
85 |
|
$this->nodes[] = $parser->{$nodeMapping[$expectedNodeIndex]}(); // @phpstan-ignore-line |
102
|
85 |
|
$nodeIndex++; |
103
|
|
|
} |
104
|
|
|
|
105
|
114 |
|
$lookaheadType = DoctrineLexer::getLookaheadType($lexer); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Final validation ensures all arguments meet requirements, including any special rules in subclass implementations |
109
|
92 |
|
$this->validateArguments(...$this->nodes); // @phpstan-ignore-line |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @throws InvalidArgumentForVariadicFunctionException |
114
|
|
|
*/ |
115
|
144 |
|
protected function validateArguments(Node ...$arguments): void |
116
|
|
|
{ |
117
|
144 |
|
$minArgumentCount = $this->getMinArgumentCount(); |
118
|
144 |
|
$maxArgumentCount = $this->getMaxArgumentCount(); |
119
|
144 |
|
$argumentCount = \count($arguments); |
120
|
|
|
|
121
|
144 |
|
if ($minArgumentCount === $maxArgumentCount && $argumentCount !== $minArgumentCount) { |
122
|
1 |
|
throw InvalidArgumentForVariadicFunctionException::exactCount($this->getFunctionName(), $this->getMinArgumentCount()); |
123
|
|
|
} |
124
|
|
|
|
125
|
143 |
|
if ($argumentCount < $minArgumentCount) { |
126
|
46 |
|
throw InvalidArgumentForVariadicFunctionException::atLeast($this->getFunctionName(), $this->getMinArgumentCount()); |
127
|
|
|
} |
128
|
|
|
|
129
|
97 |
|
if ($argumentCount > $maxArgumentCount) { |
130
|
26 |
|
throw InvalidArgumentForVariadicFunctionException::between($this->getFunctionName(), $this->getMinArgumentCount(), $this->getMaxArgumentCount()); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
54 |
|
public function getSql(SqlWalker $sqlWalker): string |
135
|
|
|
{ |
136
|
54 |
|
$dispatched = []; |
137
|
54 |
|
foreach ($this->nodes as $node) { |
138
|
52 |
|
$dispatched[] = $node instanceof Node ? $node->dispatch($sqlWalker) : 'null'; |
139
|
|
|
} |
140
|
|
|
|
141
|
54 |
|
return \sprintf($this->functionPrototype, \implode(', ', $dispatched)); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|