Passed
Pull Request — master (#301)
by
unknown
05:15
created

WhereQuery::whereTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 2
rs 10
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);
0 ignored issues
show
Bug introduced by
It seems like $arguments can also be of type null; however, parameter $param_arr of call_user_func_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
            return call_user_func_array([$that, $method], /** @scrutinizer ignore-type */ $arguments);
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
115
     * 
116
     * @return $this
117
     * @throws InvalidWhereQueryCriteriaException
118
     */
119
    public function where($criteria, $value = null) {
120
        if(is_array($criteria)){
121
            foreach($criteria as $key => $value){
122
                if(is_numeric($key)){
123
                    return $this->where($value);
124
                }
125
                return $this->where($key, $value);
126
            }
127
        }else{
128
            $criteria = $this->validate_criteria($criteria);
129
            $value = $this->parse_value($value);
130
131
            if($value === null || $value === ''){
132
                $this->query->push([$criteria]);
133
            }else{
134
                $this->query->push([$criteria, $value]);
135
            }
136
        }
137
138
        return $this;
139
    }
140
141
    /**
142
     * @param \Closure $closure
143
     *
144
     * @return $this
145
     */
146
    public function orWhere(\Closure $closure = null) {
147
        $this->query->push(['OR']);
148
        if($closure !== null) $closure($this);
149
150
        return $this;
151
    }
152
153
    /**
154
     * @param \Closure $closure
155
     *
156
     * @return $this
157
     */
158
    public function andWhere(\Closure $closure = null) {
159
        $this->query->push(['AND']);
160
        if($closure !== null) $closure($this);
161
162
        return $this;
163
    }
164
165
    /**
166
     * @return WhereQuery
167
     * @throws InvalidWhereQueryCriteriaException
168
     */
169
    public function whereAll() {
170
        return $this->where('ALL');
171
    }
172
173
    /**
174
     * @return WhereQuery
175
     * @throws InvalidWhereQueryCriteriaException
176
     */
177
    public function whereAnswered() {
178
        return $this->where('ANSWERED');
179
    }
180
181
    /**
182
     * @param string $value
183
     * 
184
     * @return WhereQuery
185
     * @throws InvalidWhereQueryCriteriaException
186
     */
187
    public function whereBcc($value) {
188
        return $this->where('BCC', $value);
189
    }
190
191
    /**
192
     * @param mixed $value
193
     * @return WhereQuery
194
     * @throws InvalidWhereQueryCriteriaException
195
     * @throws MessageSearchValidationException
196
     */
197
    public function whereBefore($value) {
198
        $date = $this->parse_date($value);
199
        return $this->where('BEFORE', $date);
200
    }
201
202
    /**
203
     * @param string $value
204
     *
205
     * @return WhereQuery
206
     * @throws InvalidWhereQueryCriteriaException
207
     */
208
    public function whereBody($value) {
209
        return $this->where('BODY', $value);
210
    }
211
212
    /**
213
     * @param string $value
214
     *
215
     * @return WhereQuery
216
     * @throws InvalidWhereQueryCriteriaException
217
     */
218
    public function whereCc($value) {
219
        return $this->where('CC', $value);
220
    }
221
222
    /**
223
     * @return WhereQuery
224
     * @throws InvalidWhereQueryCriteriaException
225
     */
226
    public function whereDeleted() {
227
        return $this->where('DELETED');
228
    }
229
230
    /**
231
     * @param string $value
232
     *
233
     * @return WhereQuery
234
     * @throws InvalidWhereQueryCriteriaException
235
     */
236
    public function whereFlagged($value) {
237
        return $this->where('FLAGGED', $value);
238
    }
239
240
    /**
241
     * @param string $value
242
     *
243
     * @return WhereQuery
244
     * @throws InvalidWhereQueryCriteriaException
245
     */
246
    public function whereFrom($value) {
247
        return $this->where('FROM', $value);
248
    }
249
250
    /**
251
     * @param string $value
252
     *
253
     * @return WhereQuery
254
     * @throws InvalidWhereQueryCriteriaException
255
     */
256
    public function whereKeyword($value) {
257
        return $this->where('KEYWORD', $value);
258
    }
259
260
    /**
261
     * @return WhereQuery
262
     * @throws InvalidWhereQueryCriteriaException
263
     */
264
    public function whereNew() {
265
        return $this->where('NEW');
266
    }
267
268
    /**
269
     * @return WhereQuery
270
     * @throws InvalidWhereQueryCriteriaException
271
     */
272
    public function whereNot() {
273
        return $this->where('NOT');
274
    }
275
276
    /**
277
     * @return WhereQuery
278
     * @throws InvalidWhereQueryCriteriaException
279
     */
280
    public function whereOld() {
281
        return $this->where('OLD');
282
    }
283
284
    /**
285
     * @param mixed $value
286
     *
287
     * @return WhereQuery
288
     * @throws MessageSearchValidationException
289
     * @throws InvalidWhereQueryCriteriaException
290
     */
291
    public function whereOn($value) {
292
        $date = $this->parse_date($value);
293
        return $this->where('ON', $date);
294
    }
295
296
    /**
297
     * @return WhereQuery
298
     * @throws InvalidWhereQueryCriteriaException
299
     */
300
    public function whereRecent() {
301
        return $this->where('RECENT');
302
    }
303
304
    /**
305
     * @return WhereQuery
306
     * @throws InvalidWhereQueryCriteriaException
307
     */
308
    public function whereSeen() {
309
        return $this->where('SEEN');
310
    }
311
312
    /**
313
     * @param mixed $value
314
     *
315
     * @return WhereQuery
316
     * @throws MessageSearchValidationException
317
     * @throws InvalidWhereQueryCriteriaException
318
     */
319
    public function whereSince($value) {
320
        $date = $this->parse_date($value);
321
        return $this->where('SINCE', $date);
322
    }
323
324
    /**
325
     * @param string $value
326
     *
327
     * @return WhereQuery
328
     * @throws InvalidWhereQueryCriteriaException
329
     */
330
    public function whereSubject($value) {
331
        return $this->where('SUBJECT', $value);
332
    }
333
334
    /**
335
     * @param string $value
336
     *
337
     * @return WhereQuery
338
     * @throws InvalidWhereQueryCriteriaException
339
     */
340
    public function whereText($value) {
341
        return $this->where('TEXT', $value);
342
    }
343
344
    /**
345
     * @param string $value
346
     *
347
     * @return WhereQuery
348
     * @throws InvalidWhereQueryCriteriaException
349
     */
350
    public function whereTo($value) {
351
        return $this->where('TO', $value);
352
    }
353
354
    /**
355
     * @param string $value
356
     *
357
     * @return WhereQuery
358
     * @throws InvalidWhereQueryCriteriaException
359
     */
360
    public function whereUnkeyword($value) {
361
        return $this->where('UNKEYWORD', $value);
362
    }
363
364
    /**
365
     * @return WhereQuery
366
     * @throws InvalidWhereQueryCriteriaException
367
     */
368
    public function whereUnanswered() {
369
        return $this->where('UNANSWERED');
370
    }
371
372
    /**
373
     * @return WhereQuery
374
     * @throws InvalidWhereQueryCriteriaException
375
     */
376
    public function whereUndeleted() {
377
        return $this->where('UNDELETED');
378
    }
379
380
    /**
381
     * @return WhereQuery
382
     * @throws InvalidWhereQueryCriteriaException
383
     */
384
    public function whereUnflagged() {
385
        return $this->where('UNFLAGGED');
386
    }
387
388
    /**
389
     * @return WhereQuery
390
     * @throws InvalidWhereQueryCriteriaException
391
     */
392
    public function whereUnseen() {
393
        return $this->where('UNSEEN');
394
    }
395
396
    /**
397
     * @return WhereQuery
398
     * @throws InvalidWhereQueryCriteriaException
399
     */
400
    public function whereNoXSpam(){
401
        return $this->where("X-Spam-Flag NO");
402
    }
403
404
    /**
405
     * @return WhereQuery
406
     * @throws InvalidWhereQueryCriteriaException
407
     */
408
    public function whereIsXSpam(){
409
        return $this->where("X-Spam-Flag YES");
410
    }
411
412
    /**
413
     * @param $country_code
414
     *
415
     * @return WhereQuery
416
     * @throws InvalidWhereQueryCriteriaException
417
     */
418
    public function whereLanguage($country_code){
419
        return $this->where("Content-Language $country_code");
420
    }
421
}
422