Completed
Push — master ( 829fea...03fe8f )
by Dominik
9s
created

Helpers::accessArrayOrObject()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 4
nop 2
dl 0
loc 12
rs 8.2222
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt;
4
5
class Helpers
6
{
7
    public static function extractNestedDataFromArray($args = [])
8
    {
9
        if (count($args) < 2) {
10
            return self::returnEmptyStringOrFirstArgument($args);
11
        }
12
        $key = array_pop($args);
13
        $data = self::returnFirstArgumentOrNestedData($args);
14
        return self::accessArrayOrObject($key, $data);
15
    }
16
17
    protected static function returnEmptyStringOrFirstArgument($args = [])
18
    {
19
        if (count($args) === 0) {
20
            return '';
21
        }
22
        if (count($args) === 1) {
23
            return $args[0];
24
        }
25
    }
26
27
    protected static function returnFirstArgumentOrNestedData($args = [])
28
    {
29
        if (count($args) > 1) {
30
            return self::extractNestedDataFromArray($args);
31
        } else {
32
            return $args[0];
33
        }
34
    }
35
36
    protected static function accessArrayOrObject($key, $data)
37
    {
38
        $output = '';
39
        if (is_array($key) || is_object($key)) {
40
            $output = $key;
41
        } elseif (is_array($data) && array_key_exists($key, $data)) {
42
            $output = $data[$key];
43
        } elseif (is_object($data) && property_exists($data, $key)) {
44
            $output = $data->$key;
45
        }
46
        return $output;
47
    }
48
}
49