1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Marcin. |
4
|
|
|
* Date: 16.06.2018 |
5
|
|
|
* Time: 19:17 |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace Mrcnpdlk\Lib\UrlSearchParser\Criteria; |
10
|
|
|
|
11
|
|
|
use function in_array; |
12
|
|
|
use Mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class FilterParam |
16
|
|
|
*/ |
17
|
|
|
class FilterParam |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
public $param; |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
public $operator; |
27
|
|
|
/** |
28
|
|
|
* @var string|null |
29
|
|
|
*/ |
30
|
|
|
public $sqlOperator; |
31
|
|
|
/** |
32
|
|
|
* @var mixed |
33
|
|
|
*/ |
34
|
|
|
public $value; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* FilterParam constructor. |
38
|
|
|
* |
39
|
|
|
* @param string $param |
40
|
|
|
* @param string $operator |
41
|
|
|
* @param mixed $value |
42
|
|
|
* |
43
|
|
|
* @throws \Mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException |
44
|
|
|
*/ |
45
|
5 |
|
public function __construct(string $param, string $operator, $value) |
46
|
|
|
{ |
47
|
5 |
|
$this->param = $param; |
48
|
5 |
|
$this->operator = strtolower(trim($operator)); |
49
|
5 |
|
if (!array_key_exists($this->operator, Filter::$allowedOperators)) { |
50
|
1 |
|
throw new InvalidParamException( |
51
|
1 |
|
sprintf('Invalid operator "%s". Only one of "%s" is allowed', |
52
|
1 |
|
$this->operator, |
53
|
1 |
|
implode(',', array_keys(Filter::$allowedOperators)) |
54
|
|
|
) |
55
|
|
|
); |
56
|
|
|
} |
57
|
4 |
|
$this->sqlOperator = Filter::$allowedOperators[$operator]; |
58
|
4 |
|
$this->value = $value; |
59
|
4 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
1 |
|
public function isWhere(): bool |
65
|
|
|
{ |
66
|
1 |
|
return in_array($this->operator, [ |
67
|
|
|
Filter::PARAM_EQ, |
68
|
|
|
Filter::PARAM_GT, |
69
|
|
|
Filter::PARAM_GTE, |
70
|
|
|
Filter::PARAM_LT, |
71
|
|
|
Filter::PARAM_LTE, |
72
|
|
|
], true); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
1 |
|
public function isWhereIn(): bool |
79
|
|
|
{ |
80
|
1 |
|
return Filter::PARAM_IN === $this->operator; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
1 |
|
public function isWhereLike(): bool |
87
|
|
|
{ |
88
|
1 |
|
return Filter::PARAM_LIKE === $this->operator; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
1 |
|
public function isWhereNotIn(): bool |
95
|
|
|
{ |
96
|
1 |
|
return Filter::PARAM_NOTIN === $this->operator; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
2 |
|
public function isWhereNotNull(): bool |
103
|
|
|
{ |
104
|
2 |
|
return Filter::PARAM_NOTNULL === $this->operator; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
2 |
|
public function isWhereNull(): bool |
111
|
|
|
{ |
112
|
2 |
|
return Filter::PARAM_NULL === $this->operator; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
1 |
|
public function isWhereNot(): bool |
119
|
|
|
{ |
120
|
1 |
|
return Filter::PARAM_NOT === $this->operator; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|