Skip to content

Commit dd2918f

Browse files
ramseyraphaelts3
andcommitted
Revert "refactor: remove reduce() from the main branch for 1.x"
This reverts commit 94a6ace. Co-authored-by: Raphael Tomé Santana <raphaelts3@gmail.com>
1 parent ad7475d commit dd2918f

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## 2.0.0 - TBD
9+
10+
### Added
11+
12+
* Add support for CollectionInterface::reduce().
13+
814
## 1.3.0 - 2022-12-27
915

1016
### Fixed

src/AbstractCollection.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use function array_filter;
2727
use function array_map;
2828
use function array_merge;
29+
use function array_reduce;
2930
use function array_search;
3031
use function array_udiff;
3132
use function array_uintersect;
@@ -210,6 +211,14 @@ public function map(callable $callback): CollectionInterface
210211
return new Collection('mixed', array_map($callback, $this->data));
211212
}
212213

214+
/**
215+
* @inheritDoc
216+
*/
217+
public function reduce(callable $callback, $initial = null)
218+
{
219+
return array_reduce($this->data, $callback, $initial);
220+
}
221+
213222
public function diff(CollectionInterface $other): CollectionInterface
214223
{
215224
$this->compareCollectionTypes($other);

src/CollectionInterface.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,27 @@ public function where(string $propertyOrMethod, $value): self;
173173
*/
174174
public function map(callable $callback): self;
175175

176+
/**
177+
* Apply a given callback method on each item of the collection
178+
* to reduce it to a single value. If an initial value is provided
179+
* it will be used at the beginning of the process, or as a final
180+
* result in case the array is empty.
181+
*
182+
* See the {@link http://php.net/manual/en/function.array-reduce.php PHP array_reduce() documentation}
183+
* for examples of how the `$callback` and `$initial` parameters work.
184+
*
185+
* @param callable(TCarry|null, T): (TCarry|null) $callback A callable to apply to each
186+
* item of the collection to reduce it to a single value.
187+
* @param TCarry|null $initial If provided, this is the initial value provided
188+
* to the callback.
189+
*
190+
* @return TCarry|mixed
191+
*
192+
* @template TCarry
193+
*/
194+
// phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
195+
public function reduce(callable $callback, $initial = null);
196+
176197
/**
177198
* Create a new collection with divergent items between current and given
178199
* collection.

tests/CollectionManipulationTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,38 @@ public function testMapShouldRunOverEachItem(): void
178178
$this->assertNotSame($barCollection, $mapCollection);
179179
}
180180

181+
public function testReduceShouldRunOverEachItem(): void
182+
{
183+
$bar1 = new Bar(1, 'a');
184+
$bar2 = new Bar(2, 'b');
185+
186+
$barCollection = new BarCollection([$bar1, $bar2]);
187+
188+
$reduceCollection = $barCollection->reduce(function (?int $carry, Bar $item): int {
189+
$carry += $item->getId();
190+
191+
return $carry;
192+
});
193+
194+
$this->assertSame(3, $reduceCollection);
195+
}
196+
197+
public function testReduceShouldUseInitialIfProvided(): void
198+
{
199+
$bar1 = new Bar(1, 'a');
200+
$bar2 = new Bar(2, 'b');
201+
202+
$barCollection = new BarCollection([$bar1, $bar2]);
203+
204+
$reduceCollection = $barCollection->reduce(function (?int $carry, Bar $item): int {
205+
$carry += $item->getId();
206+
207+
return $carry;
208+
}, 1);
209+
210+
$this->assertSame(4, $reduceCollection);
211+
}
212+
181213
public function testDiffShouldRaiseExceptionOnDiverseCollections(): void
182214
{
183215
$barCollection = new BarCollection();

0 commit comments

Comments
 (0)