Completed
Push — include-lib ( fd24a6...4173d3 )
by Arnaud
13:24
created

Collection::replace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Collection;
10
11
/**
12
 * Class Collection.
13
 */
14
class Collection implements CollectionInterface
15
{
16
    /**
17
     * Collections's identifier.
18
     *
19
     * @var string
20
     */
21
    protected $id = '';
22
23
    /**
24
     * Collection's items.
25
     *
26
     * @var array
27
     */
28
    protected $items = [];
29
30
    /**
31
     * AbstractCollection constructor.
32
     *
33
     * @param string|null $id
34
     * @param array       $items
35
     */
36
    public function __construct($id = null, $items = [])
37
    {
38
        $this->setId($id);
39
        $this->items = $items;
40
    }
41
42
    /**
43
     * If parameter is empty uses the object hash
44
     * {@inheritdoc}
45
     */
46
    public function setId($id = '')
47
    {
48
        if (empty($id)) {
49
            $this->id = spl_object_hash($this);
50
        } else {
51
            $this->id = $id;
52
        }
53
54
        return $this;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getId()
61
    {
62
        return $this->id;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function has($id)
69
    {
70
        return array_key_exists($id, $this->items);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 View Code Duplication
    public function add(ItemInterface $item)
77
    {
78
        if ($this->has($item->getId())) {
79
            throw new \DomainException(sprintf(
80
                'Failed adding item "%s": an item with that id has already been added.',
81
                $item->getId()
82
            ));
83
        }
84
        $this->items[$item->getId()] = $item;
85
86
        return $this;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 View Code Duplication
    public function replace($id, ItemInterface $item)
93
    {
94
        if ($this->has($id)) {
95
            $this->items[$id] = $item;
96
        } else {
97
            throw new \DomainException(sprintf(
98
                'Failed replacing item "%s": item does not exist.',
99
                $item->getId()
100
            ));
101
        }
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function remove($id)
108
    {
109
        if ($this->has($id)) {
110
            unset($this->items[$id]);
111
        } else {
112
            throw new \DomainException(sprintf(
113
                'Failed removing item with ID "%s": item does not exist.',
114
                $id
115
            ));
116
        }
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function get($id)
123
    {
124
        if (!$this->has($id)) {
125
            return false;
126
        }
127
128
        return $this->items[$id];
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function keys()
135
    {
136
        return array_keys($this->items);
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function count()
143
    {
144
        return count($this->items);
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function toArray()
151
    {
152
        return $this->items;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function getIterator()
159
    {
160
        return new \ArrayIterator($this->items);
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function usort(\Closure $callback = null)
167
    {
168
        $items = $this->items;
169
        $callback ? uasort($items, $callback) : uasort($items, function ($a, $b) {
170
            if ($a == $b) {
171
                return 0;
172
            }
173
174
            return ($a < $b) ? -1 : 1;
175
        });
176
177
        return new static(self::getId(), $items);
178
    }
179
180
    /**
181
     * Sort items by date.
182
     *
183
     * @return Collection
184
     */
185
    public function sortByDate()
186
    {
187
        return $this->usort(function ($a, $b) {
188
            if (!isset($a['date'])) {
189
                return -1;
190
            }
191
            if (!isset($b['date'])) {
192
                return 1;
193
            }
194
            if ($a['date'] == $b['date']) {
195
                return 0;
196
            }
197
198
            return ($a['date'] > $b['date']) ? -1 : 1;
199
        });
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     *
205
     * @return Collection
206
     */
207
    public function filter(\Closure $callback)
208
    {
209
        return new static(self::getId(), array_filter($this->items, $callback));
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function map(\Closure $callback)
216
    {
217
        return new static(self::getId(), array_map($callback, $this->items));
218
    }
219
220
    /**
221
     * Implement ArrayAccess.
222
     *
223
     * @param mixed $offset
224
     *
225
     * @return bool
226
     */
227
    public function offsetExists($offset)
228
    {
229
        return $this->has($offset);
230
    }
231
232
    /**
233
     * Implement ArrayAccess.
234
     *
235
     * @param mixed $offset
236
     *
237
     * @return null|CollectionInterface
238
     */
239
    public function offsetGet($offset)
240
    {
241
        return $this->get($offset);
242
    }
243
244
    /**
245
     * Implement ArrayAccess.
246
     *
247
     * @param mixed $offset
248
     * @param mixed $value
249
     */
250
    public function offsetSet($offset, $value)
251
    {
252
        $this->add($value);
253
    }
254
255
    /**
256
     * Implement ArrayAccess.
257
     *
258
     * @param mixed $offset
259
     */
260
    public function offsetUnset($offset)
261
    {
262
        $this->remove($offset);
263
    }
264
265
    /**
266
     * Returns a string representation of this object.
267
     *
268
     * @return string
269
     */
270
    public function __toString()
271
    {
272
        return json_encode($this->items)."\n";
273
    }
274
}
275