Completed
Push — master ( 69223a...a7228e )
by Restu
12:22
created

HelperArray   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 39
rs 10
wmc 7
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getVal() 0 18 3
A hasKey() 0 12 3
A isArray() 0 4 1
1
<?php
2
namespace JayaCode\Framework\Core\Helper;
3
4
class HelperArray
5
{
6
    public static function getVal($arr = array(), $key = "")
7
    {
8
        $val = $arr;
9
        $i = 0;
10
        $keys = explode(".", $key);
11
12
        while ($i < count($keys)) {
13
            if (!HelperArray::hasKey($val, $keys[$i])) {
14
                throw new \OutOfBoundsException("key ". $keys[$i] .", not found in the target array.");
15
            }
16
17
            $val = $val[$keys[$i]];
18
19
            $i++;
20
        }
21
22
        return $val;
23
    }
24
25
    public static function hasKey($arr = array(), $key = "")
26
    {
27
        if (!HelperArray::isArray($arr)) {
28
            return false;
29
        }
30
31
        if (!is_string($key)) {
32
            throw new \InvalidArgumentException("Argument 2 must be a string");
33
        }
34
35
        return isset($arr[$key]);
36
    }
37
38
    public static function isArray($arr = array())
39
    {
40
        return is_array($arr);
41
    }
42
}
43