1 | <?php |
||||
2 | |||||
3 | namespace Bdf\Collection\Stream; |
||||
4 | |||||
5 | use Bdf\Collection\HashSet; |
||||
6 | use Bdf\Collection\Stream\Accumulator\AccumulatorInterface; |
||||
7 | use Bdf\Collection\Stream\Collector\CollectorInterface; |
||||
8 | use Bdf\Collection\Util\Optional; |
||||
9 | use Bdf\Collection\Util\OptionalInterface; |
||||
10 | use function iterator_to_array; |
||||
11 | |||||
12 | /** |
||||
13 | * Implementation of base stream methods |
||||
14 | * |
||||
15 | * @psalm-suppress LessSpecificImplementedReturnType |
||||
16 | */ |
||||
17 | trait StreamTrait |
||||
18 | { |
||||
19 | /** |
||||
20 | * @see StreamInterface::filter() |
||||
21 | */ |
||||
22 | 12 | public function filter(callable $predicate): StreamInterface |
|||
23 | { |
||||
24 | 12 | return new FilterStream($this, $predicate); |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
25 | } |
||||
26 | |||||
27 | /** |
||||
28 | * @see StreamInterface::map() |
||||
29 | */ |
||||
30 | 18 | public function map(callable $transformer): StreamInterface |
|||
31 | { |
||||
32 | 18 | return new MapStream($this, $transformer); |
|||
0 ignored issues
–
show
$this of type Bdf\Collection\Stream\StreamTrait is incompatible with the type Bdf\Collection\Stream\StreamInterface expected by parameter $stream of Bdf\Collection\Stream\MapStream::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
33 | } |
||||
34 | |||||
35 | /** |
||||
36 | * @see StreamInterface::mapKey() |
||||
37 | */ |
||||
38 | 11 | public function mapKey(callable $function): StreamInterface |
|||
39 | { |
||||
40 | 11 | return new MapKeyStream($this, $function); |
|||
0 ignored issues
–
show
$this of type Bdf\Collection\Stream\StreamTrait is incompatible with the type Bdf\Collection\Stream\StreamInterface expected by parameter $stream of Bdf\Collection\Stream\MapKeyStream::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
41 | } |
||||
42 | |||||
43 | /** |
||||
44 | * @see StreamInterface::distinct() |
||||
45 | */ |
||||
46 | 13 | public function distinct(callable $hashFunction = null): StreamInterface |
|||
47 | { |
||||
48 | 13 | return new DistinctStream($this, new HashSet($hashFunction)); |
|||
0 ignored issues
–
show
$this of type Bdf\Collection\Stream\StreamTrait is incompatible with the type Bdf\Collection\Stream\StreamInterface expected by parameter $stream of Bdf\Collection\Stream\Di...ctStream::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
49 | } |
||||
50 | |||||
51 | /** |
||||
52 | * @see StreamInterface::sort() |
||||
53 | */ |
||||
54 | 15 | public function sort(callable $comparator = null, bool $preserveKeys = false): StreamInterface |
|||
55 | { |
||||
56 | 15 | return new SortStream($this, $comparator, $preserveKeys); |
|||
0 ignored issues
–
show
$this of type Bdf\Collection\Stream\StreamTrait is incompatible with the type Bdf\Collection\Stream\StreamInterface expected by parameter $stream of Bdf\Collection\Stream\SortStream::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
57 | } |
||||
58 | |||||
59 | /** |
||||
60 | * @see StreamInterface::concat() |
||||
61 | */ |
||||
62 | 10 | public function concat(StreamInterface $stream, bool $preserveKeys = true): StreamInterface |
|||
63 | { |
||||
64 | 10 | return new ConcatStream([$this, $stream], $preserveKeys); |
|||
65 | } |
||||
66 | |||||
67 | /** |
||||
68 | * @see StreamInterface::flatMap() |
||||
69 | */ |
||||
70 | 13 | public function flatMap(callable $transformer, bool $preserveKeys = false): StreamInterface |
|||
71 | { |
||||
72 | 13 | return new FlatMapStream($this, $transformer, $preserveKeys); |
|||
0 ignored issues
–
show
$this of type Bdf\Collection\Stream\StreamTrait is incompatible with the type Bdf\Collection\Stream\StreamInterface expected by parameter $stream of Bdf\Collection\Stream\FlatMapStream::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
73 | } |
||||
74 | |||||
75 | /** |
||||
76 | * @see StreamInterface::skip() |
||||
77 | */ |
||||
78 | 10 | public function skip(int $count): StreamInterface |
|||
79 | { |
||||
80 | 10 | return new LimitStream($this, $count); |
|||
0 ignored issues
–
show
$this of type Bdf\Collection\Stream\StreamTrait is incompatible with the type Iterator expected by parameter $iterator of Bdf\Collection\Stream\LimitStream::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
81 | } |
||||
82 | |||||
83 | /** |
||||
84 | * @see StreamInterface::limit() |
||||
85 | */ |
||||
86 | 10 | public function limit(int $count, int $offset = 0): StreamInterface |
|||
87 | { |
||||
88 | 10 | return new LimitStream($this, $offset, $count); |
|||
0 ignored issues
–
show
$this of type Bdf\Collection\Stream\StreamTrait is incompatible with the type Iterator expected by parameter $iterator of Bdf\Collection\Stream\LimitStream::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
89 | } |
||||
90 | |||||
91 | /** |
||||
92 | * @see StreamInterface::forEach() |
||||
93 | */ |
||||
94 | 10 | public function forEach(callable $consumer): void |
|||
95 | { |
||||
96 | 10 | foreach ($this as $key => $value) { |
|||
97 | 10 | $consumer($value, $key); |
|||
98 | } |
||||
99 | 10 | } |
|||
100 | |||||
101 | /** |
||||
102 | * @see StreamInterface::toArray() |
||||
103 | */ |
||||
104 | 121 | public function toArray(bool $preserveKeys = true): array |
|||
105 | { |
||||
106 | 121 | return iterator_to_array($this, $preserveKeys); |
|||
0 ignored issues
–
show
$this of type Bdf\Collection\Stream\StreamTrait is incompatible with the type Traversable expected by parameter $iterator of iterator_to_array() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
107 | } |
||||
108 | |||||
109 | /** |
||||
110 | * @see StreamInterface::first() |
||||
111 | */ |
||||
112 | 8 | public function first(): OptionalInterface |
|||
113 | { |
||||
114 | 8 | foreach ($this as $value) { |
|||
115 | 8 | return Optional::nullable($value); |
|||
116 | } |
||||
117 | |||||
118 | 8 | return Optional::empty(); |
|||
119 | } |
||||
120 | |||||
121 | /** |
||||
122 | * @see StreamInterface::reduce() |
||||
123 | * @psalm-suppress MissingParamType |
||||
124 | */ |
||||
125 | 23 | public function reduce(callable $accumulator, $initial = null) |
|||
126 | { |
||||
127 | 23 | $carry = $initial === null && $accumulator instanceof AccumulatorInterface |
|||
128 | 15 | ? $accumulator->initial() |
|||
129 | 23 | : $initial |
|||
130 | ; |
||||
131 | |||||
132 | 23 | foreach ($this as $item) { |
|||
133 | 21 | $carry = $accumulator($carry, $item); |
|||
134 | } |
||||
135 | |||||
136 | 23 | return $carry; |
|||
137 | } |
||||
138 | |||||
139 | /** |
||||
140 | * @see StreamInterface::collect() |
||||
141 | */ |
||||
142 | 22 | public function collect(CollectorInterface $collector) |
|||
143 | { |
||||
144 | 22 | foreach ($this as $key => $item) { |
|||
145 | 22 | $collector->aggregate($item, $key); |
|||
146 | } |
||||
147 | |||||
148 | 22 | return $collector->finalize(); |
|||
149 | } |
||||
150 | |||||
151 | /** |
||||
152 | * @see StreamInterface::matchAll() |
||||
153 | */ |
||||
154 | 12 | public function matchAll(callable $predicate): bool |
|||
155 | { |
||||
156 | 12 | foreach ($this as $key => $item) { |
|||
157 | 12 | if (!$predicate($item, $key)) { |
|||
158 | 12 | return false; |
|||
159 | } |
||||
160 | } |
||||
161 | |||||
162 | 11 | return true; |
|||
163 | } |
||||
164 | |||||
165 | /** |
||||
166 | * @see StreamInterface::matchOne() |
||||
167 | */ |
||||
168 | 11 | public function matchOne(callable $predicate): bool |
|||
169 | { |
||||
170 | 11 | foreach ($this as $key => $item) { |
|||
171 | 11 | if ($predicate($item, $key)) { |
|||
172 | 11 | return true; |
|||
173 | } |
||||
174 | } |
||||
175 | |||||
176 | 10 | return false; |
|||
177 | } |
||||
178 | } |
||||
179 |