1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebComplete\core\condition; |
4
|
|
|
|
5
|
|
|
class Condition |
6
|
|
|
{ |
7
|
|
|
const EQUALS = 1; |
8
|
|
|
const NOT_EQUALS = 2; |
9
|
|
|
const LESS_THAN = 3; |
10
|
|
|
const GREATER_THAN = 4; |
11
|
|
|
const LESS_OR_EQUALS = 5; |
12
|
|
|
const GREATER_OR_EQUALS = 6; |
13
|
|
|
const BETWEEN = 7; |
14
|
|
|
const LIKE = 8; |
15
|
|
|
const IN = 9; |
16
|
|
|
|
17
|
|
|
protected $conditions = []; |
18
|
|
|
protected $sort = []; |
19
|
|
|
protected $offsetValue; |
20
|
|
|
protected $limitValue; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param array $conditions |
24
|
|
|
* @param string|null $sortField |
25
|
|
|
* @param int|null $sortDir |
26
|
|
|
* @param int|null $offset |
27
|
|
|
* @param int|null $limit |
28
|
|
|
*/ |
29
|
|
|
public function __construct( |
30
|
|
|
array $conditions = [], |
31
|
|
|
string $sortField = null, |
32
|
|
|
int $sortDir = null, |
33
|
|
|
int $offset = null, |
34
|
|
|
int $limit = null |
35
|
|
|
) { |
36
|
|
|
if ($conditions = $this->parseConditionsArg($conditions)) { |
37
|
|
|
$this->conditions = $conditions; |
38
|
|
|
} |
39
|
|
|
if ($sortField !== null && $sortDir !== null) { |
40
|
|
|
$this->addSort($sortField, $sortDir); |
41
|
|
|
} |
42
|
|
|
if ($offset !== null) { |
43
|
|
|
$this->offset($offset); |
44
|
|
|
} |
45
|
|
|
if ($limit !== null) { |
46
|
|
|
$this->limit($limit); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param $field |
52
|
|
|
* @param $value |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
|
|
public function addEqualsCondition(string $field, $value) |
56
|
|
|
{ |
57
|
|
|
$this->conditions[] = [self::EQUALS, $field, $value]; |
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $field |
63
|
|
|
* @param $value |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function addNotEqualsCondition(string $field, $value) |
67
|
|
|
{ |
68
|
|
|
$this->conditions[] = [self::NOT_EQUALS, $field, $value]; |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $field |
74
|
|
|
* @param $value |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
public function addLessThanCondition(string $field, $value) |
78
|
|
|
{ |
79
|
|
|
$this->conditions[] = [self::LESS_THAN, $field, $value]; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $field |
85
|
|
|
* @param $value |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function addGreaterThanCondition(string $field, $value) |
89
|
|
|
{ |
90
|
|
|
$this->conditions[] = [self::GREATER_THAN, $field, $value]; |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $field |
96
|
|
|
* @param $value |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function addLessOrEqualsCondition(string $field, $value) |
100
|
|
|
{ |
101
|
|
|
$this->conditions[] = [self::LESS_OR_EQUALS, $field, $value]; |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $field |
107
|
|
|
* @param $value |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function addGreaterOrEqualsCondition(string $field, $value) |
111
|
|
|
{ |
112
|
|
|
$this->conditions[] = [self::GREATER_OR_EQUALS, $field, $value]; |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param $field |
118
|
|
|
* @param $valueFrom |
119
|
|
|
* @param $valueTo |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
|
|
public function addBetweenCondition(string $field, $valueFrom, $valueTo) |
123
|
|
|
{ |
124
|
|
|
$this->conditions[] = [self::BETWEEN, $field, $valueFrom, $valueTo]; |
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $field |
130
|
|
|
* @param $value |
131
|
|
|
* @param bool $left |
132
|
|
|
* @param bool $right |
133
|
|
|
* |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
|
public function addLikeCondition(string $field, $value, $left = true, $right = true) |
137
|
|
|
{ |
138
|
|
|
$this->conditions[] = [self::LIKE, $field, $value, $left, $right]; |
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $field |
144
|
|
|
* @param array $array |
145
|
|
|
* @param bool $isNumeric |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
|
|
public function addInCondition(string $field, array $array, bool $isNumeric = false) |
149
|
|
|
{ |
150
|
|
|
$this->conditions[] = [self::IN, $field, $array, $isNumeric]; |
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $sortField |
156
|
|
|
* @param int $sortDir SORT FLAG (SORT_ASC, SORT_DESC) |
157
|
|
|
* @return $this |
158
|
|
|
*/ |
159
|
|
|
public function addSort(string $sortField, int $sortDir) |
160
|
|
|
{ |
161
|
|
|
$this->sort[$sortField] = $sortDir; |
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $sortField |
167
|
|
|
* @param int $sortDir |
168
|
|
|
* |
169
|
|
|
* @return $this |
170
|
|
|
*/ |
171
|
|
|
public function setSort(string $sortField, int $sortDir) |
172
|
|
|
{ |
173
|
|
|
$this->sort = []; |
174
|
|
|
return $this->addSort($sortField, $sortDir); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param $offset |
179
|
|
|
* @return $this |
180
|
|
|
*/ |
181
|
|
|
public function offset(int $offset) |
182
|
|
|
{ |
183
|
|
|
$this->offsetValue = $offset; |
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param $limit |
189
|
|
|
* @return $this |
190
|
|
|
*/ |
191
|
|
|
public function limit(int $limit) |
192
|
|
|
{ |
193
|
|
|
$this->limitValue = $limit; |
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return array |
199
|
|
|
*/ |
200
|
|
|
public function getConditions(): array |
201
|
|
|
{ |
202
|
|
|
return $this->conditions; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @return array |
207
|
|
|
*/ |
208
|
|
|
public function getSort(): array |
209
|
|
|
{ |
210
|
|
|
return $this->sort; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return null|int |
215
|
|
|
*/ |
216
|
|
|
public function getOffset() |
217
|
|
|
{ |
218
|
|
|
return $this->offsetValue; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return null|int |
223
|
|
|
*/ |
224
|
|
|
public function getLimit() |
225
|
|
|
{ |
226
|
|
|
return $this->limitValue; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param $conditions |
231
|
|
|
* @return array |
232
|
|
|
*/ |
233
|
|
|
protected function parseConditionsArg($conditions): array |
234
|
|
|
{ |
235
|
|
|
$result = []; |
236
|
|
|
if (\is_array($conditions)) { |
237
|
|
|
foreach ($conditions as $k => $v) { |
238
|
|
|
if (\is_numeric($k) && \is_array($v)) { |
239
|
|
|
$result[] = $v; |
240
|
|
|
} |
241
|
|
|
if (\is_string($k) && $v) { |
242
|
|
|
if (\is_scalar($v)) { |
243
|
|
|
$result[] = [self::EQUALS, $k, $v]; |
244
|
|
|
} elseif (\is_array($v)) { |
245
|
|
|
$result[] = [self::IN, $k, $v]; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
return $result; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|