Completed
Push — master ( 46524c...9d1592 )
by Sérgio
07:33
created

getin.php ➔ getin()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 10
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 20
ccs 8
cts 9
cp 0.8889
crap 3.0123
rs 9.4285
1
<?php
2
3
namespace Sergiors\Functional;
4
5
/**
6
 * Returns the value in a nested associative structure,
7
 * where $ks is a sequence of keys. Returns false if the key
8
 * is not present, or the $notfound value if supplied.
9
 *
10
 * @author Sérgio Rafael Siqueira <[email protected]>
11
 *
12
 * @link https://clojuredocs.org/clojure.core/get-in
13
 *
14
 * @return mixed
15
 */
16
function getin()
17
{
18 1
    $args = func_get_args();
19
20
    $getin = function (array $xs, array $ks, $notfound = false) {
21 1
        if (false === isset($ks[0])) {
22
            return $xs;
23
        }
24
25 1
        $x = get($xs, $ks[0], $notfound);
26
27 1
        if (is_array($x)) {
28 1
            return getin($x, array_slice($ks, 1), $notfound);
29
        }
30
31 1
        return $x;
32 1
    };
33
34 1
    return call_user_func_array(partial($getin), $args);
35
}
36