-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgetIn.php
More file actions
27 lines (24 loc) · 764 Bytes
/
getIn.php
File metadata and controls
27 lines (24 loc) · 764 Bytes
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
<?php
namespace Sergiors\Functional;
const getIn = __NAMESPACE__.'\getIn';
/**
* Returns the value in a nested associative structure,
* where $ks is a sequence of keys. Returns false if the key
* is not present, or the $notfound value if supplied.
*
* @author Sérgio Rafael Siqueira <sergio@inbep.com.br>
*
* @link https://clojuredocs.org/clojure.core/get-in
*
* @param array ...$args
*
* @return mixed
*/
function getIn(...$args)
{
return partial(function (array $xss, array $ks, $notfound = false) {
return ifElse(has(0), function (array $ks) use ($xss, $notfound) {
return ifElse('is_array', hold(getIn, _, tail($ks), $notfound), id)(get($xss, $ks[0], $notfound));
}, always($notfound))($ks);
})(...$args);
}