1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Query\Expression; |
6
|
|
|
|
7
|
|
|
use Countable; |
8
|
|
|
use function count; |
9
|
|
|
use function implode; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Composite expression is responsible to build a group of similar expression. |
13
|
|
|
*/ |
14
|
|
|
class CompositeExpression implements Countable |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Constant that represents an AND composite expression. |
18
|
|
|
*/ |
19
|
|
|
public const TYPE_AND = 'AND'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constant that represents an OR composite expression. |
23
|
|
|
*/ |
24
|
|
|
public const TYPE_OR = 'OR'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The instance type of composite expression. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $type; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Each expression part of the composite expression. |
35
|
|
|
* |
36
|
|
|
* @var self[]|string[] |
37
|
|
|
*/ |
38
|
|
|
private $parts = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $type Instance type of composite expression. |
42
|
|
|
* @param self[]|string[] $parts Composition of expressions to be joined on composite expression. |
43
|
|
|
*/ |
44
|
891 |
|
public function __construct(string $type, array $parts = []) |
45
|
|
|
{ |
46
|
891 |
|
$this->type = $type; |
47
|
|
|
|
48
|
891 |
|
$this->addMultiple($parts); |
49
|
891 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Adds multiple parts to composite expression. |
53
|
|
|
* |
54
|
|
|
* @param array<int, self|string> $parts |
55
|
|
|
* |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
891 |
|
public function addMultiple(array $parts = []) : self |
59
|
|
|
{ |
60
|
891 |
|
foreach ($parts as $part) { |
61
|
567 |
|
$this->add($part); |
62
|
|
|
} |
63
|
|
|
|
64
|
891 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Adds an expression to composite expression. |
69
|
|
|
* |
70
|
|
|
* @param self|string $part |
71
|
|
|
* |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
891 |
|
public function add($part) : self |
75
|
|
|
{ |
76
|
891 |
|
if (empty($part)) { |
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
891 |
|
if ($part instanceof self && count($part) === 0) { |
81
|
27 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
891 |
|
$this->parts[] = $part; |
85
|
|
|
|
86
|
891 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Retrieves the amount of expressions on composite expression. |
91
|
|
|
*/ |
92
|
864 |
|
public function count() : int |
93
|
|
|
{ |
94
|
864 |
|
return count($this->parts); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Retrieves the string representation of this composite expression. |
99
|
|
|
*/ |
100
|
810 |
|
public function __toString() : string |
101
|
|
|
{ |
102
|
810 |
|
if ($this->count() === 1) { |
103
|
189 |
|
return (string) $this->parts[0]; |
104
|
|
|
} |
105
|
|
|
|
106
|
621 |
|
return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Returns the type of this composite expression (AND/OR). |
111
|
|
|
*/ |
112
|
54 |
|
public function getType() : string |
113
|
|
|
{ |
114
|
54 |
|
return $this->type; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|