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
|
|
|
* @param array $values |
27
|
|
|
* @param string $placeholder |
28
|
|
|
* @param string $glue |
29
|
|
|
* @return Expression |
30
|
|
|
* @throws \InvalidArgumentException |
31
|
|
|
*/ |
32
|
|
|
public function in(array $values, string $placeholder = '?', string $glue = ', '): Expression |
33
|
|
|
{ |
34
|
|
|
return where(sprintf('%s IN (%s)', $this->field, placeholders($values, $placeholder, $glue)), ...array_values($values)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $values |
39
|
|
|
* @param string $placeholder |
40
|
|
|
* @param string $glue |
41
|
|
|
* @return Expression |
42
|
|
|
* @throws \InvalidArgumentException |
43
|
|
|
*/ |
44
|
|
|
public function notIn(array $values, string $placeholder = '?', string $glue = ', '): Expression |
45
|
|
|
{ |
46
|
|
|
return where(sprintf('%s NOT IN (%s)', $this->field, placeholders($values, $placeholder, $glue)), ...array_values($values)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $value |
51
|
|
|
* @param string $placeholder |
52
|
|
|
* @return Expression |
53
|
|
|
* @throws \InvalidArgumentException |
54
|
|
|
*/ |
55
|
|
|
public function equals($value, string $placeholder = '?'): Expression |
56
|
|
|
{ |
57
|
|
|
return where(sprintf('%s = %s', $this->field, $placeholder), $value); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $value |
62
|
|
|
* @param string $placeholder |
63
|
|
|
* @return Expression |
64
|
|
|
* @throws \InvalidArgumentException |
65
|
|
|
*/ |
66
|
|
|
public function notEquals($value, string $placeholder = '?'): Expression |
67
|
|
|
{ |
68
|
|
|
return where(sprintf('%s <> %s', $this->field, $placeholder), $value); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $value |
73
|
|
|
* @param string $placeholder |
74
|
|
|
* @return Expression |
75
|
|
|
* @throws \InvalidArgumentException |
76
|
|
|
*/ |
77
|
|
|
public function lt($value, string $placeholder = '?'): Expression |
78
|
|
|
{ |
79
|
|
|
return where(sprintf('%s < %s', $this->field, $placeholder), $value); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $value |
84
|
|
|
* @param string $placeholder |
85
|
|
|
* @return Expression |
86
|
|
|
* @throws \InvalidArgumentException |
87
|
|
|
*/ |
88
|
|
|
public function lte($value, string $placeholder = '?'): Expression |
89
|
|
|
{ |
90
|
|
|
return where(sprintf('%s <= %s', $this->field, $placeholder), $value); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $value |
95
|
|
|
* @param string $placeholder |
96
|
|
|
* @return Expression |
97
|
|
|
* @throws \InvalidArgumentException |
98
|
|
|
*/ |
99
|
|
|
public function gt($value, string $placeholder = '?'): Expression |
100
|
|
|
{ |
101
|
|
|
return where(sprintf('%s > %s', $this->field, $placeholder), $value); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $value |
106
|
|
|
* @param string $placeholder |
107
|
|
|
* @return Expression |
108
|
|
|
* @throws \InvalidArgumentException |
109
|
|
|
*/ |
110
|
|
|
public function gte($value, string $placeholder = '?'): Expression |
111
|
|
|
{ |
112
|
|
|
return where(sprintf('%s >= %s', $this->field, $placeholder), $value); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $start |
117
|
|
|
* @param $end |
118
|
|
|
* @param string $placeholder |
119
|
|
|
* @return Expression |
120
|
|
|
* @throws \InvalidArgumentException |
121
|
|
|
*/ |
122
|
|
|
public function between($start, $end, string $placeholder = '?'): Expression |
123
|
|
|
{ |
124
|
|
|
return where(sprintf('%s BETWEEN %s AND %s', $this->field, $placeholder, $placeholder), $start, $end); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param $start |
129
|
|
|
* @param $end |
130
|
|
|
* @param string $placeholder |
131
|
|
|
* @return Expression |
132
|
|
|
* @throws \InvalidArgumentException |
133
|
|
|
*/ |
134
|
|
|
public function notBetween($start, $end, string $placeholder = '?'): Expression |
135
|
|
|
{ |
136
|
|
|
return where(sprintf('%s NOT BETWEEN %s AND %s', $this->field, $placeholder, $placeholder), $start, $end); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $value |
141
|
|
|
* @param string $placeholder |
142
|
|
|
* @param string $surroundWith |
143
|
|
|
* @return Expression |
144
|
|
|
* @throws \InvalidArgumentException |
145
|
|
|
*/ |
146
|
|
|
public function like(string $value, string $placeholder = '?', string $surroundWith = '%'): Expression |
147
|
|
|
{ |
148
|
|
|
return where(sprintf('%s LIKE %s', $this->field, $placeholder), $surroundWith . $value . $surroundWith); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $value |
153
|
|
|
* @param string $placeholder |
154
|
|
|
* @param string $surroundWith |
155
|
|
|
* @return Expression |
156
|
|
|
* @throws \InvalidArgumentException |
157
|
|
|
*/ |
158
|
|
|
public function notLike(string $value, string $placeholder = '?', string $surroundWith = '%'): Expression |
159
|
|
|
{ |
160
|
|
|
return where(sprintf('%s NOT LIKE %s', $this->field, $placeholder), $surroundWith . $value . $surroundWith); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $value |
165
|
|
|
* @param string $placeholder |
166
|
|
|
* @param string $surroundWith |
167
|
|
|
* @return Expression |
168
|
|
|
* @throws \InvalidArgumentException |
169
|
|
|
*/ |
170
|
|
|
public function startsWith(string $value, string $placeholder = '?', string $surroundWith = '%'): Expression |
171
|
|
|
{ |
172
|
|
|
return where(sprintf('%s LIKE %s', $this->field, $placeholder), $value . $surroundWith); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string $value |
177
|
|
|
* @param string $placeholder |
178
|
|
|
* @param string $surroundWith |
179
|
|
|
* @return Expression |
180
|
|
|
* @throws \InvalidArgumentException |
181
|
|
|
*/ |
182
|
|
|
public function notStartsWith(string $value, string $placeholder = '?', string $surroundWith = '%'): Expression |
183
|
|
|
{ |
184
|
|
|
return where(sprintf('%s NOT LIKE %s', $this->field, $placeholder), $value . $surroundWith); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param string $value |
189
|
|
|
* @param string $placeholder |
190
|
|
|
* @param string $surroundWith |
191
|
|
|
* @return Expression |
192
|
|
|
* @throws \InvalidArgumentException |
193
|
|
|
*/ |
194
|
|
|
public function endsWith(string $value, string $placeholder = '?', string $surroundWith = '%'): Expression |
195
|
|
|
{ |
196
|
|
|
return where(sprintf('%s LIKE %s', $this->field, $placeholder), $surroundWith . $value); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param string $value |
201
|
|
|
* @param string $placeholder |
202
|
|
|
* @param string $surroundWith |
203
|
|
|
* @return Expression |
204
|
|
|
* @throws \InvalidArgumentException |
205
|
|
|
*/ |
206
|
|
|
public function notEndsWith(string $value, string $placeholder = '?', string $surroundWith = '%'): Expression |
207
|
|
|
{ |
208
|
|
|
return where(sprintf('%s NOT LIKE %s', $this->field, $placeholder), $surroundWith . $value); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|