equip /
assist
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 | 9 | if (is_array($value)) { |
|
| 34 | 9 | 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 | * Get a list from an array. |
||
| 126 | * |
||
| 127 | * Similar to array_column(), with the following exceptions: |
||
| 128 | * |
||
| 129 | * 1. Only returns a list from a single column. |
||
| 130 | * 2. Works with iterator collections. |
||
| 131 | * 3. Works with object rows. |
||
| 132 | * |
||
| 133 | * @param array|Traversable $source |
||
| 134 | * @param string $column |
||
| 135 | * |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | function column($source, $column = null) |
||
| 139 | { |
||
| 140 | 2 | $values = []; |
|
| 141 | |||
| 142 | 2 | foreach ($source as $row) { |
|
| 143 | 2 | if (is_object($row)) { |
|
| 144 | 1 | if (isset($row->$column)) { |
|
| 145 | 1 | $values[] = $row->$column; |
|
| 146 | 1 | } |
|
| 147 | 1 | } else { |
|
| 148 | 1 | if (isset($row[$column])) { |
|
| 149 | 1 | $values[] = $row[$column]; |
|
| 150 | 1 | } |
|
| 151 | } |
||
| 152 | 2 | } |
|
| 153 | |||
| 154 | 2 | return $values; |
|
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Take the first value from an array. |
||
| 159 | * |
||
| 160 | * @param array $list |
||
| 161 | * |
||
| 162 | * @return mixed |
||
| 163 | */ |
||
| 164 | function head($list) |
||
| 165 | { |
||
| 166 | 1 | $list = to_array($list); |
|
| 167 | 1 | return array_shift($list); |
|
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Take the last value from an array. |
||
| 172 | * |
||
| 173 | * @param array $list |
||
| 174 | * |
||
| 175 | * @return mixed |
||
| 176 | */ |
||
| 177 | function tail($list) |
||
| 178 | { |
||
| 179 | 1 | $list = to_array($list); |
|
| 180 | 1 | return array_pop($list); |
|
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Index an collection by a key. |
||
| 185 | * |
||
| 186 | * Rows in the collection can be arrays or objects. |
||
| 187 | * |
||
| 188 | * @param array|Traversable $source |
||
| 189 | * |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | function index_by($source, $key) |
||
| 193 | { |
||
| 194 | 1 | $indexed = []; |
|
| 195 | |||
| 196 | 1 | foreach ($source as $row) { |
|
| 197 | 1 | if (is_object($row)) { |
|
| 198 | 1 | $indexed[$row->$key] = $row; |
|
| 199 | 1 | } else { |
|
| 200 | 1 | $indexed[$row[$key]] = $row; |
|
| 201 | } |
||
| 202 | 1 | } |
|
| 203 | |||
| 204 | 1 | return $indexed; |
|
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Search and replace keys in an array. |
||
| 209 | * Keys in $source that are not found in $keys_to_replace will be left as is. |
||
| 210 | * Keys in $keys_to_replace not found in $source will be ignored. |
||
| 211 | * |
||
| 212 | * @param array|Traversable $source |
||
| 213 | * @param array $keys_to_replace key/value pairs [search => replace] |
||
| 214 | * |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | function array_replace_keys($source, $keys_to_replace) |
||
|
0 ignored issues
–
show
|
|||
| 218 | { |
||
| 219 | 1 | $source = to_array($source); |
|
| 220 | |||
| 221 | //Only use keys that are present in source |
||
| 222 | 1 | $keys_to_replace = array_intersect_key($keys_to_replace, $source); |
|
| 223 | |||
| 224 | 1 | $keys = array_keys($source); |
|
| 225 | 1 | $replaced_keys = array_replace(array_combine($keys, $keys), $keys_to_replace); |
|
| 226 | |||
| 227 | 1 | return array_combine($replaced_keys, $source); |
|
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Type cast some values in an array. |
||
| 232 | * |
||
| 233 | * Only defined values will be typed. |
||
| 234 | * |
||
| 235 | * @param array|Traversable $source |
||
| 236 | * @param array $types |
||
| 237 | * |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | function typed($source, array $types) |
||
| 241 | { |
||
| 242 | 1 | $source = to_array($source); |
|
| 243 | 1 | $values = array_intersect_key($source, $types); |
|
| 244 | |||
| 245 | 1 | foreach ($values as $key => $value) { |
|
| 246 | 1 | if ($value !== null) { |
|
| 247 | 1 | settype($values[$key], $types[$key]); |
|
| 248 | 1 | } |
|
| 249 | 1 | } |
|
| 250 | |||
| 251 | 1 | return array_replace($source, $values); |
|
| 252 | } |
||
| 253 |
Learn more about camelCase.