Container::offsetUnset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * This file is part of Graze DataStructure
4
 *
5
 * Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @see  http://github.com/graze/data-structure/blob/master/LICENSE
11
 * @link http://github.com/graze/data-structure
12
 */
13
14
namespace Graze\DataStructure\Container;
15
16
use ArrayIterator;
17
use Graze\DataStructure\Exception\RegisteredKeyException;
18
use Serializable;
19
20
class Container implements ContainerInterface, Serializable
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $params = [];
26
27
    /**
28
     * @param array $params
29
     */
30 147
    public function __construct(array $params = [])
31
    {
32 147
        foreach ($params as $key => $value) {
33 97
            $this->set($key, $value);
34
        }
35 147
    }
36
37
    /**
38
     * @param string $key
39
     * @param mixed  $value
40
     *
41
     * @return Container
42
     */
43 6
    public function add($key, $value)
44
    {
45 6
        if ($this->has($key)) {
46 3
            throw new RegisteredKeyException($key);
47
        }
48
49 3
        return $this->set($key, $value);
50
    }
51
52
    /**
53
     * @param callable $fn
54
     */
55 3
    public function forAll(callable $fn)
56
    {
57 3
        foreach ($this->params as $key => $value) {
58 3
            call_user_func($fn, $value, $key);
59
        }
60 3
    }
61
62
    /**
63
     * @param string $key
64
     *
65
     * @return mixed|null
66
     */
67 58
    public function get($key)
68
    {
69 58
        return $this->has($key) ? $this->params[$key] : null;
70
    }
71
72
    /**
73
     * @return array
74
     */
75 72
    public function getAll()
76
    {
77 72
        return $this->params;
78
    }
79
80
    /**
81
     * @return ArrayIterator
82
     */
83 4
    public function getIterator()
84
    {
85 4
        return new ArrayIterator($this->params);
86
    }
87
88
    /**
89
     * @param string $key
90
     *
91
     * @return bool
92
     */
93 103
    public function has($key)
94
    {
95 103
        return isset($this->params[$key]);
96
    }
97
98
    /**
99
     * @param string $key
100
     *
101
     * @return $this
102
     */
103 23
    public function remove($key)
104
    {
105 23
        if ($this->has($key)) {
106 17
            unset($this->params[$key]);
107
        }
108
109 23
        return $this;
110
    }
111
112
    /**
113
     * @param string $key
114
     * @param mixed  $value
115
     *
116
     * @return $this
117
     */
118 129
    public function set($key, $value)
119
    {
120 129
        $this->params[$key] = $value;
121
122 129
        return $this;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     *
128
     * @return string the string representation of the object or null
129
     */
130 3
    public function serialize()
131
    {
132 3
        return serialize($this->params);
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     *
138
     * @param string $data The string representation of the object.
139
     *
140
     * @return void
141
     */
142 3
    public function unserialize($data)
143
    {
144 3
        $this->params = unserialize($data);
145 3
    }
146
147
    /**
148
     * {@inheritdoc}
149
     *
150
     * @param mixed $offset An offset to check for.
151
     *
152
     * @return bool true on success or false on failure.
153
     *              The return value will be casted to boolean if non-boolean was returned.
154
     */
155 25
    public function offsetExists($offset)
156
    {
157 25
        return $this->has($offset);
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     *
163
     * @param mixed $offset The offset to retrieve.
164
     *
165
     * @return mixed Can return all value types.
166
     */
167 15
    public function offsetGet($offset)
168
    {
169 15
        return $this->get($offset);
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     *
175
     * @param mixed $offset The offset to assign the value to.
176
     * @param mixed $value  The value to set.
177
     *
178
     * @return void
179
     */
180 12
    public function offsetSet($offset, $value)
181
    {
182 12
        $this->set($offset, $value);
183 12
    }
184
185
    /**
186
     * {@inheritdoc}
187
     *
188
     * @param mixed $offset The offset to unset.
189
     *
190
     * @return void
191
     */
192 17
    public function offsetUnset($offset)
193
    {
194 17
        $this->remove($offset);
195 17
    }
196
197
    /**
198
     * @param mixed $item
199
     *
200
     * @return mixed
201
     */
202 38
    protected function recursiveClone($item)
203
    {
204 38
        if (is_object($item)) {
205 4
            return clone $item;
206 38
        } elseif (is_array($item)) {
207 6
            return array_map([$this, 'recursiveClone'], $item);
208
        }
209 38
        return $item;
210
    }
211
212
    /**
213
     * Clone all child objects (in the array tree)
214
     */
215 20
    public function __clone()
216
    {
217 20
        $this->params = array_map([$this, 'recursiveClone'], $this->params);
218 20
    }
219
}
220