AbstractCollection::some()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RemotelyLiving\PHPCollection;
6
7
abstract class AbstractCollection implements CollectionInterface
8
{
9
    protected array $items;
10
11
    private \Generator $deferredValues;
12
13
    protected function __construct(array $items)
14
    {
15
        $this->items = $items;
16
    }
17
18
    final public static function collect(array $items = []): CollectionInterface
19
    {
20
        return new static($items);
21
    }
22
23
    final public static function fill(int $startIndex, int $amount, $item = null): CollectionInterface
24
    {
25
        $items = array_fill($startIndex, $amount, $item);
26
27
        return new static($items);
28
    }
29
30
    final public static function fromString(string $string, string $delimiter = ','): CollectionInterface
31
    {
32
        return new static(explode($delimiter, trim($string)));
33
    }
34
35
    final public static function later(\Generator $deferredValues): CollectionInterface
36
    {
37
        $deferred = new static([]);
38
        $deferred->deferredValues = $deferredValues;
39
40
        return $deferred;
41
    }
42
43
    final public function count(): int
44
    {
45
        return count($this->all());
46
    }
47
48
    /**
49
     * @return mixed|null
50
     */
51
    final public function first()
52
    {
53
        if ($this->empty()) {
54
            return null;
55
        }
56
57
        return $this->get(array_key_first($this->all()));
58
    }
59
60
    final public function last()
61
    {
62
        if ($this->empty()) {
63
            return null;
64
        }
65
66
        return $this->get(array_key_last($this->all()));
67
    }
68
69
    final public function reduce(callable $fn, $initial = null)
70
    {
71
        return array_reduce($this->all(), $fn, $initial);
72
    }
73
74
    final public function union(CollectionInterface $collection): CollectionInterface
75
    {
76
        return $this->merge($collection)->unique();
77
    }
78
79
    final public function empty(): bool
80
    {
81
        return $this->count() === 0;
82
    }
83
84
    final public function all(): array
85
    {
86
        if (isset($this->deferredValues) && $this->deferredValues->valid()) {
87
            $this->items = Helper::unwrapDeferred($this->deferredValues);
88
        }
89
90
        return $this->items;
91
    }
92
93
    final public function deferred(): \Generator
94
    {
95
        foreach ($this->all() as $key => $value) {
96
            yield $key => $value;
97
        }
98
    }
99
100
    final public function values(): array
101
    {
102
        return array_values($this->all());
103
    }
104
105
    final public function equals(CollectionInterface $collection): bool
106
    {
107
        return $this->diff($collection)->empty();
108
    }
109
110
    /**
111
     * @param string|int $offset
112
     *
113
     * @return bool
114
     */
115
    final public function has($offset): bool
116
    {
117
        return isset($this->all()[$offset]);
118
    }
119
120
    /**
121
     * @param mixed $item
122
     *
123
     * @return bool
124
     */
125
    final public function contains($item): bool
126
    {
127
        return in_array($item, $this->all(), true);
128
    }
129
130
    final public function some(callable $evaluation): bool
131
    {
132
        foreach ($this->all() as $key => $value) {
133
            if ($evaluation($value, $key) === true) {
134
                return true;
135
            }
136
        }
137
138
        return false;
139
    }
140
141
    final public function get($offset, $default = null)
142
    {
143
        return $this->all()[$offset] ?? $default;
144
    }
145
146
    final public function rand()
147
    {
148
        return $this->all()[array_rand($this->all())];
149
    }
150
151
    final public function getIterator(): \ArrayIterator
152
    {
153
        return new \ArrayIterator($this->all());
154
    }
155
156
    final public function serialize(): string
157
    {
158
        return \serialize(['items' => $this->all()]);
159
    }
160
161
    final public function unserialize($serialized, array $classnames = [])
162
    {
163
        if (isset($this->items)) {
164
            throw new \LogicException('Cannot unserialize instance of collection');
165
        }
166
167
        $this->items = \unserialize($serialized, $classnames)['items'] ?? [];
168
    }
169
170
    final public function jsonSerialize(): array
171
    {
172
        return $this->all();
173
    }
174
}
175