1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Query\AST\Functions\FunctionNode; |
8
|
|
|
use Doctrine\ORM\Query\AST\Node; |
9
|
|
|
use Doctrine\ORM\Query\Lexer; |
10
|
|
|
use Doctrine\ORM\Query\Parser; |
11
|
|
|
use Doctrine\ORM\Query\SqlWalker; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @since 0.1 |
15
|
|
|
* |
16
|
|
|
* @author Martin Georgiev <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
abstract class BaseFunction extends FunctionNode |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $functionPrototype; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string[] |
27
|
|
|
*/ |
28
|
|
|
protected $nodesMapping = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Node[] |
32
|
|
|
*/ |
33
|
|
|
protected $nodes = []; |
34
|
|
|
|
35
|
|
|
abstract protected function customiseFunction(): void; |
36
|
|
|
|
37
|
|
|
protected function setFunctionPrototype(string $functionPrototype): void |
38
|
|
|
{ |
39
|
|
|
$this->functionPrototype = $functionPrototype; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function addNodeMapping(string $parserMethod): void |
43
|
|
|
{ |
44
|
|
|
$this->nodesMapping[] = $parserMethod; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function parse(Parser $parser): void |
48
|
|
|
{ |
49
|
|
|
$this->customiseFunction(); |
50
|
|
|
|
51
|
|
|
$parser->match(Lexer::T_IDENTIFIER); |
52
|
|
|
$parser->match(Lexer::T_OPEN_PARENTHESIS); |
53
|
|
|
$this->feedParserWithNodes($parser); |
54
|
|
|
$parser->match(Lexer::T_CLOSE_PARENTHESIS); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Feeds given parser with previously set nodes. |
59
|
|
|
*/ |
60
|
|
|
protected function feedParserWithNodes(Parser $parser): void |
61
|
|
|
{ |
62
|
|
|
$nodesMappingCount = \count($this->nodesMapping); |
63
|
|
|
$lastNode = $nodesMappingCount - 1; |
64
|
|
|
for ($i = 0; $i < $nodesMappingCount; $i++) { |
65
|
|
|
$parserMethod = $this->nodesMapping[$i]; |
66
|
|
|
$this->nodes[$i] = $parser->{$parserMethod}(); |
67
|
|
|
if ($i < $lastNode) { |
68
|
|
|
$parser->match(Lexer::T_COMMA); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getSql(SqlWalker $sqlWalker): string |
74
|
|
|
{ |
75
|
|
|
$dispatched = []; |
76
|
|
|
foreach ($this->nodes as $node) { |
77
|
|
|
$dispatched[] = $node->dispatch($sqlWalker); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return \vsprintf($this->functionPrototype, $dispatched); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|