Passed
Push — master ( a2014d...22f24f )
by Kacper
03:29
created

ArrayHelper::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * Highlighter
4
 *1
5
 * Copyright (C) 2016, 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
class ArrayHelper
19
{
20 1
    public static function rearrange(array $array, array $keys)
21
    {
22
        return array_combine($keys, array_map(function ($key) use ($array) {
23 1
            return $array[$key];
24 1
        }, $keys));
25
    }
26
27
    public static function column(array $array, $index)
28
    {
29
        return array_map(function ($e) use ($index) { return $e[$index]; }, $array);
30
    }
31
32 1
    public static function find(array $array, callable $tester)
33
    {
34 1
        foreach ($array as $key => $value) {
35 1
            if ($tester($key, $value)) {
36 1
                return $key;
37
            }
38 1
        }
39
40 1
        return false;
41
    }
42
43 3
    public static function resolve(array $array, $key, $fallback = null)
44
    {
45
        do {
46 3
            if (isset($array[$key])) {
47 3
                return $array[$key];
48
            }
49
50 2
            $key = StringHelper::pop($key);
51 2
        } while (!empty($key));
52
53 1
        return $fallback;
54
    }
55
56
    public static function get(array $array, $key, $fallback = null)
57
    {
58
        return isset($array[$key]) ? $array[$key] : $fallback;
59
    }
60
}
61