1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Equip\Arr; |
4
|
|
|
|
5
|
|
|
use Traversable; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Check if a key exists in an array. |
9
|
|
|
* |
10
|
|
|
* @param array|Traversable $array |
11
|
|
|
* @param string|integer $key |
12
|
|
|
* |
13
|
|
|
* @return boolean |
14
|
|
|
*/ |
15
|
|
|
function exists($array, $key) |
16
|
|
|
{ |
17
|
1 |
|
return array_key_exists($key, to_array($array)); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Convert a value to an array. |
22
|
|
|
* |
23
|
|
|
* If the value is an array it will be returned. |
24
|
|
|
* If the value is Traversable it will be converted to an array. |
25
|
|
|
* If the value is anything else it will be cast to an array. |
26
|
|
|
* |
27
|
|
|
* @param mixed $value |
28
|
|
|
* |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
function to_array($value) |
|
|
|
|
32
|
|
|
{ |
33
|
7 |
|
if (is_array($value)) { |
34
|
7 |
|
return $value; |
35
|
|
|
} |
36
|
|
|
|
37
|
4 |
|
if ($value instanceof Traversable) { |
38
|
1 |
|
return iterator_to_array($value); |
39
|
|
|
} |
40
|
|
|
|
41
|
4 |
|
return (array) $value; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get a single value from an array. |
46
|
|
|
* |
47
|
|
|
* If the value does not exist, the default will be returned. |
48
|
|
|
* |
49
|
|
|
* @param array|Traversable $source |
50
|
|
|
* @param string|integer $key |
51
|
|
|
* @param mixed $default |
52
|
|
|
* |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
function get($source, $key, $default = null) |
56
|
|
|
{ |
57
|
1 |
|
if (exists($source, $key)) { |
58
|
1 |
|
return $source[$key]; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
return $default; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Grab some values from an array. |
66
|
|
|
* |
67
|
|
|
* @param array|Traversable $source |
68
|
|
|
* @param array|string $keys |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
function some($source, $keys) |
73
|
|
|
{ |
74
|
1 |
|
return array_intersect_key( |
75
|
1 |
|
to_array($source), |
76
|
1 |
|
array_flip(to_array($keys)) |
77
|
1 |
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Exclude some values from an array. |
82
|
|
|
* |
83
|
|
|
* This is the inverse of some(). |
84
|
|
|
* |
85
|
|
|
* @param array|Traversable $source |
86
|
|
|
* @param array|string $keys |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
function without($source, $keys) |
91
|
|
|
{ |
92
|
1 |
|
return array_diff_key( |
93
|
1 |
|
to_array($source), |
94
|
1 |
|
array_flip(to_array($keys)) |
95
|
1 |
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Grab some values from an array, ensuring that all keys are defined. |
100
|
|
|
* |
101
|
|
|
* If a given key does not exist, the default value will be set. |
102
|
|
|
* |
103
|
|
|
* @param array|Traversable $source |
104
|
|
|
* @param array|string $keys |
105
|
|
|
* @param mixed $default |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
function expect($source, $keys, $default = null) |
110
|
|
|
{ |
111
|
1 |
|
$defaults = array_fill_keys( |
112
|
1 |
|
to_array($keys), |
113
|
|
|
$default |
114
|
1 |
|
); |
115
|
|
|
|
116
|
1 |
|
$source = to_array($source); |
117
|
|
|
|
118
|
1 |
|
return array_replace( |
119
|
1 |
|
array_intersect_key($source, $defaults), |
120
|
1 |
|
array_diff_key($defaults, $source) |
121
|
1 |
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Take the first value from an array. |
126
|
|
|
* |
127
|
|
|
* @param array $list |
128
|
|
|
* |
129
|
|
|
* @return mixed |
130
|
|
|
*/ |
131
|
|
|
function head($list) |
132
|
|
|
{ |
133
|
1 |
|
$list = to_array($list); |
134
|
1 |
|
return array_shift($list); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Take the last value from an array. |
139
|
|
|
* |
140
|
|
|
* @param array $list |
141
|
|
|
* |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
|
|
function tail($list) |
145
|
|
|
{ |
146
|
1 |
|
$list = to_array($list); |
147
|
1 |
|
return array_pop($list); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Index an collection by a key. |
152
|
|
|
* |
153
|
|
|
* Rows in the collection can be arrays or objects. |
154
|
|
|
* |
155
|
|
|
* @param array|Traversable $source |
156
|
|
|
* |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
function index_by($source, $key) |
|
|
|
|
160
|
|
|
{ |
161
|
1 |
|
$indexed = []; |
162
|
|
|
|
163
|
1 |
|
foreach ($source as $row) { |
164
|
1 |
|
if (is_object($row)) { |
165
|
1 |
|
$indexed[$row->$key] = $row; |
166
|
1 |
|
} else { |
167
|
1 |
|
$indexed[$row[$key]] = $row; |
168
|
|
|
} |
169
|
1 |
|
} |
170
|
|
|
|
171
|
1 |
|
return $indexed; |
172
|
|
|
} |
173
|
|
|
|
Learn more about camelCase.