forked from maciejczyzewski/bottomline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.php
62 lines (58 loc) · 2 KB
/
get.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace collections;
/**
* Get item of an array or object by index, accepting path (nested index).
*
* If `$collection` is an object that implements the ArrayAccess interface, this
* function will treat it as an array instead of accessing class properties.
*
* Use a period (`.`) in `$path` to go down a level in a multidimensional array.
*
* **Usage**
*
* ```php
* __::get(['foo' => ['bar' => 'ter']], 'foo.bar');
* ```
*
* **Result**
*
* ```
* 'ter'
* ```
*
* @param array|object $collection Array of values or object
* @param string $path Array key or object attribute. Use a period
* for depicting a new level in a multidimensional
* array
* @param mixed $default Default value to return if index not exist
*
* @return array|mixed|null
*/
function get($collection, $path, $default = null)
{
// TODO Make the algorithm recursive.
// TODO Factorize between object and array access (use a $getter function,
// as the $setter in __::set()).
if (\is_array($collection) && isset($collection[$path])) {
return $collection[$path];
}
foreach (\__::split($path, \__::DOT_NOTATION_DELIMITER) as $segment) {
if (\is_object($collection) && !($collection instanceof \ArrayAccess)) {
if (isset($collection->{$segment})) {
$collection = $collection->{$segment};
} else {
// TODO Remove Closure option: what is the point if it has no parameter:
// it will always yield the same value? KISS.
return $default && $default instanceof \Closure ? $default() : $default;
}
} else {
if (isset($collection[$segment])) {
$collection = $collection[$segment];
} else {
// TODO Same as above on Closure.
return $default && $default instanceof \Closure ? $default() : $default;
}
}
}
return $collection;
}