|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SQLParser\Node; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
|
6
|
|
|
use Mouf\MoufManager; |
|
7
|
|
|
use Mouf\MoufInstanceDescriptor; |
|
8
|
|
|
use SQLParser\Node\Traverser\NodeTraverser; |
|
9
|
|
|
use SQLParser\Node\Traverser\VisitorInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This class represents an operator with many operators (AND, OR...) in an SQL expression. |
|
13
|
|
|
* |
|
14
|
|
|
* @author David Négrier <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class AbstractManyInstancesOperator implements NodeInterface |
|
17
|
|
|
{ |
|
18
|
|
|
private $operands; |
|
19
|
|
|
|
|
20
|
|
|
public function getOperands() |
|
21
|
|
|
{ |
|
22
|
|
|
return $this->operands; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Sets the operands. |
|
27
|
|
|
* |
|
28
|
|
|
* @Important |
|
29
|
|
|
* //@param array<array<NodeInterface>> $operands |
|
30
|
|
|
* |
|
31
|
|
|
* @param array<NodeInterface> $operands |
|
32
|
|
|
*/ |
|
33
|
|
|
public function setOperands($operands) |
|
34
|
|
|
{ |
|
35
|
|
|
if (!is_array($operands)) { |
|
36
|
|
|
$operands = array($operands); |
|
37
|
|
|
} |
|
38
|
|
|
$this->operands = $operands; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Returns a Mouf instance descriptor describing this object. |
|
43
|
|
|
* |
|
44
|
|
|
* @param MoufManager $moufManager |
|
45
|
|
|
* |
|
46
|
|
|
* @return MoufInstanceDescriptor |
|
47
|
|
|
*/ |
|
48
|
|
View Code Duplication |
public function toInstanceDescriptor(MoufManager $moufManager) |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
$instanceDescriptor = $moufManager->createInstance(get_called_class()); |
|
51
|
|
|
$instanceDescriptor->getProperty('operands')->setValue(NodeFactory::nodeToInstanceDescriptor($this->operands, $moufManager)); |
|
52
|
|
|
|
|
53
|
|
|
return $instanceDescriptor; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Renders the object as a SQL string. |
|
58
|
|
|
* |
|
59
|
|
|
* @param Connection $dbConnection |
|
60
|
|
|
* @param array $parameters |
|
61
|
|
|
* @param number $indent |
|
62
|
|
|
* @param int $conditionsMode |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY) |
|
67
|
|
|
{ |
|
68
|
|
|
$sqlOperands = array(); |
|
69
|
|
|
foreach ($this->operands as $operand) { |
|
70
|
|
|
$sql = NodeFactory::toSql($operand, $dbConnection, $parameters, ' ', true, $indent, $conditionsMode); |
|
71
|
|
|
if ($sql != null) { |
|
|
|
|
|
|
72
|
|
|
$sqlOperands[] = $sql; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return implode("\n".str_repeat(' ', $indent).$this->getOperatorSymbol().' ', $sqlOperands); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Walks the tree of nodes, calling the visitor passed in parameter. |
|
81
|
|
|
* |
|
82
|
|
|
* @param VisitorInterface $visitor |
|
83
|
|
|
*/ |
|
84
|
|
View Code Duplication |
public function walk(VisitorInterface $visitor) |
|
|
|
|
|
|
85
|
|
|
{ |
|
86
|
|
|
$node = $this; |
|
87
|
|
|
$result = $visitor->enterNode($node); |
|
88
|
|
|
if ($result instanceof NodeInterface) { |
|
89
|
|
|
$node = $result; |
|
90
|
|
|
} |
|
91
|
|
|
if ($result !== NodeTraverser::DONT_TRAVERSE_CHILDREN) { |
|
92
|
|
|
foreach ($this->operands as $key => $operand) { |
|
93
|
|
|
$result2 = $operand->walk($visitor); |
|
94
|
|
|
if ($result2 === NodeTraverser::REMOVE_NODE) { |
|
95
|
|
|
unset($this->operands[$key]); |
|
96
|
|
|
} elseif ($result2 instanceof NodeInterface) { |
|
97
|
|
|
$this->operands[$key] = $result2; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $visitor->leaveNode($node); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Returns the symbol for this operator. |
|
107
|
|
|
* |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
abstract protected function getOperatorSymbol(); |
|
111
|
|
|
} |
|
112
|
|
|
|
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.