1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace BenTools\Where\Helper; |
5
|
|
|
|
6
|
|
|
use BenTools\Where\Expression\Expression; |
7
|
|
|
use function BenTools\Where\placeholders; |
8
|
|
|
use function BenTools\Where\where; |
9
|
|
|
|
10
|
|
|
class FieldHelper |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $field; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Field constructor. |
19
|
|
|
*/ |
20
|
|
|
public function __construct(string $field) |
21
|
|
|
{ |
22
|
|
|
$this->field = $field; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return Expression |
27
|
|
|
* @throws \InvalidArgumentException |
28
|
|
|
*/ |
29
|
|
|
public function isNull(): Expression |
30
|
|
|
{ |
31
|
|
|
return where(sprintf('%s IS NULL', $this->field)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return Expression |
36
|
|
|
* @throws \InvalidArgumentException |
37
|
|
|
*/ |
38
|
|
|
public function isNotNull(): Expression |
39
|
|
|
{ |
40
|
|
|
return where(sprintf('%s IS NOT NULL', $this->field)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $values |
45
|
|
|
* @param null|string $placeholder |
46
|
|
|
* @param string $glue |
47
|
|
|
* @return Expression |
48
|
|
|
* @throws \InvalidArgumentException |
49
|
|
|
*/ |
50
|
|
|
public function in(array $values, ?string $placeholder = '?', string $glue = ', '): Expression |
51
|
|
|
{ |
52
|
|
|
$expression = '%s IN (%s)'; |
53
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, placeholders($values, $placeholder, $glue)), ...array_values($values)) : where(sprintf($expression, $this->field, implode(', ', $values))); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $values |
58
|
|
|
* @param string $placeholder |
59
|
|
|
* @param string $glue |
60
|
|
|
* @return Expression |
61
|
|
|
* @throws \InvalidArgumentException |
62
|
|
|
*/ |
63
|
|
|
public function notIn(array $values, ?string $placeholder = '?', string $glue = ', '): Expression |
64
|
|
|
{ |
65
|
|
|
$expression = '%s NOT IN (%s)'; |
66
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, placeholders($values, $placeholder, $glue)), ...array_values($values)) : where(sprintf($expression, $this->field, implode(', ', $values))); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $value |
71
|
|
|
* @param string $placeholder |
72
|
|
|
* @return Expression |
73
|
|
|
* @throws \InvalidArgumentException |
74
|
|
|
*/ |
75
|
|
|
public function equals($value, ?string $placeholder = '?'): Expression |
76
|
|
|
{ |
77
|
|
|
$expression = '%s = %s'; |
78
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param $value |
83
|
|
|
* @param string $placeholder |
84
|
|
|
* @return Expression |
85
|
|
|
* @throws \InvalidArgumentException |
86
|
|
|
*/ |
87
|
|
|
public function notEquals($value, ?string $placeholder = '?'): Expression |
88
|
|
|
{ |
89
|
|
|
$expression = '%s <> %s'; |
90
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $value |
95
|
|
|
* @param string $placeholder |
96
|
|
|
* @return Expression |
97
|
|
|
* @throws \InvalidArgumentException |
98
|
|
|
*/ |
99
|
|
|
public function lt($value, ?string $placeholder = '?'): Expression |
100
|
|
|
{ |
101
|
|
|
$expression = '%s < %s'; |
102
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $value |
107
|
|
|
* @param string $placeholder |
108
|
|
|
* @return Expression |
109
|
|
|
* @throws \InvalidArgumentException |
110
|
|
|
*/ |
111
|
|
|
public function lte($value, ?string $placeholder = '?'): Expression |
112
|
|
|
{ |
113
|
|
|
$expression = '%s <= %s'; |
114
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $value |
119
|
|
|
* @param string $placeholder |
120
|
|
|
* @return Expression |
121
|
|
|
* @throws \InvalidArgumentException |
122
|
|
|
*/ |
123
|
|
|
public function gt($value, ?string $placeholder = '?'): Expression |
124
|
|
|
{ |
125
|
|
|
$expression = '%s > %s'; |
126
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $value |
131
|
|
|
* @param string $placeholder |
132
|
|
|
* @return Expression |
133
|
|
|
* @throws \InvalidArgumentException |
134
|
|
|
*/ |
135
|
|
|
public function gte($value, ?string $placeholder = '?'): Expression |
136
|
|
|
{ |
137
|
|
|
$expression = '%s >= %s'; |
138
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param $start |
143
|
|
|
* @param $end |
144
|
|
|
* @param string $placeholder |
145
|
|
|
* @return Expression |
146
|
|
|
* @throws \InvalidArgumentException |
147
|
|
|
*/ |
148
|
|
|
public function between($start, $end, ?string $placeholder = '?'): Expression |
149
|
|
|
{ |
150
|
|
|
$expression = '%s BETWEEN %s AND %s'; |
151
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder, $placeholder), $start, $end) : where(sprintf($expression, $this->field, $start, $end)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param $start |
156
|
|
|
* @param $end |
157
|
|
|
* @param string $placeholder |
158
|
|
|
* @return Expression |
159
|
|
|
* @throws \InvalidArgumentException |
160
|
|
|
*/ |
161
|
|
|
public function notBetween($start, $end, ?string $placeholder = '?'): Expression |
162
|
|
|
{ |
163
|
|
|
$expression = '%s NOT BETWEEN %s AND %s'; |
164
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder, $placeholder), $start, $end) : where(sprintf($expression, $this->field, $start, $end)); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $value |
169
|
|
|
* @param string $placeholder |
170
|
|
|
* @param string $surroundWith |
171
|
|
|
* @return Expression |
172
|
|
|
* @throws \InvalidArgumentException |
173
|
|
|
*/ |
174
|
|
|
public function like(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression |
175
|
|
|
{ |
176
|
|
|
$expression = '%s LIKE %s'; |
177
|
|
|
$value = $surroundWith . $value . $surroundWith; |
178
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string $value |
183
|
|
|
* @param string $placeholder |
184
|
|
|
* @param string $surroundWith |
185
|
|
|
* @return Expression |
186
|
|
|
* @throws \InvalidArgumentException |
187
|
|
|
*/ |
188
|
|
|
public function notLike(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression |
189
|
|
|
{ |
190
|
|
|
$expression = '%s NOT LIKE %s'; |
191
|
|
|
$value = $surroundWith . $value . $surroundWith; |
192
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param string $value |
197
|
|
|
* @param string $placeholder |
198
|
|
|
* @param string $surroundWith |
199
|
|
|
* @return Expression |
200
|
|
|
* @throws \InvalidArgumentException |
201
|
|
|
*/ |
202
|
|
|
public function startsWith(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression |
203
|
|
|
{ |
204
|
|
|
$expression = '%s LIKE %s'; |
205
|
|
|
$value = $value . $surroundWith; |
206
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param string $value |
211
|
|
|
* @param string $placeholder |
212
|
|
|
* @param string $surroundWith |
213
|
|
|
* @return Expression |
214
|
|
|
* @throws \InvalidArgumentException |
215
|
|
|
*/ |
216
|
|
|
public function notStartsWith(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression |
217
|
|
|
{ |
218
|
|
|
$expression = '%s NOT LIKE %s'; |
219
|
|
|
$value = $value . $surroundWith; |
220
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param string $value |
225
|
|
|
* @param string $placeholder |
226
|
|
|
* @param string $surroundWith |
227
|
|
|
* @return Expression |
228
|
|
|
* @throws \InvalidArgumentException |
229
|
|
|
*/ |
230
|
|
|
public function endsWith(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression |
231
|
|
|
{ |
232
|
|
|
$expression = '%s LIKE %s'; |
233
|
|
|
$value = $surroundWith . $value; |
234
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param string $value |
239
|
|
|
* @param string $placeholder |
240
|
|
|
* @param string $surroundWith |
241
|
|
|
* @return Expression |
242
|
|
|
* @throws \InvalidArgumentException |
243
|
|
|
*/ |
244
|
|
|
public function notEndsWith(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression |
245
|
|
|
{ |
246
|
|
|
$expression = '%s NOT LIKE %s'; |
247
|
|
|
$value = $surroundWith . $value; |
248
|
|
|
return null !== $placeholder ? where(sprintf($expression, $this->field, $placeholder), $value) : where(sprintf($expression, $this->field, $value)); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|