-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDotFilteringTrait.php
More file actions
266 lines (230 loc) · 7.67 KB
/
Copy pathDotFilteringTrait.php
File metadata and controls
266 lines (230 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
namespace BinaryCube\DotArray;
/**
* DotFilteringTrait
*
* @package BinaryCube\DotArray
* @author Banciu N. Cristian Mihai <banciu.n.cristian.mihai@gmail.com>
* @license https://github.com/binary-cube/dot-array/blob/master/LICENSE <MIT License>
* @link https://github.com/binary-cube/dot-array
*/
trait DotFilteringTrait
{
/**
* List with internal operators and the associated callbacks.
*
* @return array
*/
protected static function operators()
{
return [
[
'tokens' => ['=', '==', 'eq'],
'closure' => function ($item, $property, $value) {
return $item[$property] == $value[0];
},
],
[
'tokens' => ['===', 'i'],
'closure' => function ($item, $property, $value) {
return $item[$property] === $value[0];
},
],
[
'tokens' => ['!=', 'ne'],
'closure' => function ($item, $property, $value) {
return $item[$property] != $value[0];
},
],
[
'tokens' => ['!==', 'ni'],
'closure' => function ($item, $property, $value) {
return $item[$property] !== $value[0];
},
],
[
'tokens' => ['<', 'lt'],
'closure' => function ($item, $property, $value) {
return $item[$property] < $value[0];
},
],
[
'tokens' => ['>', 'gt'],
'closure' => function ($item, $property, $value) {
return $item[$property] > $value[0];
},
],
[
'tokens' => ['<=', 'lte'],
'closure' => function ($item, $property, $value) {
return $item[$property] <= $value[0];
},
],
[
'tokens' => ['>=', 'gte'],
'closure' => function ($item, $property, $value) {
return $item[$property] >= $value[0];
},
],
[
'tokens' => ['in', 'contains'],
'closure' => function ($item, $property, $value) {
return \in_array($item[$property], (array) $value, true);
},
],
[
'tokens' => ['not-in', 'not-contains'],
'closure' => function ($item, $property, $value) {
return !\in_array($item[$property], (array) $value, true);
},
],
[
'tokens' => ['between'],
'closure' => function ($item, $property, $value) {
return ($item[$property] >= $value[0] && $item[$property] <= $value[1]);
},
],
[
'tokens' => ['not-between'],
'closure' => function ($item, $property, $value) {
return ($item[$property] < $value[0] || $item[$property] > $value[1]);
},
],
];
}
/**
* Find the first item in an array that passes the truth test, otherwise return false.
* The signature of the callable must be: `function ($value, $key)`
*
* @param \Closure $closure
*
* @return false|mixed
*/
public function find(\Closure $closure)
{
foreach ($this->items as $key => $value) {
if ($closure($value, $key)) {
if (\is_array($value)) {
$value = static::create($value);
}
return $value;
}
}
return false;
}
/**
* Use a callable function to filter through items.
* The signature of the callable must be: `function ($value, $key)`
*
* @param \Closure|null $closure
* @param int $flag Flag determining what arguments are sent to callback.
* ARRAY_FILTER_USE_KEY :: pass key as the only argument
* to callback. ARRAY_FILTER_USE_BOTH :: pass both value
* and key as arguments to callback.
*
* @return static
*/
public function filter(\Closure $closure = null, $flag = ARRAY_FILTER_USE_BOTH)
{
$items = $this->items;
if (!isset($closure)) {
return static::create($items);
}
return static::create(
\array_values(
\array_filter(
$items,
$closure,
$flag
)
)
);
}
/**
* Allow to filter an array using one of the following comparison operators:
* - [ =, ==, eq (equal) ]
* - [ ===, i (identical) ]
* - [ !=, ne (not equal) ]
* - [ !==, ni (not identical) ]
* - [ <, lt (less than) ]
* - [ >, gr (greater than) ]
* - [ <=, lte (less than or equal to) ]
* - [ =>, gte (greater than or equal to) ]
* - [ in, contains ]
* - [ not-in, not-contains ]
* - [ between ]
* - [ not-between ]
*
* @param string $property
* @param string $comparisonOperator
* @param mixed $value
*
* @return static
*/
public function filterBy($property, $comparisonOperator, $value)
{
$args = \func_get_args();
$value = \array_slice($args, 2, \count($args));
$closure = null;
$operators = static::operators();
if (isset($value[0]) && \is_array($value[0])) {
$value = $value[0];
}
foreach ($operators as $entry) {
if (\in_array($comparisonOperator, $entry['tokens'])) {
$closure = function ($item) use ($entry, $property, $value) {
$item = (array) $item;
if (!\array_key_exists($property, $item)) {
return false;
}
return $entry['closure']($item, $property, $value);
};
break;
}
}
return $this->filter($closure);
}
/**
* Filtering through array.
* The signature of the call can be:
* - where([property, comparisonOperator, ...value])
* - where(\Closure) :: The signature of the callable must be: `function ($value, $key)`
* - where([\Closure]) :: The signature of the callable must be: `function ($value, $key)`
*
* Allowed comparison operators:
* - [ =, ==, eq (equal) ]
* - [ ===, i (identical) ]
* - [ !=, ne (not equal) ]
* - [ !==, ni (not identical) ]
* - [ <, lt (less than) ]
* - [ >, gr (greater than) ]
* - [ <=, lte (less than or equal to) ]
* - [ =>, gte (greater than or equal to) ]
* - [ in, contains ]
* - [ not-in, not-contains ]
* - [ between ]
* - [ not-between ]
*
* @param array|callable $criteria
*
* @return static
*/
public function where($criteria)
{
$criteria = (array) $criteria;
if (empty($criteria)) {
return $this->filter();
}
$closure = \array_shift($criteria);
if ($closure instanceof \Closure) {
return $this->filter($closure);
}
$property = $closure;
$comparisonOperator = \array_shift($criteria);
$value = $criteria;
if (isset($value[0]) && \is_array($value[0])) {
$value = $value[0];
}
return $this->filterBy($property, $comparisonOperator, $value);
}
}