Passed
Push — master ( 86e1ec...37bea7 )
by Yao
01:29
created

array_get()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 2
b 0
f 0
nc 4
nop 4
dl 0
loc 12
rs 10
1
<?php
2
/**
3
 * This file is part of the mucts.com.
4
 *
5
 * This source file is subject to the MIT license that is bundled
6
 * with this source code in the file LICENSE.
7
 *
8
 * @version 1.0
9
 * @author herry<[email protected]>
10
 * @copyright © 2020  MuCTS.com All Rights Reserved.
11
 */
12
13
if (!function_exists('get_namespace')) {
14
    /**
15
     * Returns the namespace of the class of an object
16
     *
17
     * @param object $object
18
     * @return string
19
     */
20
    function get_namespace(object $object): string
21
    {
22
        return strtr(dirname(strtr(get_class($object), ['\\' => '/'])), ['/' => '\\']);
23
    }
24
}
25
26
if (!function_exists('hump_to_underline')) {
27
    /**
28
     * 驼峰转下划线
29
     *
30
     * @param string $hump
31
     * @return string
32
     */
33
    function hump_to_underline(string $hump): string
34
    {
35
        return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $hump), "_"));
36
    }
37
}
38
39
if (!function_exists('underline_to_hump')) {
40
    /**
41
     * 下划线转驼峰
42
     *
43
     * @param string $underline
44
     * @return string
45
     */
46
    function underline_to_hump(string $underline): string
47
    {
48
        return strtr(ucwords(strtr($underline, ['_' => ' '])), [' ' => '']);
49
    }
50
}
51
52
if (!function_exists('array_get')) {
53
    /**
54
     * Get an item from an array using "dot" notation.
55
     *
56
     * @param mixed $array
57
     * @param string|null $key
58
     * @param mixed $default
59
     * @param string $glue
60
     * @return mixed
61
     */
62
    function array_get($array, ?string $key, $default = null, string $glue = '.')
63
    {
64
        if (is_array($array)) {
65
            return $default;
66
        }
67
        if (is_null($key)) {
68
            return $array;
69
        }
70
        if (array_key_exists($key, $array)) {
71
            return $array[$key];
72
        }
73
        return chain_array_get($array, $key, $default, $glue);
74
    }
75
}
76
77
if (!function_exists('chain_array_get')) {
78
    /**
79
     * Get an item from an array using "dot" notation.
80
     *
81
     * @param array $array
82
     * @param string $key
83
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
84
     * @param string $glue
85
     * @return mixed|null
86
     */
87
    function chain_array_get(array $array, string $key, $default = null, string $glue = '.')
88
    {
89
        if (strpos($key, $glue) === false) {
90
            return $array[$key] ?? $default;
91
        }
92
        foreach (explode($glue, $key) as $segment) {
93
            if (is_array($array) && array_key_exists($segment, $array)) {
94
                $array = $array[$segment];
95
            } else {
96
                return $default;
97
            }
98
        }
99
        return $array;
100
    }
101
}