Passed
Push — master ( 1d0ea2...0f9d68 )
by Kacper
05:34
created

ArrayHelper::column()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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