|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Caridea |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
|
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
|
8
|
|
|
* the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
15
|
|
|
* License for the specific language governing permissions and limitations under |
|
16
|
|
|
* the License. |
|
17
|
|
|
* |
|
18
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
|
19
|
|
|
* @license Apache-2.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
namespace Caridea\Filter; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Go get her, Ray! |
|
25
|
|
|
* |
|
26
|
|
|
* This class contains some array-related filters. |
|
27
|
|
|
*/ |
|
28
|
|
|
class Arrays |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* Coerces a value into an array or throws an exception. |
|
32
|
|
|
* |
|
33
|
|
|
* @param mixed $var - The value to coerce |
|
34
|
|
|
* @return array The converted array |
|
35
|
|
|
* @throws \InvalidArgumentException if the value could not be coerced |
|
36
|
|
|
*/ |
|
37
|
3 |
|
public static function coerce($var): array |
|
38
|
|
|
{ |
|
39
|
3 |
|
if (is_array($var)) { |
|
40
|
1 |
|
return $var; |
|
41
|
3 |
|
} elseif ($var instanceof \Traversable) { |
|
42
|
2 |
|
return iterator_to_array($var, true); |
|
43
|
2 |
|
} elseif ($var === null || is_scalar($var)) { |
|
44
|
1 |
|
return (array) $var; |
|
45
|
|
|
} |
|
46
|
1 |
|
throw new \InvalidArgumentException("Could not convert to array: " . gettype($var)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Creates a new regular expression split filter. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $pattern The search pattern |
|
53
|
|
|
* @return \Closure The created filter |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function split(string $pattern): \Closure |
|
56
|
|
|
{ |
|
57
|
1 |
|
return function ($value) use ($pattern) { |
|
58
|
1 |
|
return preg_split($pattern, Strings::coerce($value)); |
|
59
|
1 |
|
}; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Creates a new explode filter. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $needle The search string |
|
66
|
|
|
* @return \Closure The created filter |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function explode(string $needle): \Closure |
|
69
|
|
|
{ |
|
70
|
1 |
|
return function ($value) use ($needle) { |
|
71
|
1 |
|
return explode($needle, Strings::coerce($value)); |
|
72
|
1 |
|
}; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Creates a new array slice filter. |
|
77
|
|
|
* |
|
78
|
|
|
* @param int $length The maximum array length |
|
79
|
|
|
* @return \Closure The created filter |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function slice(int $length): \Closure |
|
82
|
|
|
{ |
|
83
|
1 |
|
return function ($value) use ($length) { |
|
84
|
1 |
|
return array_slice(Arrays::coerce($value), 0, $length); |
|
85
|
1 |
|
}; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Creates a new implode filter. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $joiner The glue string |
|
92
|
|
|
* @return \Closure The created filter |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function join(string $joiner): \Closure |
|
95
|
|
|
{ |
|
96
|
2 |
|
return function ($value) use ($joiner) { |
|
97
|
2 |
|
return implode($joiner, Arrays::coerce($value)); |
|
98
|
2 |
|
}; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|