From 2ee5c1955f8fdf2e601bc3b05dc7f9eb2eee8589 Mon Sep 17 00:00:00 2001 From: Brent Roose Date: Tue, 23 Apr 2019 14:48:48 +0200 Subject: [PATCH] Add map and reduce to Periodcollection --- CHANGELOG.md | 4 ++++ README.md | 2 ++ src/PeriodCollection.php | 23 +++++++++++++++++++++++ tests/PeriodCollectionTest.php | 31 +++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bcb602..bc463f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `period` will be documented in this file +## 1.4.0 - 2019-04-23 + +- Add `map` and `reduce` to `PeriodCollection` + ## 1.3.1 - 2019-04-19 - Remove unused code diff --git a/README.md b/README.md index 4094680..3a18d2f 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,8 @@ $period->diff(Period ...$periods): PeriodCollection ```php $periodCollection->overlap(PeriodCollection ...$periodCollections): PeriodCollection $periodCollection->overlapSingle(PeriodCollection $periodCollection): PeriodCollection +$periodCollection->map(Closure $closure): PeriodCollection +$periodCollection->reduce(Closure $closure): mixed ``` ```php diff --git a/src/PeriodCollection.php b/src/PeriodCollection.php index 91ad594..9f171c0 100644 --- a/src/PeriodCollection.php +++ b/src/PeriodCollection.php @@ -2,6 +2,7 @@ namespace Spatie\Period; +use Closure; use Iterator; use Countable; use ArrayAccess; @@ -119,6 +120,28 @@ public function add(Period ...$periods): PeriodCollection return $collection; } + public function map(Closure $closure): PeriodCollection + { + $collection = clone $this; + + foreach ($collection->periods as $key => $period) { + $collection->periods[$key] = $closure($period); + } + + return $collection; + } + + public function reduce(Closure $closure, $initial = null) + { + $carry = $initial; + + foreach ($this as $period) { + $carry = $closure($carry, $period); + } + + return $carry; + } + public function isEmpty(): bool { return count($this->periods) === 0; diff --git a/tests/PeriodCollectionTest.php b/tests/PeriodCollectionTest.php index b57ea56..1ffc86d 100644 --- a/tests/PeriodCollectionTest.php +++ b/tests/PeriodCollectionTest.php @@ -173,4 +173,35 @@ public function intersect_test() $this->assertTrue($intersect[1]->equals(Period::make('2019-01-09', '2019-01-10'))); $this->assertTrue($intersect[2]->equals(Period::make('2019-01-10', '2019-01-11'))); } + + /** @test */ + public function map() + { + $collection = new PeriodCollection( + Period::make('2019-01-01', '2019-01-02'), + Period::make('2019-01-01', '2019-01-02'), + ); + + $mapped = $collection->map(function (Period $period) { + return $period; + }); + + $this->assertTrue($mapped[0]->equals($collection[0])); + $this->assertTrue($mapped[1]->equals($collection[1])); + } + + /** @test */ + public function reduce() + { + $collection = new PeriodCollection( + Period::make('2019-01-01', '2019-01-02'), + Period::make('2019-01-03', '2019-01-04'), + ); + + $totalLength = $collection->reduce(function (int $carry, Period $period) { + return $carry + $period->length(); + }, 0); + + $this->assertEquals(4, $totalLength); + } }