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
|
6 |
|
public function __construct(string $param, string $operator, $value) |
46
|
|
|
{ |
47
|
6 |
|
$this->param = $param; |
48
|
6 |
|
$this->operator = strtolower(trim($operator)); |
49
|
6 |
|
if (!array_key_exists($this->operator, Filter::$allowedOperators)) { |
50
|
1 |
|
throw new InvalidParamException(sprintf('Invalid operator "%s". Only one of "%s" is allowed', $this->operator, implode(',', array_keys(Filter::$allowedOperators)))); |
51
|
|
|
} |
52
|
5 |
|
$this->sqlOperator = Filter::$allowedOperators[$operator]; |
53
|
5 |
|
$this->value = $value; |
54
|
5 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
1 |
|
public function isWhere(): bool |
60
|
|
|
{ |
61
|
1 |
|
return in_array($this->operator, [ |
62
|
|
|
Filter::PARAM_EQ, |
63
|
|
|
Filter::PARAM_GT, |
64
|
|
|
Filter::PARAM_GTE, |
65
|
|
|
Filter::PARAM_LT, |
66
|
|
|
Filter::PARAM_LTE, |
67
|
|
|
], true); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
1 |
|
public function isWhereIn(): bool |
74
|
|
|
{ |
75
|
1 |
|
return Filter::PARAM_IN === $this->operator; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
1 |
|
public function isWhereLike(): bool |
82
|
|
|
{ |
83
|
1 |
|
return Filter::PARAM_LIKE === $this->operator; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
1 |
|
public function isWhereLikeLeft(): bool |
90
|
|
|
{ |
91
|
1 |
|
return Filter::PARAM_LIKE_LEFT === $this->operator; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
1 |
|
public function isWhereLikeRight(): bool |
98
|
|
|
{ |
99
|
1 |
|
return Filter::PARAM_LIKE_RIGHT === $this->operator; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
1 |
|
public function isWhereNotIn(): bool |
106
|
|
|
{ |
107
|
1 |
|
return Filter::PARAM_NOTIN === $this->operator; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
2 |
|
public function isWhereNotNull(): bool |
114
|
|
|
{ |
115
|
2 |
|
return Filter::PARAM_NOTNULL === $this->operator; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
2 |
|
public function isWhereNull(): bool |
122
|
|
|
{ |
123
|
2 |
|
return Filter::PARAM_NULL === $this->operator; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
1 |
|
public function isWhereNot(): bool |
130
|
|
|
{ |
131
|
1 |
|
return Filter::PARAM_NOT === $this->operator; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
1 |
|
public function isWhereRegexp(): bool |
138
|
|
|
{ |
139
|
1 |
|
return Filter::PARAM_REGEXP === $this->operator; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|