Completed
Push — master ( abdd5c...4a1452 )
by Sergey
08:14 queued 05:47
created

functions.php ➔ get_array_data()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 3
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace seregazhuk;
4
5
if (! function_exists('class_uses_recursive')) {
6
    /**
7
     * Returns all traits used by a class, its subclasses and trait of their traits.
8
     *
9
     * @param  object|string  $class
10
     * @return array
11
     */
12
    function class_uses_recursive($class)
13
    {
14
        if (is_object($class)) {
15
            $class = get_class($class);
16
        }
17
        $results = [];
18
        foreach (array_merge([$class => $class], class_parents($class)) as $class) {
19
            $results += trait_uses_recursive($class);
20
        }
21
        return array_unique($results);
22
    }
23
}
24
25
if (! function_exists('trait_uses_recursive')) {
26
    /**
27
     * Returns all traits used by a trait and its traits.
28
     *
29
     * @param  string $trait
30
     * @return array
31
     */
32
    function trait_uses_recursive($trait)
33
    {
34
        $traits = class_uses($trait);
35
        foreach ($traits as $trait) {
36
            $traits += trait_uses_recursive($trait);
37
        }
38
        return $traits;
39
    }
40
}
41
42
if(! function_exists('get_array_data')) {
43
    /**
44
     * Returns array's value by key using dot notation.
45
     *
46
     * @param string $key
47
     * @param array $data
48
     * @param mixed $default
49
     * @return array|bool|mixed
50
     */
51
    function get_array_data($key = '', $data, $default = null)
52
    {
53
        if(empty($key)) return $data;
54
55
        $indexes = explode('.', $key);
56
57
        $value = $data;
58
59
        foreach ($indexes as $index) {
60
            if(!isset($value[$index])) return $default;
61
62
            $value = $value[$index];
63
        }
64
65
        return $value;
66
    }
67
}