Completed
Push — collection ( 08980f )
by Arnaud
01:58
created

Collection::sortByDate()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4222
c 0
b 0
f 0
cc 5
nc 1
nop 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Collection::filter() 0 4 1
A Collection::map() 0 4 1
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
     * Collection'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
     * Collection 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's hash.
44
     * {@inheritdoc}
45
     */
46
    public function setId(string $id = null)
47
    {
48
        $this->id = $id;
49
        if (empty($this->id)) {
50
            $this->id = spl_object_hash($this);
51
        }
52
53
        return $this;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getId(): string
60
    {
61
        return $this->id;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function has(string $id): bool
68
    {
69
        return array_key_exists($id, $this->items);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function add(ItemInterface $item): ?CollectionInterface
76
    {
77
        if ($this->has($item->getId())) {
78
            throw new \DomainException(sprintf(
79
                'Failed adding "%s" in "%s" collection: item already exists.',
80
                $item->getId(),
81
                $this->getId()
82
            ));
83
        }
84
        $this->items[$item->getId()] = $item;
85
86
        return $this;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function replace(string $id, ItemInterface $item): ?CollectionInterface
93
    {
94
        if (!$this->has($id)) {
95
            throw new \DomainException(sprintf(
96
                'Failed replacing "%s" in "%s" collection: item does not exist.',
97
                $item->getId(),
98
                $this->getId()
99
            ));
100
        }
101
        $this->items[$id] = $item;
102
103
        return $this;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function remove(string $id): ?CollectionInterface
110
    {
111
        if (!$this->has($id)) {
112
            throw new \DomainException(sprintf(
113
                'Failed removing "%s" in "%s" collection: item does not exist.',
114
                $id,
115
                $this->getId()
116
            ));
117
        }
118
        unset($this->items[$id]);
119
120
        return $this;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function get(string $id): ?ItemInterface
127
    {
128
        if (!$this->has($id)) {
129
            return false;
130
        }
131
132
        return $this->items[$id];
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function keys(): array
139
    {
140
        return array_keys($this->items);
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function count(): int
147
    {
148
        return count($this->items);
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function toArray(): array
155
    {
156
        return $this->items;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function getIterator(): \ArrayIterator
163
    {
164
        return new \ArrayIterator($this->items);
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function usort(\Closure $callback = null): CollectionInterface
171
    {
172
        $callback ? uasort($this->items, $callback) : uasort($this->items, function ($a, $b) {
173
            if ($a == $b) {
174
                return 0;
175
            }
176
177
            return ($a < $b) ? -1 : 1;
178
        });
179
180
        return new static(self::getId(), $this->items);
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function filter(\Closure $callback): CollectionInterface
187
    {
188
        return new static(self::getId(), array_filter($this->items, $callback));
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function map(\Closure $callback): CollectionInterface
195
    {
196
        return new static(self::getId(), array_map($callback, $this->items));
197
    }
198
199
    /**
200
     * Implement ArrayAccess.
201
     *
202
     * @param string $offset
203
     *
204
     * @return bool
205
     */
206
    public function offsetExists($offset)
207
    {
208
        return $this->has($offset);
209
    }
210
211
    /**
212
     * Implement ArrayAccess.
213
     *
214
     * @param string $offset
215
     *
216
     * @return CollectionInterface|null
0 ignored issues
show
Documentation introduced by
Should the return type not be ItemInterface|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
217
     */
218
    public function offsetGet($offset)
219
    {
220
        return $this->get($offset);
221
    }
222
223
    /**
224
     * Implement ArrayAccess.
225
     *
226
     * @param mixed $offset
227
     * @param ItemInterface $value
228
     *
229
     * @return CollectionInterface|null
230
     */
231
    public function offsetSet($offset, $value)
232
    {
233
        return $this->add($value);
234
    }
235
236
    /**
237
     * Implement ArrayAccess.
238
     *
239
     * @param string $offset
240
     *
241
     * @return CollectionInterface|null
242
     */
243
    public function offsetUnset($offset)
244
    {
245
        return $this->remove($offset);
246
    }
247
248
    /**
249
     * Returns a string representation of this object.
250
     *
251
     * @return string
252
     */
253
    public function __toString()
254
    {
255
        return sprintf("%s\n", json_encode($this->items));
256
    }
257
}
258