Passed
Push — master ( 6b0532...91a83b )
by Malte
02:21
created

WhereQuery::whereText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
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
use Webklex\IMAP\Exceptions\MethodNotFoundException;
17
18
/**
19
 * Class Query
20
 *
21
 * @package Webklex\IMAP\Query
22
 */
23
class WhereQuery extends Query {
24
25
    /**
26
     * @var array $available_criteria
27
     */
28
    protected $available_criteria = [
29
        'OR', 'AND',
30
        'ALL', 'ANSWERED', 'BCC', 'BEFORE', 'BODY', 'CC', 'DELETED', 'FLAGGED', 'FROM', 'KEYWORD',
31
        'NEW', 'OLD', 'ON', 'RECENT', 'SEEN', 'SINCE', 'SUBJECT', 'TEXT', 'TO',
32
        'UNANSWERED', 'UNDELETED', 'UNFLAGGED', 'UNKEYWORD', 'UNSEEN'
33
    ];
34
35
    /**
36
     * Magic method in order to allow alias usage of all "where" methods
37
     * @param string $name
38
     * @param array|null $arguments
39
     *
40
     * @return mixed
41
     * @throws MethodNotFoundException
42
     */
43
    public function __call($name, $arguments) {
44
        $method = 'where'.ucfirst($name);
45
        if(method_exists($this, $method) === true){
46
            return call_user_func_array([$this, $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

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