Completed
Push — master ( 9803c0...824c84 )
by Fabien
03:56
created

Matcher::like()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 3
1
<?php
2
namespace Fab\Vidi\Persistence;
3
4
/**
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
/**
18
 * Matcher class for conditions that will apply to a query.
19
 */
20
class Matcher
21
{
22
23
    /**
24
     * The logical OR
25
     */
26
    const LOGICAL_OR = 'logicalOr';
27
28
    /**
29
     * The logical AND
30
     */
31
    const LOGICAL_AND = 'logicalAnd';
32
33
    /**
34
     * @var string
35
     */
36
    protected $dataType = '';
37
38
    /**
39
     * @var string
40
     */
41
    protected $searchTerm = '';
42
43
    /**
44
     * @var array
45
     */
46
    protected $supportedOperators = [
47
        '=' => 'equals',
48
        'in' => 'in',
49
        'like' => 'like',
50
        '>' => 'greaterThan',
51
        '>=' => 'greaterThanOrEqual',
52
        '<' => 'lessThan',
53
        '<=' => 'lessThanOrEqual'
54
    ];
55
56
    /**
57
     * @var array
58
     */
59
    protected $equals = [];
60
61
    /**
62
     * @var array
63
     */
64
    protected $greaterThan = [];
65
66
    /**
67
     * @var array
68
     */
69
    protected $greaterThanOrEqual = [];
70
71
    /**
72
     * @var array
73
     */
74
    protected $lessThan = [];
75
76
    /**
77
     * @var array
78
     */
79
    protected $lessThanOrEqual = [];
80
81
    /**
82
     * @var array
83
     */
84
    protected $in = [];
85
86
    /**
87
     * @var array
88
     */
89
    protected $like = [];
90
91
    /**
92
     * @var string
93
     */
94
    protected $defaultLogicalSeparator = self::LOGICAL_AND;
95
96
    /**
97
     * @var string
98
     */
99
    protected $logicalSeparatorForEquals = self::LOGICAL_AND;
100
101
    /**
102
     * @var string
103
     */
104
    protected $logicalSeparatorForGreaterThan = self::LOGICAL_AND;
105
106
    /**
107
     * @var string
108
     */
109
    protected $logicalSeparatorForGreaterThanOrEqual = self::LOGICAL_AND;
110
111
    /**
112
     * @var string
113
     */
114
    protected $logicalSeparatorForLessThan = self::LOGICAL_AND;
115
116
    /**
117
     * @var string
118
     */
119
    protected $logicalSeparatorForLessThanOrEqual = self::LOGICAL_AND;
120
121
    /**
122
     * @var string
123
     */
124
    protected $logicalSeparatorForIn = self::LOGICAL_AND;
125
126
    /**
127
     * @var string
128
     */
129
    protected $logicalSeparatorForLike = self::LOGICAL_AND;
130
131
    /**
132
     * @var string
133
     */
134
    protected $logicalSeparatorForSearchTerm = self::LOGICAL_OR;
135
136
    /**
137
     * Constructs a new Matcher
138
     *
139
     * @param array $matches associative [$field => $value]
140
     * @param string $dataType which corresponds to an entry of the TCA (table name).
141
     * @return \Fab\Vidi\Persistence\Matcher
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
142
     */
143
    public function __construct($matches = [], $dataType = '')
144
    {
145
        $this->dataType = $dataType;
146
        $this->matches = $matches;
0 ignored issues
show
Bug introduced by
The property matches does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
147
    }
148
149
    /**
150
     * @param string $searchTerm
151
     * @return \Fab\Vidi\Persistence\Matcher
152
     */
153
    public function setSearchTerm($searchTerm)
154
    {
155
        $this->searchTerm = $searchTerm;
156
        return $this;
157
    }
158
159
    /**
160
     * @return string
161
     */
162
    public function getSearchTerm()
163
    {
164
        return $this->searchTerm;
165
    }
166
167
    /**
168
     * @return array
169
     */
170
    public function getEquals()
171
    {
172
        return $this->equals;
173
    }
174
175
    /**
176
     * @param $fieldNameAndPath
177
     * @param $operand
178
     * @return $this
179
     */
180
    public function equals($fieldNameAndPath, $operand)
181
    {
182
        $this->equals[] = ['fieldNameAndPath' => $fieldNameAndPath, 'operand' => $operand];
183
        return $this;
184
    }
185
186
    /**
187
     * @return array
188
     */
189
    public function getGreaterThan()
190
    {
191
        return $this->greaterThan;
192
    }
193
194
    /**
195
     * @param $fieldNameAndPath
196
     * @param $operand
197
     * @return $this
198
     */
199
    public function greaterThan($fieldNameAndPath, $operand)
200
    {
201
        $this->greaterThan[] = ['fieldNameAndPath' => $fieldNameAndPath, 'operand' => $operand];
202
        return $this;
203
    }
204
205
    /**
206
     * @return array
207
     */
208
    public function getGreaterThanOrEqual()
209
    {
210
        return $this->greaterThanOrEqual;
211
    }
212
213
    /**
214
     * @param $fieldNameAndPath
215
     * @param $operand
216
     * @return $this
217
     */
218
    public function greaterThanOrEqual($fieldNameAndPath, $operand)
219
    {
220
        $this->greaterThanOrEqual[] = ['fieldNameAndPath' => $fieldNameAndPath, 'operand' => $operand];
221
        return $this;
222
    }
223
224
    /**
225
     * @return array
226
     */
227
    public function getLessThan()
228
    {
229
        return $this->lessThan;
230
    }
231
232
    /**
233
     * @param $fieldNameAndPath
234
     * @param $operand
235
     * @return $this
236
     */
237
    public function lessThan($fieldNameAndPath, $operand)
238
    {
239
        $this->lessThan[] = ['fieldNameAndPath' => $fieldNameAndPath, 'operand' => $operand];
240
        return $this;
241
    }
242
243
    /**
244
     * @return array
245
     */
246
    public function getLessThanOrEqual()
247
    {
248
        return $this->lessThanOrEqual;
249
    }
250
251
    /**
252
     * @param $fieldNameAndPath
253
     * @param $operand
254
     * @return $this
255
     */
256
    public function lessThanOrEqual($fieldNameAndPath, $operand)
257
    {
258
        $this->lessThanOrEqual[] = ['fieldNameAndPath' => $fieldNameAndPath, 'operand' => $operand];
259
        return $this;
260
    }
261
262
    /**
263
     * @return array
264
     */
265
    public function getLike()
266
    {
267
        return $this->like;
268
    }
269
270
    /**
271
     * @param $fieldNameAndPath
272
     * @param $operand
273
     * @return $this
274
     */
275
    public function in($fieldNameAndPath, $operand)
276
    {
277
        $this->in[] = ['fieldNameAndPath' => $fieldNameAndPath, 'operand' => $operand];
278
        return $this;
279
    }
280
281
    /**
282
     * @return array
283
     */
284
    public function getIn()
285
    {
286
        return $this->in;
287
    }
288
289
    /**
290
     * @param $fieldNameAndPath
291
     * @param $operand
292
     * @param bool $addWildCard
293
     * @return $this
294
     */
295
    public function like($fieldNameAndPath, $operand, $addWildCard = TRUE)
296
    {
297
        $wildCardSymbol = $addWildCard ? '%' : '';
298
        $this->like[] = ['fieldNameAndPath' => $fieldNameAndPath, 'operand' => $wildCardSymbol . $operand . $wildCardSymbol];
299
        return $this;
300
    }
301
302
    /**
303
     * @return array
304
     */
305
    public function getDefaultLogicalSeparator()
306
    {
307
        return $this->defaultLogicalSeparator;
308
    }
309
310
    /**
311
     * @param string $defaultLogicalSeparator
312
     * @return $this
313
     */
314
    public function setDefaultLogicalSeparator($defaultLogicalSeparator)
315
    {
316
        $this->defaultLogicalSeparator = $defaultLogicalSeparator;
317
        return $this;
318
    }
319
320
    /**
321
     * @return string
322
     */
323
    public function getLogicalSeparatorForEquals()
324
    {
325
        return $this->logicalSeparatorForEquals;
326
    }
327
328
    /**
329
     * @param string $logicalSeparatorForEquals
330
     * @return $this
331
     */
332
    public function setLogicalSeparatorForEquals($logicalSeparatorForEquals)
333
    {
334
        $this->logicalSeparatorForEquals = $logicalSeparatorForEquals;
335
        return $this;
336
    }
337
338
    /**
339
     * @return string
340
     */
341
    public function getLogicalSeparatorForGreaterThan()
342
    {
343
        return $this->logicalSeparatorForGreaterThan;
344
    }
345
346
    /**
347
     * @param string $logicalSeparatorForGreaterThan
348
     * @return $this
349
     */
350
    public function setLogicalSeparatorForGreaterThan($logicalSeparatorForGreaterThan)
351
    {
352
        $this->logicalSeparatorForGreaterThan = $logicalSeparatorForGreaterThan;
353
        return $this;
354
    }
355
356
    /**
357
     * @return string
358
     */
359
    public function getLogicalSeparatorForGreaterThanOrEqual()
360
    {
361
        return $this->logicalSeparatorForGreaterThanOrEqual;
362
    }
363
364
    /**
365
     * @param string $logicalSeparatorForGreaterThanOrEqual
366
     * @return $this
367
     */
368
    public function setLogicalSeparatorForGreaterThanOrEqual($logicalSeparatorForGreaterThanOrEqual)
369
    {
370
        $this->logicalSeparatorForGreaterThanOrEqual = $logicalSeparatorForGreaterThanOrEqual;
371
        return $this;
372
    }
373
374
    /**
375
     * @return string
376
     */
377
    public function getLogicalSeparatorForLessThan()
378
    {
379
        return $this->logicalSeparatorForLessThan;
380
    }
381
382
    /**
383
     * @param string $logicalSeparatorForLessThan
384
     * @return $this
385
     */
386
    public function setLogicalSeparatorForLessThan($logicalSeparatorForLessThan)
387
    {
388
        $this->logicalSeparatorForLessThan = $logicalSeparatorForLessThan;
389
        return $this;
390
    }
391
392
    /**
393
     * @return string
394
     */
395
    public function getLogicalSeparatorForLessThanOrEqual()
396
    {
397
        return $this->logicalSeparatorForLessThanOrEqual;
398
    }
399
400
    /**
401
     * @param string $logicalSeparatorForLessThanOrEqual
402
     * @return $this
403
     */
404
    public function setLogicalSeparatorForLessThanOrEqual($logicalSeparatorForLessThanOrEqual)
405
    {
406
        $this->logicalSeparatorForLessThanOrEqual = $logicalSeparatorForLessThanOrEqual;
407
        return $this;
408
    }
409
410
    /**
411
     * @return string
412
     */
413
    public function getLogicalSeparatorForIn()
414
    {
415
        return $this->logicalSeparatorForIn;
416
    }
417
418
    /**
419
     * @param string $logicalSeparatorForIn
420
     * @return $this
421
     */
422
    public function setLogicalSeparatorForIn($logicalSeparatorForIn)
423
    {
424
        $this->logicalSeparatorForIn = $logicalSeparatorForIn;
425
        return $this;
426
    }
427
428
    /**
429
     * @return string
430
     */
431
    public function getLogicalSeparatorForLike()
432
    {
433
        return $this->logicalSeparatorForLike;
434
    }
435
436
    /**
437
     * @param string $logicalSeparatorForLike
438
     * @return $this
439
     */
440
    public function setLogicalSeparatorForLike($logicalSeparatorForLike)
441
    {
442
        $this->logicalSeparatorForLike = $logicalSeparatorForLike;
443
        return $this;
444
    }
445
446
    /**
447
     * @return string
448
     */
449
    public function getLogicalSeparatorForSearchTerm()
450
    {
451
        return $this->logicalSeparatorForSearchTerm;
452
    }
453
454
    /**
455
     * @param string $logicalSeparatorForSearchTerm
456
     * @return $this
457
     */
458
    public function setLogicalSeparatorForSearchTerm($logicalSeparatorForSearchTerm)
459
    {
460
        $this->logicalSeparatorForSearchTerm = $logicalSeparatorForSearchTerm;
461
        return $this;
462
    }
463
464
    /**
465
     * @return array
466
     */
467
    public function getSupportedOperators()
468
    {
469
        return $this->supportedOperators;
470
    }
471
472
    /**
473
     * @return string
474
     */
475
    public function getDataType()
476
    {
477
        return $this->dataType;
478
    }
479
480
    /**
481
     * @param string $dataType
482
     * @return $this
483
     */
484
    public function setDataType($dataType)
485
    {
486
        $this->dataType = $dataType;
487
        return $this;
488
    }
489
}
490