1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Query\AST\Functions\FunctionNode; |
6
|
|
|
use Doctrine\ORM\Query\AST\Node; |
7
|
|
|
use Doctrine\ORM\Query\Lexer; |
8
|
|
|
use Doctrine\ORM\Query\Parser; |
9
|
|
|
use Doctrine\ORM\Query\SqlWalker; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @since 0.1 |
13
|
|
|
* @author Martin Georgiev <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
abstract class AbstractFunction extends FunctionNode |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $functionPrototype; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string[] |
24
|
|
|
*/ |
25
|
|
|
protected $nodesMapping = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Node[] |
29
|
|
|
*/ |
30
|
|
|
protected $nodes = []; |
31
|
|
|
|
32
|
|
|
abstract protected function customiseFunction(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Sets function prototype |
36
|
|
|
* |
37
|
|
|
* @param string $functionPrototype |
38
|
|
|
*/ |
39
|
|
|
protected function setFunctionPrototype($functionPrototype) |
40
|
|
|
{ |
41
|
|
|
$this->functionPrototype = $functionPrototype; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Adds new node mapping |
46
|
|
|
* |
47
|
|
|
* @param string $parserMethod |
48
|
|
|
*/ |
49
|
|
|
protected function addNodeMapping($parserMethod) |
50
|
|
|
{ |
51
|
|
|
$this->nodesMapping[] = $parserMethod; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritDoc} |
56
|
|
|
*/ |
57
|
|
|
public function parse(Parser $parser) |
58
|
|
|
{ |
59
|
|
|
$this->customiseFunction(); |
60
|
|
|
|
61
|
|
|
$parser->match(Lexer::T_IDENTIFIER); |
62
|
|
|
$parser->match(Lexer::T_OPEN_PARENTHESIS); |
63
|
|
|
$this->feedParserWithNodes($parser); |
64
|
|
|
$parser->match(Lexer::T_CLOSE_PARENTHESIS); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Feeds given parser with previously set nodes |
69
|
|
|
* |
70
|
|
|
* @param Parser $parser |
71
|
|
|
*/ |
72
|
|
|
protected function feedParserWithNodes(Parser $parser) |
73
|
|
|
{ |
74
|
|
|
$nodesMappingCount = count($this->nodesMapping); |
75
|
|
|
$lastNode = $nodesMappingCount - 1; |
76
|
|
|
for ($i = 0; $i < $nodesMappingCount; $i++) { |
77
|
|
|
$parserMethod = $this->nodesMapping[$i]; |
78
|
|
|
$this->nodes[$i] = $parser->$parserMethod(); |
79
|
|
|
if ($i < $lastNode) { |
80
|
|
|
$parser->match(Lexer::T_COMMA); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritDoc} |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public function getSql(SqlWalker $sqlWalker) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$dispatched = []; |
91
|
|
|
foreach ($this->nodes as $node) { |
92
|
|
|
$dispatched[] = $node->dispatch($sqlWalker); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return vsprintf($this->functionPrototype, $dispatched); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.