1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BenTools\Where\Helper; |
4
|
|
|
|
5
|
|
|
use BenTools\Where\Expression\Expression; |
6
|
|
|
use function BenTools\Where\where; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @internal |
10
|
|
|
*/ |
11
|
|
|
final class CaseHelper extends Expression |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var Expression |
16
|
|
|
*/ |
17
|
|
|
private $field; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Expression[] |
21
|
|
|
*/ |
22
|
|
|
private $when = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Expression[] |
26
|
|
|
*/ |
27
|
|
|
private $then = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Expression |
31
|
|
|
*/ |
32
|
|
|
private $else; |
33
|
|
|
|
34
|
|
|
private $lock = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* CaseHelper constructor. |
38
|
|
|
* @param Expression|null $field |
39
|
|
|
*/ |
40
|
|
|
private function __construct(Expression $field = null) |
41
|
|
|
{ |
42
|
|
|
$this->field = $field; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $expression |
47
|
|
|
* @param array ...$values |
48
|
|
|
* @return CaseHelper |
49
|
|
|
* @throws \InvalidArgumentException |
50
|
|
|
*/ |
51
|
|
|
public function when($expression, ...$values): self |
52
|
|
|
{ |
53
|
|
|
if (\count($this->when) !== \count($this->then)) { |
54
|
|
|
throw new \RuntimeException("Mising a 'then' somewhere."); |
55
|
|
|
} |
56
|
|
|
if (true === $this->lock) { |
57
|
|
|
throw new \RuntimeException('This conditionnal structure is locked.'); |
58
|
|
|
} |
59
|
|
|
$clone = clone $this; |
60
|
|
|
$clone->when[] = where($expression, ...$values); |
61
|
|
|
return $clone; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $expression |
66
|
|
|
* @param array ...$values |
67
|
|
|
* @return CaseHelper |
68
|
|
|
* @throws \InvalidArgumentException |
69
|
|
|
*/ |
70
|
|
|
public function then($expression, ...$values): self |
71
|
|
|
{ |
72
|
|
|
if (\count($this->when) !== \count($this->then) + 1) { |
73
|
|
|
throw new \RuntimeException("Mising a 'when' somewhere."); |
74
|
|
|
} |
75
|
|
|
if (true === $this->lock) { |
76
|
|
|
throw new \RuntimeException('This conditionnal structure is locked.'); |
77
|
|
|
} |
78
|
|
|
$clone = clone $this; |
79
|
|
|
$clone->then[] = where($expression, ...$values); |
80
|
|
|
return $clone; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $expression |
85
|
|
|
* @param array ...$values |
86
|
|
|
* @return CaseHelper |
87
|
|
|
* @throws \InvalidArgumentException |
88
|
|
|
*/ |
89
|
|
|
public function else($expression, ...$values): self |
90
|
|
|
{ |
91
|
|
|
if (true === $this->lock) { |
92
|
|
|
throw new \RuntimeException('This conditionnal structure is locked.'); |
93
|
|
|
} |
94
|
|
|
$clone = clone $this; |
95
|
|
|
$clone->else = where($expression, ...$values); |
96
|
|
|
return $clone; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Only for fluent purposes. |
101
|
|
|
* |
102
|
|
|
* @return CaseHelper |
103
|
|
|
*/ |
104
|
|
|
public function end(): self |
105
|
|
|
{ |
106
|
|
|
$clone = clone $this; |
107
|
|
|
$clone->lock = true; |
108
|
|
|
return $clone; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function __toString(): string |
115
|
|
|
{ |
116
|
|
|
$parts = ['CASE']; |
117
|
|
|
|
118
|
|
|
if (null !== $this->field) { |
119
|
|
|
$parts[] = $this->field; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
foreach ($this->when as $key => $value) { |
123
|
|
|
$parts[] = 'WHEN'; |
124
|
|
|
$parts[] = $this->when[$key]; |
125
|
|
|
$parts[] = 'THEN'; |
126
|
|
|
$parts[] = $this->then[$key] ?? null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (null !== $this->else) { |
130
|
|
|
$parts[] = 'ELSE'; |
131
|
|
|
$parts[] = $this->else; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$parts[] = 'END'; |
135
|
|
|
|
136
|
|
|
return \implode(' ', $parts); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public function getValues(): array |
143
|
|
|
{ |
144
|
|
|
$values = [[]]; |
145
|
|
|
|
146
|
|
|
if (null !== $this->field) { |
147
|
|
|
$values[] = $this->field->getValues(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
foreach ($this->when as $key => $whenExpression) { |
151
|
|
|
$values[] = $whenExpression->getValues(); |
152
|
|
|
$thenExpression = $this->then[$key]; |
153
|
|
|
if (null !== $thenExpression) { |
154
|
|
|
$values[] = $thenExpression->getValues(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (null !== $this->else) { |
159
|
|
|
$values[] = $this->else->getValues(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return \array_merge(...$values); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param $expression |
167
|
|
|
* @param array ...$values |
168
|
|
|
* @return CaseHelper |
169
|
|
|
* @throws \InvalidArgumentException |
170
|
|
|
*/ |
171
|
|
|
public static function create($expression = null, ...$values): self |
172
|
|
|
{ |
173
|
|
|
if (null !== $expression) { |
174
|
|
|
$expression = where($expression, ...$values); |
175
|
|
|
} |
176
|
|
|
return new self($expression); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|