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\Utils\DoctrineOrm; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Martin Georgiev <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
abstract class BaseVariadicFunction extends BaseFunction |
18
|
|
|
{ |
19
|
|
|
protected string $commonNodeMapping = 'StringPrimary'; |
20
|
|
|
|
21
|
|
|
public function feedParserWithNodes(Parser $parser): void |
22
|
|
|
{ |
23
|
|
|
$lexer = $parser->getLexer(); |
24
|
|
|
|
25
|
|
|
$this->nodes[] = $parser->{$this->commonNodeMapping}(); |
26
|
|
|
if ($lexer->lookahead?->type === null) { |
27
|
|
|
throw new \RuntimeException('The parser\'s "lookahead" property is not populated with a type'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$aheadType = $lexer->lookahead->type; |
31
|
|
|
$shouldUseLexer = DoctrineOrm::isPre219(); |
32
|
|
|
|
33
|
|
|
while (($shouldUseLexer ? Lexer::T_CLOSE_PARENTHESIS : TokenType::T_CLOSE_PARENTHESIS) !== $aheadType) { |
|
|
|
|
34
|
|
|
if (($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA) === $aheadType) { |
|
|
|
|
35
|
|
|
$parser->match($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA); |
36
|
|
|
$this->nodes[] = $parser->{$this->commonNodeMapping}(); |
37
|
|
|
} |
38
|
|
|
$aheadType = $lexer->lookahead->type; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getSql(SqlWalker $sqlWalker): string |
43
|
|
|
{ |
44
|
|
|
$dispatched = []; |
45
|
|
|
foreach ($this->nodes as $node) { |
46
|
|
|
$dispatched[] = $node instanceof Node ? $node->dispatch($sqlWalker) : 'null'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return \sprintf($this->functionPrototype, \implode(', ', $dispatched)); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|