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 Illuminate\Support\Str; |
16
|
|
|
use Webklex\IMAP\Exceptions\InvalidWhereQueryCriteriaException; |
17
|
|
|
use Webklex\IMAP\Exceptions\MethodNotFoundException; |
18
|
|
|
use Webklex\IMAP\Exceptions\MessageSearchValidationException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class WhereQuery |
22
|
|
|
* |
23
|
|
|
* @package Webklex\IMAP\Query |
24
|
|
|
* |
25
|
|
|
* @method WhereQuery all() |
26
|
|
|
* @method WhereQuery answered() |
27
|
|
|
* @method WhereQuery deleted() |
28
|
|
|
* @method WhereQuery new() |
29
|
|
|
* @method WhereQuery old() |
30
|
|
|
* @method WhereQuery recent() |
31
|
|
|
* @method WhereQuery seen() |
32
|
|
|
* @method WhereQuery unanswered() |
33
|
|
|
* @method WhereQuery undeleted() |
34
|
|
|
* @method WhereQuery unflagged() |
35
|
|
|
* @method WhereQuery unseen() |
36
|
|
|
* @method WhereQuery not() |
37
|
|
|
* @method WhereQuery unkeyword($value) |
38
|
|
|
* @method WhereQuery to($value) |
39
|
|
|
* @method WhereQuery text($value) |
40
|
|
|
* @method WhereQuery subject($value) |
41
|
|
|
* @method WhereQuery since($date) |
42
|
|
|
* @method WhereQuery on($date) |
43
|
|
|
* @method WhereQuery keyword($value) |
44
|
|
|
* @method WhereQuery from($value) |
45
|
|
|
* @method WhereQuery flagged() |
46
|
|
|
* @method WhereQuery cc($value) |
47
|
|
|
* @method WhereQuery body($value) |
48
|
|
|
* @method WhereQuery before($date) |
49
|
|
|
* @method WhereQuery bcc($value) |
50
|
|
|
* |
51
|
|
|
* @mixin Query |
52
|
|
|
*/ |
53
|
|
|
class WhereQuery extends Query { |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array $available_criteria |
57
|
|
|
*/ |
58
|
|
|
protected $available_criteria = [ |
59
|
|
|
'OR', 'AND', |
60
|
|
|
'ALL', 'ANSWERED', 'BCC', 'BEFORE', 'BODY', 'CC', 'DELETED', 'FLAGGED', 'FROM', 'KEYWORD', |
61
|
|
|
'NEW', 'NOT', 'OLD', 'ON', 'RECENT', 'SEEN', 'SINCE', 'SUBJECT', 'TEXT', 'TO', |
62
|
|
|
'UNANSWERED', 'UNDELETED', 'UNFLAGGED', 'UNKEYWORD', 'UNSEEN' |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Magic method in order to allow alias usage of all "where" methods in an optional connection with "NOT" |
67
|
|
|
* @param string $name |
68
|
|
|
* @param array|null $arguments |
69
|
|
|
* |
70
|
|
|
* @return mixed |
71
|
|
|
* @throws InvalidWhereQueryCriteriaException |
72
|
|
|
* @throws MethodNotFoundException |
73
|
|
|
*/ |
74
|
|
|
public function __call($name, $arguments) { |
75
|
|
|
$that = $this; |
76
|
|
|
|
77
|
|
|
$name = Str::camel($name); |
78
|
|
|
|
79
|
|
|
if(strtolower(substr($name, 0, 3)) === 'not') { |
80
|
|
|
$that = $that->whereNot(); |
81
|
|
|
$name = substr($name, 3); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$method = 'where'.ucfirst($name); |
85
|
|
|
if(method_exists($this, $method) === true){ |
86
|
|
|
return call_user_func_array([$that, $method], $arguments); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Validate a given criteria |
94
|
|
|
* @param $criteria |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
* @throws InvalidWhereQueryCriteriaException |
98
|
|
|
*/ |
99
|
|
|
protected function validate_criteria($criteria) { |
100
|
|
|
$criteria = strtoupper($criteria); |
101
|
|
|
|
102
|
|
|
if (substr($criteria, 0, 6) === "CUSTOM") { |
103
|
|
|
return substr($criteria, 6); |
104
|
|
|
} |
105
|
|
|
if(in_array($criteria, $this->available_criteria) === false) { |
106
|
|
|
throw new InvalidWhereQueryCriteriaException(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $criteria; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param mixed $criteria |
114
|
|
|
* @param null $value |
|
|
|
|
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
* @throws InvalidWhereQueryCriteriaException |
118
|
|
|
*/ |
119
|
|
|
public function where($criteria, $value = null) { |
120
|
|
|
if(is_array($criteria)){ |
121
|
|
|
foreach($criteria as $arguments){ |
122
|
|
|
if(count($arguments) == 1){ |
123
|
|
|
$this->where($arguments[0]); |
124
|
|
|
}elseif(count($arguments) == 2){ |
125
|
|
|
$this->where($arguments[0], $arguments[1]); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
}else{ |
129
|
|
|
$criteria = $this->validate_criteria($criteria); |
130
|
|
|
$value = $this->parse_value($value); |
131
|
|
|
|
132
|
|
|
if($value === null || $value === ''){ |
133
|
|
|
$this->query->push([$criteria]); |
134
|
|
|
}else{ |
135
|
|
|
$this->query->push([$criteria, $value]); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param \Closure $closure |
144
|
|
|
* |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
|
|
public function orWhere(\Closure $closure = null) { |
148
|
|
|
$this->query->push(['OR']); |
149
|
|
|
if($closure !== null) $closure($this); |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param \Closure $closure |
156
|
|
|
* |
157
|
|
|
* @return $this |
158
|
|
|
*/ |
159
|
|
|
public function andWhere(\Closure $closure = null) { |
160
|
|
|
$this->query->push(['AND']); |
161
|
|
|
if($closure !== null) $closure($this); |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return WhereQuery |
168
|
|
|
* @throws InvalidWhereQueryCriteriaException |
169
|
|
|
*/ |
170
|
|
|
public function whereAll() { |
171
|
|
|
return $this->where('ALL'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return WhereQuery |
176
|
|
|
* @throws InvalidWhereQueryCriteriaException |
177
|
|
|
*/ |
178
|
|
|
public function whereAnswered() { |
179
|
|
|
return $this->where('ANSWERED'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $value |
184
|
|
|
* |
185
|
|
|
* @return WhereQuery |
186
|
|
|
* @throws InvalidWhereQueryCriteriaException |
187
|
|
|
*/ |
188
|
|
|
public function whereBcc($value) { |
189
|
|
|
return $this->where('BCC', $value); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param mixed $value |
194
|
|
|
* @return WhereQuery |
195
|
|
|
* @throws InvalidWhereQueryCriteriaException |
196
|
|
|
* @throws MessageSearchValidationException |
197
|
|
|
*/ |
198
|
|
|
public function whereBefore($value) { |
199
|
|
|
$date = $this->parse_date($value); |
200
|
|
|
return $this->where('BEFORE', $date); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string $value |
205
|
|
|
* |
206
|
|
|
* @return WhereQuery |
207
|
|
|
* @throws InvalidWhereQueryCriteriaException |
208
|
|
|
*/ |
209
|
|
|
public function whereBody($value) { |
210
|
|
|
return $this->where('BODY', $value); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param string $value |
215
|
|
|
* |
216
|
|
|
* @return WhereQuery |
217
|
|
|
* @throws InvalidWhereQueryCriteriaException |
218
|
|
|
*/ |
219
|
|
|
public function whereCc($value) { |
220
|
|
|
return $this->where('CC', $value); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return WhereQuery |
225
|
|
|
* @throws InvalidWhereQueryCriteriaException |
226
|
|
|
*/ |
227
|
|
|
public function whereDeleted() { |
228
|
|
|
return $this->where('DELETED'); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param string $value |
233
|
|
|
* |
234
|
|
|
* @return WhereQuery |
235
|
|
|
* @throws InvalidWhereQueryCriteriaException |
236
|
|
|
*/ |
237
|
|
|
public function whereFlagged($value) { |
238
|
|
|
return $this->where('FLAGGED', $value); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param string $value |
243
|
|
|
* |
244
|
|
|
* @return WhereQuery |
245
|
|
|
* @throws InvalidWhereQueryCriteriaException |
246
|
|
|
*/ |
247
|
|
|
public function whereFrom($value) { |
248
|
|
|
return $this->where('FROM', $value); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param string $value |
253
|
|
|
* |
254
|
|
|
* @return WhereQuery |
255
|
|
|
* @throws InvalidWhereQueryCriteriaException |
256
|
|
|
*/ |
257
|
|
|
public function whereKeyword($value) { |
258
|
|
|
return $this->where('KEYWORD', $value); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @return WhereQuery |
263
|
|
|
* @throws InvalidWhereQueryCriteriaException |
264
|
|
|
*/ |
265
|
|
|
public function whereNew() { |
266
|
|
|
return $this->where('NEW'); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @return WhereQuery |
271
|
|
|
* @throws InvalidWhereQueryCriteriaException |
272
|
|
|
*/ |
273
|
|
|
public function whereNot() { |
274
|
|
|
return $this->where('NOT'); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @return WhereQuery |
279
|
|
|
* @throws InvalidWhereQueryCriteriaException |
280
|
|
|
*/ |
281
|
|
|
public function whereOld() { |
282
|
|
|
return $this->where('OLD'); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param mixed $value |
287
|
|
|
* |
288
|
|
|
* @return WhereQuery |
289
|
|
|
* @throws MessageSearchValidationException |
290
|
|
|
* @throws InvalidWhereQueryCriteriaException |
291
|
|
|
*/ |
292
|
|
|
public function whereOn($value) { |
293
|
|
|
$date = $this->parse_date($value); |
294
|
|
|
return $this->where('ON', $date); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return WhereQuery |
299
|
|
|
* @throws InvalidWhereQueryCriteriaException |
300
|
|
|
*/ |
301
|
|
|
public function whereRecent() { |
302
|
|
|
return $this->where('RECENT'); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @return WhereQuery |
307
|
|
|
* @throws InvalidWhereQueryCriteriaException |
308
|
|
|
*/ |
309
|
|
|
public function whereSeen() { |
310
|
|
|
return $this->where('SEEN'); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @param mixed $value |
315
|
|
|
* |
316
|
|
|
* @return WhereQuery |
317
|
|
|
* @throws MessageSearchValidationException |
318
|
|
|
* @throws InvalidWhereQueryCriteriaException |
319
|
|
|
*/ |
320
|
|
|
public function whereSince($value) { |
321
|
|
|
$date = $this->parse_date($value); |
322
|
|
|
return $this->where('SINCE', $date); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @param string $value |
327
|
|
|
* |
328
|
|
|
* @return WhereQuery |
329
|
|
|
* @throws InvalidWhereQueryCriteriaException |
330
|
|
|
*/ |
331
|
|
|
public function whereSubject($value) { |
332
|
|
|
return $this->where('SUBJECT', $value); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @param string $value |
337
|
|
|
* |
338
|
|
|
* @return WhereQuery |
339
|
|
|
* @throws InvalidWhereQueryCriteriaException |
340
|
|
|
*/ |
341
|
|
|
public function whereText($value) { |
342
|
|
|
return $this->where('TEXT', $value); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* @param string $value |
347
|
|
|
* |
348
|
|
|
* @return WhereQuery |
349
|
|
|
* @throws InvalidWhereQueryCriteriaException |
350
|
|
|
*/ |
351
|
|
|
public function whereTo($value) { |
352
|
|
|
return $this->where('TO', $value); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @param string $value |
357
|
|
|
* |
358
|
|
|
* @return WhereQuery |
359
|
|
|
* @throws InvalidWhereQueryCriteriaException |
360
|
|
|
*/ |
361
|
|
|
public function whereUnkeyword($value) { |
362
|
|
|
return $this->where('UNKEYWORD', $value); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @return WhereQuery |
367
|
|
|
* @throws InvalidWhereQueryCriteriaException |
368
|
|
|
*/ |
369
|
|
|
public function whereUnanswered() { |
370
|
|
|
return $this->where('UNANSWERED'); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @return WhereQuery |
375
|
|
|
* @throws InvalidWhereQueryCriteriaException |
376
|
|
|
*/ |
377
|
|
|
public function whereUndeleted() { |
378
|
|
|
return $this->where('UNDELETED'); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* @return WhereQuery |
383
|
|
|
* @throws InvalidWhereQueryCriteriaException |
384
|
|
|
*/ |
385
|
|
|
public function whereUnflagged() { |
386
|
|
|
return $this->where('UNFLAGGED'); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* @return WhereQuery |
391
|
|
|
* @throws InvalidWhereQueryCriteriaException |
392
|
|
|
*/ |
393
|
|
|
public function whereUnseen() { |
394
|
|
|
return $this->where('UNSEEN'); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* @return WhereQuery |
399
|
|
|
* @throws InvalidWhereQueryCriteriaException |
400
|
|
|
*/ |
401
|
|
|
public function whereNoXSpam(){ |
402
|
|
|
return $this->where("X-Spam-Flag NO"); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* @return WhereQuery |
407
|
|
|
* @throws InvalidWhereQueryCriteriaException |
408
|
|
|
*/ |
409
|
|
|
public function whereIsXSpam(){ |
410
|
|
|
return $this->where("X-Spam-Flag YES"); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* @param $country_code |
415
|
|
|
* |
416
|
|
|
* @return WhereQuery |
417
|
|
|
* @throws InvalidWhereQueryCriteriaException |
418
|
|
|
*/ |
419
|
|
|
public function whereLanguage($country_code){ |
420
|
|
|
return $this->where("Content-Language $country_code"); |
421
|
|
|
} |
422
|
|
|
} |
423
|
|
|
|