Completed
Pull Request — master (#7)
by Harry
01:45
created

Container   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 1
dl 0
loc 194
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A add() 0 8 2
A forAll() 0 6 2
A get() 0 4 2
A getAll() 0 4 1
A getIterator() 0 4 1
A has() 0 4 1
A remove() 0 8 2
A set() 0 6 1
A serialize() 0 4 1
A unserialize() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A __clone() 0 13 3
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 143
    public function __construct(array $params = [])
31
    {
32 143
        foreach ($params as $key => $value) {
33 93
            $this->set($key, $value);
34
        }
35 143
    }
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 56
    public function get($key)
68
    {
69 56
        return $this->has($key) ? $this->params[$key] : null;
70
    }
71
72
    /**
73
     * @return array
74
     */
75 69
    public function getAll()
76
    {
77 69
        return $this->params;
78
    }
79
80
    /**
81
     * @return ArrayIterator
82
     */
83 3
    public function getIterator()
84
    {
85 3
        return new ArrayIterator($this->params);
86
    }
87
88
    /**
89
     * @param string $key
90
     *
91
     * @return bool
92
     */
93 101
    public function has($key)
94
    {
95 101
        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 125
    public function set($key, $value)
119
    {
120 125
        $this->params[$key] = $value;
121
122 125
        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
     * Clone all child objects (in the array tree)
199
     */
200
    public function __clone()
201
    {
202 17
        $clone = function ($item) use (&$clone) {
203 17
            if (is_object($item)) {
204 1
                return clone $item;
205 17
            } elseif (is_array($item)) {
206 3
                return array_map($clone, $item);
207
            }
208 17
            return $item;
209 17
        };
210
211 17
        $this->params = array_map($clone, $this->params);
212 17
    }
213
}
214