Passed
Push — master ( a3de2b...088e51 )
by Yao
01:34
created

array_get()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 23
rs 8.4444
cc 8
nc 7
nop 3
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
     * @return mixed
60
     */
61
    function array_get($array, ?string $key, $default = null)
62
    {
63
        if (is_array($array)) {
64
            return $default;
65
        }
66
        if (is_null($key)) {
67
            return $array;
68
        }
69
        if (array_key_exists($key, $array)) {
70
            return $array[$key];
71
        }
72
        if (strpos($key, '.') === false) {
73
            return $default;
74
        }
75
76
        foreach (explode('.', $key) as $segment) {
77
            if (is_array($array) && array_key_exists($segment, $array)) {
78
                $array = $array[$segment];
79
            } else {
80
                return $default;
81
            }
82
        }
83
        return $array;
84
    }
85
}