Completed
Push — master ( 5df011...747e00 )
by Changwan
03:50
created

DotArray::has()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 4
nop 1
dl 0
loc 16
ccs 10
cts 11
cp 0.9091
crap 5.0187
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Support;
3
4
use InvalidArgumentException;
5
use ArrayAccess;
6
7
class DotArray implements ArrayAccess
8
{
9
    /** @var array */
10
    protected $items;
11
12
    /**
13
     * @param array $items
14
     */
15 24
    public function __construct(array $items = [])
16
    {
17 24
        $this->items = $items;
18 24
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 4
    public function getRawData()
24
    {
25 4
        return $this->items;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 16
    public function get($name, $default = null)
32
    {
33 16
        if ($name === '') {
34 2
            return $this->items;
35
        }
36 16
        $names = explode('.', $name);
37 16
        $dataToReturn = $this->items;
38 16
        while (count($names)) {
39 16
            $name = array_shift($names);
40 16
            if (!is_array($dataToReturn) || !array_key_exists($name, $dataToReturn)) {
41 9
                return $default;
42
            }
43 15
            $dataToReturn = $dataToReturn[$name];
44
        }
45 15
        return $dataToReturn;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 2
    public function set($name, $value)
52
    {
53 2
        if ($name === '') {
54
            $this->items = $value;
55
        }
56 2
        $names = explode('.', $name);
57 2
        $dataToSet = &$this->items;
58 2
        while (count($names)) {
59 2
            $name = array_shift($names);
60 2
            if (!is_array($dataToSet)) {
61 2
                $dataToSet = [];
62
            }
63 2
            if (!array_key_exists($name, $dataToSet)) {
64 2
                $dataToSet[$name] = null;
65
            }
66 2
            $dataToSet = &$dataToSet[$name];
67
        }
68 2
        $dataToSet = $value;
69 2
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 4
    public function has($name)
75
    {
76 4
        if ($name === '') {
77
            return true;
78
        }
79 4
        $names = explode('.', $name);
80 4
        $dataToReturn = $this->items;
81 4
        while (count($names)) {
82 4
            $name = array_shift($names);
83 4
            if (!is_array($dataToReturn) || !array_key_exists($name, $dataToReturn)) {
84 4
                return false;
85
            }
86 4
            $dataToReturn = $dataToReturn[$name];
87
        }
88 4
        return true;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 2
    public function remove($name)
95
    {
96 2
        if ($name === '') {
97
            $this->items = [];
98
            return true;
99
        }
100 2
        $names = explode('.', $name);
101 2
        $dataToReturn = &$this->items;
102 2
        while (count($names)) {
103 2
            $name = array_shift($names);
104 2
            if (!is_array($dataToReturn) || !array_key_exists($name, $dataToReturn)) {
105 2
                return false;
106
            }
107 2
            if (count($names) === 0) {
108 2
                unset($dataToReturn[$name]);
109
            } else {
110 2
                $dataToReturn = &$dataToReturn[$name];
111
            }
112
        }
113 2
        return true;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 2
    public function subset($name)
120
    {
121 2
        $subset = $this->get($name);
122 2
        if (!is_array($subset)) {
123 2
            throw new InvalidArgumentException('subset must be an array.');
124
        }
125 2
        return new static($subset);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function offsetExists($offset)
132
    {
133
        return $this->has($offset);
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 2
    public function offsetGet($offset)
140
    {
141 2
        if (strpos($offset, '||') !== false) {
142 2
            list($offset, $default) = explode('||', $offset);
143 2
            return $this->get($offset, $default);
144
        }
145 2
        return $this->get($offset);
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function offsetSet($offset, $value)
152
    {
153
        $this->set($offset, $value);
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function offsetUnset($offset)
160
    {
161
        $this->remove($offset);
162
    }    
163
}
164