1 | <?php |
||
2 | |||
3 | namespace Endeavors\Components\Routing; |
||
4 | |||
5 | use ArrayAccess; |
||
6 | |||
7 | class Arr |
||
8 | { |
||
9 | /** |
||
10 | * Determine whether the given value is array accessible. |
||
11 | * |
||
12 | * @param mixed $value |
||
13 | * @return bool |
||
14 | */ |
||
15 | public static function accessible($value) |
||
16 | { |
||
17 | return is_array($value) || $value instanceof ArrayAccess; |
||
18 | } |
||
19 | |||
20 | /** |
||
21 | * Get an item from an array using "dot" notation. |
||
22 | * |
||
23 | * @param \ArrayAccess|array $array |
||
24 | * @param string $key |
||
25 | * @param mixed $default |
||
26 | * @return mixed |
||
27 | */ |
||
28 | public static function get($array, $key, $default = null) |
||
29 | { |
||
30 | if (!static::accessible($array)) { |
||
31 | return value($default); |
||
32 | } |
||
33 | |||
34 | if (null === $key) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
35 | return $array; |
||
36 | } |
||
37 | |||
38 | if (static::exists($array, $key)) { |
||
39 | return $array[$key]; |
||
40 | } |
||
41 | |||
42 | foreach (explode('.', $key) as $segment) { |
||
43 | if (static::accessible($array) && static::exists($array, $segment)) { |
||
44 | $array = $array[$segment]; |
||
45 | } else { |
||
46 | return value($default); |
||
47 | } |
||
48 | } |
||
49 | |||
50 | return $array; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Determine if the given key exists in the provided array. |
||
55 | * |
||
56 | * @param \ArrayAccess|array $array |
||
57 | * @param string|int $key |
||
58 | * @return bool |
||
59 | */ |
||
60 | public static function exists($array, $key) |
||
61 | { |
||
62 | if ($array instanceof ArrayAccess) { |
||
63 | return $array->offsetExists($key); |
||
64 | } |
||
65 | |||
66 | return array_key_exists($key, $array); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Get all of the given array except for a specified array of items. |
||
71 | * |
||
72 | * @param array $array |
||
73 | * @param array|string $keys |
||
74 | * @return array |
||
75 | */ |
||
76 | public static function except($array, $keys) |
||
77 | { |
||
78 | static::forget($array, $keys); |
||
79 | |||
80 | return $array; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Remove one or many array items from a given array using "dot" notation. |
||
85 | * |
||
86 | * @param array $array |
||
87 | * @param array|string $keys |
||
88 | * @return void |
||
89 | */ |
||
90 | public static function forget(&$array, $keys) |
||
91 | { |
||
92 | $original = &$array; |
||
93 | |||
94 | $keys = (array) $keys; |
||
95 | |||
96 | if (count($keys) === 0) { |
||
97 | return; |
||
98 | } |
||
99 | |||
100 | foreach ($keys as $key) { |
||
101 | // if the exact key exists in the top-level, remove it |
||
102 | if (static::exists($array, $key)) { |
||
103 | unset($array[$key]); |
||
104 | |||
105 | continue; |
||
106 | } |
||
107 | |||
108 | $parts = explode('.', $key); |
||
109 | |||
110 | // clean up before each pass |
||
111 | $array = &$original; |
||
112 | |||
113 | while (count($parts) > 1) { |
||
114 | $part = array_shift($parts); |
||
115 | |||
116 | if (isset($array[$part]) && is_array($array[$part])) { |
||
117 | $array = &$array[$part]; |
||
118 | } else { |
||
119 | continue 2; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | unset($array[array_shift($parts)]); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * If the given value is not an array and not null, wrap it in one. |
||
129 | * |
||
130 | * @param mixed $value |
||
131 | * @return array |
||
132 | */ |
||
133 | public static function wrap($value) |
||
134 | { |
||
135 | if (is_null($value)) { |
||
136 | return []; |
||
137 | } |
||
138 | |||
139 | return !is_array($value) ? [$value] : $value; |
||
140 | } |
||
141 | } |