Passed
Push — master ( a18919...9a039e )
by 世昌
01:53
created

ArrayDotAccess   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 28
eloc 53
dl 0
loc 132
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 11 4
A __construct() 0 3 1
A offsetExists() 0 3 1
A offsetGet() 0 3 1
B set() 0 22 10
A offsetSet() 0 6 2
A exist() 0 11 3
A toArray() 0 2 1
A offsetUnset() 0 3 1
A unset() 0 16 4
1
<?php
2
namespace nebula\arrayobject;
3
4
/**
5
 * 数组点获取类
6
 */
7
class ArrayDotAccess implements \ArrayAccess
8
{
9
    protected $value;
10
11
    public function __construct(array $array)
12
    {
13
        $this->value = $array;
14
    }
15
16
    public function offsetSet($offset, $value)
17
    {
18
        if (is_null($offset)) {
19
            $this->value[] = $value;
20
        } else {
21
            static::set($this->value, $offset, $value);
22
        }
23
    }
24
25
    public function offsetExists($offset)
26
    {
27
        return static::exist($this->value, $offset);
28
    }
29
30
    public function offsetUnset($offset)
31
    {
32
        static::unset($this->value, $offset);
33
    }
34
35
    public function offsetGet($offset)
36
    {
37
        return static::get($this->value, $offset);
38
    }
39
40
    /**
41
     * 获取数组元素
42
     *
43
     * @param array $array
44
     * @param string $name 查询列
45
     * @param mixed $def 查询的默认值
46
     * @return mixed 查询的值
47
     */
48
    public static function get(array $array, string $name, $def = null)
49
    {
50
        $path = explode('.', $name);
51
        while ($key = array_shift($path)) {
52
            if (is_array($array) && array_key_exists($key, $array)) {
53
                $array = $array[$key];
54
            } else {
55
                return $def;
56
            }
57
        }
58
        return $array;
59
    }
60
61
    /**
62
     * 检查元素是否存在
63
     *
64
     * @param array $array
65
     * @param string $name
66
     * @return boolean
67
     */
68
    public static function exist(array $array, string $name)
69
    {
70
        $path = explode('.', $name);
71
        while ($key = array_shift($path)) {
72
            if (array_key_exists($key, $array)) {
73
                $array = $array[$key];
74
            } else {
75
                return false;
76
            }
77
        }
78
        return true;
79
    }
80
81
    /**
82
     * 设置数组的值
83
     *
84
     * @param array $array
85
     * @param string $name
86
     * @param mixed $value
87
     * @param mixed $def
88
     * @return array 设置后的数组
89
     */
90
    public static function set(array &$array, string $name, $value, $def=null):array
91
    {
92
        $path = explode('.', $name);
93
        $root = &$array;
94
        while (count($path) > 1) {
95
            $key = array_shift($path);
96
            if (is_array($array)) {
97
                if (!array_key_exists($key, $array)) {
98
                    $array[$key] = [];
99
                }
100
            } else {
101
                $array=[];
102
            }
103
            $array = &$array[$key];
104
        }
105
        $key = array_shift($path);
106
        if (is_array($array) && array_key_exists($key, $array) && is_array($array[$key]) && is_array($value)) {
107
            $array[$key] = array_merge($array[$key], is_array($def) ? $def : [], $value);
108
        } else {
109
            $array[$key] = is_null($value) ? $def : $value;
110
        }
111
        return $root;
112
    }
113
114
    public static function unset(array &$array, string $name)
115
    {
116
        $path = explode('.', $name);
117
        while (count($path) > 1) {
118
            $key = array_shift($path);
119
            if (is_array($array)) {
120
                if (!array_key_exists($key, $array)) {
121
                    $array[$key] = [];
122
                }
123
            } else {
124
                $array=[];
125
            }
126
            $array = &$array[$key];
127
        }
128
        $key = array_shift($path);
129
        unset($array[$key]);
130
    }
131
132
    /**
133
     * 转换成正常数组
134
     *
135
     * @return array
136
     */
137
    public function toArray():array {
138
        return $this->value;
139
    }
140
}
141