ArrayHelper   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 17
c 1
b 0
f 0
dl 0
loc 34
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getColumn() 0 8 2
B getValue() 0 22 10
1
<?php
2
/**
3
 * Created for IG Client.
4
 * User: jakim <[email protected]>
5
 * Date: 14.03.2018
6
 */
7
8
namespace Jakim\Helper;
9
10
11
class ArrayHelper
12
{
13
    public static function getValue($array, $key, $default = null)
14
    {
15
        if ($key instanceof \Closure) {
16
            return $key($array, $default);
17
        }
18
19
        if (is_array($array) && (isset($array[$key]) || array_key_exists($key, $array))) {
20
            return $array[$key];
21
        }
22
23
        if (($pos = strrpos($key, '.')) !== false) {
24
            $array = static::getValue($array, substr($key, 0, $pos), $default);
25
            $key = substr($key, $pos + 1);
26
        }
27
28
        if (is_object($array)) {
29
            return $array->$key;
30
        } elseif (is_array($array)) {
31
            return (isset($array[$key]) || array_key_exists($key, $array)) ? $array[$key] : $default;
32
        }
33
34
        return $default;
35
    }
36
37
    public static function getColumn($array, $name)
38
    {
39
        $result = [];
40
        foreach ($array as $k => $element) {
41
            $result[$k] = static::getValue($element, $name);
42
        }
43
44
        return $result;
45
    }
46
}