1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* File: Query.php |
4
|
|
|
* Category: - |
5
|
|
|
* Author: M. Goldenbaum |
6
|
|
|
* Created: 21.07.18 18:54 |
7
|
|
|
* Updated: - |
8
|
|
|
* |
9
|
|
|
* Description: |
10
|
|
|
* - |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Webklex\IMAP\Query; |
14
|
|
|
|
15
|
|
|
use Webklex\IMAP\Exceptions\InvalidWhereQueryCriteriaException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Query |
19
|
|
|
* |
20
|
|
|
* @package Webklex\IMAP\Query |
21
|
|
|
*/ |
22
|
|
|
class WhereQuery extends Query { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array $available_criteria |
26
|
|
|
*/ |
27
|
|
|
protected $available_criteria = [ |
28
|
|
|
'OR', 'AND', |
29
|
|
|
'ALL', 'ANSWERED', 'BCC', 'BEFORE', 'BODY', 'CC', 'DELETED', 'FLAGGED', 'FROM', 'KEYWORD', |
30
|
|
|
'NEW', 'OLD', 'ON', 'RECENT', 'SEEN', 'SINCE', 'SUBJECT', 'TEXT', 'TO', |
31
|
|
|
'UNANSWERED', 'UNDELETED', 'UNFLAGGED', 'UNKEYWORD', 'UNSEEN' |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param $criteria |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
* @throws InvalidWhereQueryCriteriaException |
39
|
|
|
*/ |
40
|
|
|
protected function validate_criteria($criteria){ |
41
|
|
|
$criteria = strtoupper($criteria); |
42
|
|
|
|
43
|
|
|
if(in_array($criteria, $this->available_criteria) === false) { |
44
|
|
|
throw new InvalidWhereQueryCriteriaException(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $criteria; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string|array $criteria |
52
|
|
|
* @param mixed $value |
53
|
|
|
* |
54
|
|
|
* @return $this |
55
|
|
|
* @throws InvalidWhereQueryCriteriaException |
56
|
|
|
*/ |
57
|
|
|
public function where($criteria, $value = null){ |
58
|
|
|
if(is_array($criteria)){ |
59
|
|
|
foreach($criteria as $arguments){ |
60
|
|
|
if(count($arguments) == 1){ |
61
|
|
|
$this->where($arguments[0]); |
62
|
|
|
}elseif(count($arguments) == 2){ |
63
|
|
|
$this->where($arguments[0], $arguments[1]); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
}else{ |
67
|
|
|
$criteria = $this->validate_criteria($criteria); |
68
|
|
|
$value = $this->parse_value($value); |
69
|
|
|
|
70
|
|
|
if($value === null || $value === ''){ |
71
|
|
|
$this->query->push([$criteria]); |
72
|
|
|
}else{ |
73
|
|
|
$this->query->push([$criteria, $value]); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param \Closure $closure |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function orWhere(\Closure $closure = null){ |
86
|
|
|
$this->query->push(['OR']); |
87
|
|
|
if($closure !== null) $closure($this); |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param \Closure $closure |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function andWhere(\Closure $closure = null) { |
98
|
|
|
$this->query->push(['AND']); |
99
|
|
|
if($closure !== null) $closure($this); |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return WhereQuery |
106
|
|
|
* @throws InvalidWhereQueryCriteriaException |
107
|
|
|
*/ |
108
|
|
|
public function whereAll(){ |
109
|
|
|
return $this->where('ALL'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return WhereQuery |
114
|
|
|
* @throws InvalidWhereQueryCriteriaException |
115
|
|
|
*/ |
116
|
|
|
public function whereAnswered(){ |
117
|
|
|
return $this->where('ANSWERED'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $value |
122
|
|
|
* |
123
|
|
|
* @return WhereQuery |
124
|
|
|
* @throws InvalidWhereQueryCriteriaException |
125
|
|
|
*/ |
126
|
|
|
public function whereBcc($value){ |
127
|
|
|
return $this->where('BCC', $value); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param mixed $value |
132
|
|
|
* |
133
|
|
|
* @return WhereQuery |
134
|
|
|
* @throws \Webklex\IMAP\Exceptions\MessageSearchValidationException |
135
|
|
|
*/ |
136
|
|
|
public function whereBefore($value){ |
137
|
|
|
$date = $this->parse_date($value); |
138
|
|
|
return $this->where('BEFORE', $date); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $value |
143
|
|
|
* |
144
|
|
|
* @return WhereQuery |
145
|
|
|
* @throws InvalidWhereQueryCriteriaException |
146
|
|
|
*/ |
147
|
|
|
public function whereBody($value){ |
148
|
|
|
return $this->where('BODY', $value); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $value |
153
|
|
|
* |
154
|
|
|
* @return WhereQuery |
155
|
|
|
* @throws InvalidWhereQueryCriteriaException |
156
|
|
|
*/ |
157
|
|
|
public function whereCc($value){ |
158
|
|
|
return $this->where('CC', $value); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return WhereQuery |
163
|
|
|
* @throws InvalidWhereQueryCriteriaException |
164
|
|
|
*/ |
165
|
|
|
public function whereDeleted(){ |
166
|
|
|
return $this->where('DELETED'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string $value |
171
|
|
|
* |
172
|
|
|
* @return WhereQuery |
173
|
|
|
* @throws InvalidWhereQueryCriteriaException |
174
|
|
|
*/ |
175
|
|
|
public function whereFlagged($value){ |
176
|
|
|
return $this->where('FLAGGED', $value); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $value |
181
|
|
|
* |
182
|
|
|
* @return WhereQuery |
183
|
|
|
* @throws InvalidWhereQueryCriteriaException |
184
|
|
|
*/ |
185
|
|
|
public function whereFrom($value){ |
186
|
|
|
return $this->where('FROM', $value); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string $value |
191
|
|
|
* |
192
|
|
|
* @return WhereQuery |
193
|
|
|
* @throws InvalidWhereQueryCriteriaException |
194
|
|
|
*/ |
195
|
|
|
public function whereKeyword($value){ |
196
|
|
|
return $this->where('KEYWORD', $value); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return WhereQuery |
201
|
|
|
* @throws InvalidWhereQueryCriteriaException |
202
|
|
|
*/ |
203
|
|
|
public function whereNew(){ |
204
|
|
|
return $this->where('NEW'); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return WhereQuery |
209
|
|
|
* @throws InvalidWhereQueryCriteriaException |
210
|
|
|
*/ |
211
|
|
|
public function whereOld(){ |
212
|
|
|
return $this->where('OLD'); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param mixed $value |
217
|
|
|
* |
218
|
|
|
* @return WhereQuery |
219
|
|
|
* @throws \Webklex\IMAP\Exceptions\MessageSearchValidationException |
220
|
|
|
*/ |
221
|
|
|
public function whereOn($value){ |
222
|
|
|
$date = $this->parse_date($value); |
223
|
|
|
return $this->where('ON', $date); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return WhereQuery |
228
|
|
|
* @throws InvalidWhereQueryCriteriaException |
229
|
|
|
*/ |
230
|
|
|
public function whereRecent(){ |
231
|
|
|
return $this->where('RECENT'); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return WhereQuery |
236
|
|
|
* @throws InvalidWhereQueryCriteriaException |
237
|
|
|
*/ |
238
|
|
|
public function whereSeen(){ |
239
|
|
|
return $this->where('SEEN'); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param mixed $value |
244
|
|
|
* |
245
|
|
|
* @return WhereQuery |
246
|
|
|
* @throws \Webklex\IMAP\Exceptions\MessageSearchValidationException |
247
|
|
|
*/ |
248
|
|
|
public function whereSince($value){ |
249
|
|
|
$date = $this->parse_date($value); |
250
|
|
|
return $this->where('SINCE', $date); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param string $value |
255
|
|
|
* |
256
|
|
|
* @return WhereQuery |
257
|
|
|
* @throws InvalidWhereQueryCriteriaException |
258
|
|
|
*/ |
259
|
|
|
public function whereSubject($value){ |
260
|
|
|
return $this->where('SUBJECT', $value); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @param string $value |
265
|
|
|
* |
266
|
|
|
* @return WhereQuery |
267
|
|
|
* @throws InvalidWhereQueryCriteriaException |
268
|
|
|
*/ |
269
|
|
|
public function whereText($value){ |
270
|
|
|
return $this->where('TEXT', $value); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param string $value |
275
|
|
|
* |
276
|
|
|
* @return WhereQuery |
277
|
|
|
* @throws InvalidWhereQueryCriteriaException |
278
|
|
|
*/ |
279
|
|
|
public function whereTo($value){ |
280
|
|
|
return $this->where('TO', $value); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @param string $value |
285
|
|
|
* |
286
|
|
|
* @return WhereQuery |
287
|
|
|
* @throws InvalidWhereQueryCriteriaException |
288
|
|
|
*/ |
289
|
|
|
public function whereUnkeyword($value){ |
290
|
|
|
return $this->where('UNKEYWORD', $value); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @return WhereQuery |
295
|
|
|
* @throws InvalidWhereQueryCriteriaException |
296
|
|
|
*/ |
297
|
|
|
public function whereUnanswered(){ |
298
|
|
|
return $this->where('UNANSWERED'); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @return WhereQuery |
303
|
|
|
* @throws InvalidWhereQueryCriteriaException |
304
|
|
|
*/ |
305
|
|
|
public function whereUndeleted(){ |
306
|
|
|
return $this->where('UNDELETED'); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @return WhereQuery |
311
|
|
|
* @throws InvalidWhereQueryCriteriaException |
312
|
|
|
*/ |
313
|
|
|
public function whereUnflagged(){ |
314
|
|
|
return $this->where('UNFLAGGED'); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @return WhereQuery |
319
|
|
|
* @throws InvalidWhereQueryCriteriaException |
320
|
|
|
*/ |
321
|
|
|
public function whereUnseen(){ |
322
|
|
|
return $this->where('UNSEEN'); |
323
|
|
|
} |
324
|
|
|
} |