10
10
use Traversable ;
11
11
use IteratorAggregate ;
12
12
13
+ /**
14
+ * @implements IteratorAggregate<int|string, mixed>
15
+ */
13
16
class Collection implements CollectionInterface, IteratorAggregate, Countable
14
17
{
18
+ /**
19
+ * Create new collection object
20
+ *
21
+ * @param array<int|string, mixed> $items
22
+ * @return void
23
+ */
15
24
public function __construct (protected array $ items = [])
16
25
{
17
26
}
18
27
19
28
/**
20
29
* Static constructor
21
30
*
22
- * @param array $items
23
- * @return self
31
+ * @param array<int|string, mixed> $items
32
+ * @return self<int|string, mixed>
24
33
*/
25
34
public static function create (array $ items = []): self
26
35
{
27
36
return new self ($ items );
28
37
}
29
38
39
+ /**
40
+ * {@inheritdoc}
41
+ *
42
+ * @see CollectionInterface::has()
43
+ */
30
44
public function has (int |string $ key ): bool
31
45
{
32
46
return array_key_exists ($ key , $ this ->items );
@@ -35,13 +49,18 @@ public function has(int|string $key): bool
35
49
/**
36
50
* Returns Iterator
37
51
*
38
- * @return Traversable
52
+ * @return Traversable<int|string, mixed>
39
53
*/
40
54
public function getIterator (): Traversable
41
55
{
42
56
return new ArrayIterator ($ this ->items );
43
57
}
44
58
59
+ /**
60
+ * {@inheritdoc}
61
+ *
62
+ * @see CollectionInterface::toArray()
63
+ */
45
64
public function toArray (): array
46
65
{
47
66
return $ this ->items ;
@@ -61,7 +80,7 @@ public function count(): int
61
80
* Append new item to collection
62
81
*
63
82
* @param mixed $item
64
- * @return CollectionInterface
83
+ * @return CollectionInterface<int|string, mixed>
65
84
*/
66
85
public function push ($ item ): CollectionInterface
67
86
{
@@ -154,6 +173,12 @@ public function get(int|string $query, $default = null): mixed
154
173
return $ result ;
155
174
}
156
175
176
+ /**
177
+ * Map each item of collection by given callback
178
+ *
179
+ * @param callable $callback
180
+ * @return self
181
+ */
157
182
public function map (callable $ callback ): self
158
183
{
159
184
$ items = array_map (function ($ item ) use ($ callback ) {
@@ -167,7 +192,7 @@ public function map(callable $callback): self
167
192
* Run callback on each item of the collection an remove it if it does not return true
168
193
*
169
194
* @param callable $callback
170
- * @return Collection
195
+ * @return self
171
196
*/
172
197
public function filter (callable $ callback ): self
173
198
{
0 commit comments