Passed
Push — master ( 9fe25d...e6dd30 )
by Christian
02:33
created

Collection::first()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RemotelyLiving\PHPCollection;
6
7
final class Collection extends AbstractCollection
8
{
9
    /**
10
     * @inheritDoc
11
     */
12
    public function map(callable $fn): CollectionInterface
13
    {
14
        return new self(array_map($fn, $this->all()));
15
    }
16
17
    /**
18
     * @inheritDoc
19
     */
20
    public function filter(callable $fn): CollectionInterface
21
    {
22
        return new self(array_filter($this->all(), $fn, ARRAY_FILTER_USE_BOTH));
23
    }
24
25
    /**
26
     * @inheritDoc
27
     */
28
    public function each(callable $fn): CollectionInterface
29
    {
30
        $deferredValues = function () use ($fn) {
31
            foreach ($this->all() as $key => $item) {
32
                yield $key => $fn($item, $key);
33
            }
34
        };
35
36
        return self::later($deferredValues());
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42
    public function reverse(): CollectionInterface
43
    {
44
        return new self(array_reverse($this->all()));
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    public function unique(): CollectionInterface
51
    {
52
        return new self(array_unique($this->all()));
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function diff(CollectionInterface $collection, callable $comparator = null): CollectionInterface
59
    {
60
        return new self(
61
            array_udiff($this->all(), $collection->all(), $comparator ?? Helper::getObjectSafeComparator())
62
        );
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function merge(CollectionInterface $collection): CollectionInterface
69
    {
70
        return new self(array_merge($this->all(), $collection->all()));
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    public function intersect(CollectionInterface $collection): CollectionInterface
77
    {
78
        return new self(array_intersect($this->all(), $collection->all()));
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function sort(callable $comparator = null): CollectionInterface
85
    {
86
        $items = $this->all();
87
        uasort($items, $comparator ?? Helper::getObjectSafeComparator());
88
89
        return new self($items);
90
    }
91
92
    /**
93
     * @inheritDoc
94
     */
95
    public function kSort(callable $comparator = null): CollectionInterface
96
    {
97
        $items = $this->all();
98
        uksort($items, $comparator ?? Helper::getStringComparator());
99
100
        return new self($items);
101
    }
102
103
    /**
104
     * @inheritDoc
105
     */
106
    public function reIndex(): CollectionInterface
107
    {
108
        return new self($this->values());
109
    }
110
111
    /**
112
     * @inheritDoc
113
     */
114
    public function chunk(int $size, callable $fn): CollectionInterface
115
    {
116
        $deferred = function () use ($size, $fn) {
117
            foreach (array_chunk($this->all(), $size, true) as $chunk) {
118
                foreach ($chunk as $index => $value) {
119
                    yield $index => $fn($value, $index);
120
                }
121
            }
122
        };
123
124
        return self::later($deferred());
125
    }
126
127
    /**
128
     * @inheritDoc
129
     */
130
    public function unset(...$offsets): CollectionInterface
131
    {
132
        $items = $this->all();
133
        foreach ($offsets as $offset) {
134
            unset($items[$offset]);
135
        }
136
137
        return new self($items);
138
    }
139
140
    /**
141
     * @inheritDoc
142
     */
143
    public function set($offset, $item): CollectionInterface
144
    {
145
        $added = $this->all();
146
        $added[$offset] = $item;
147
148
        return new self($added);
149
    }
150
151
    /**
152
     * @inheritDoc
153
     */
154
    final public function push($item): CollectionInterface
155
    {
156
        $pushed = $this->all();
157
        $pushed[] = $item;
158
159
        return new self($pushed);
160
    }
161
162
    /**
163
     * @inheritDoc
164
     */
165
    final public function unshift(...$items): CollectionInterface
166
    {
167
        $unshifted = $this->all();
168
        array_unshift($unshifted, ...$items);
169
170
        return new self($unshifted);
171
    }
172
}
173