1 | <?php |
||
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 |
||
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 |
||
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 |
||
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 |
||
100 | } |
||
101 |