Passed
Branch fuck-54 (abba45)
by Kacper
02:46
created

ArrayHelper::resolve()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 7
nc 2
nop 3
crap 3
1
<?php
2
/**
3
 * Highlighter
4
 *1
5
 * Copyright (C) 2015, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Highlighter\Utils;
17
18
19
class ArrayHelper
20
{
21 1
    public static function rearrange(array $array, array $keys)
22
    {
23
        return array_combine($keys, array_map(function ($key) use ($array) {
24 1
            return $array[$key];
25 1
        }, $keys));
26
    }
27
28
    public static function column(array $array, $index)
29
    {
30
        return array_map(function ($e) use ($index) { return $e[$index]; }, $array);
31
    }
32
33 2
    public static function find(array $array, callable $tester)
34
    {
35 2
        foreach ($array as $key => $value) {
36 2
            if ($tester($key, $value)) {
37 2
                return $key;
38
            }
39 1
        }
40
41 1
        return false;
42
    }
43
44 2
    public static function resolve(array $array, $key, $fallback = null) {
45
        do {
46 2
            if(isset($array[$key])) {
47 2
                return $array[$key];
48
            }
49
50 2
            $key = StringHelper::pop($key);
51 2
        } while (!empty($key));
52
53 1
        return $fallback;
54
    }
55
}