Skip to content

Commit

Permalink
Add map and reduce to Periodcollection
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Apr 23, 2019
1 parent 26c9079 commit 2ee5c19
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ $period->diff(Period ...$periods): PeriodCollection
```php
$periodCollection->overlap(PeriodCollection ...$periodCollections): PeriodCollection
$periodCollection->overlapSingle(PeriodCollection $periodCollection): PeriodCollection
$periodCollection->map(Closure<Period> $closure): PeriodCollection
$periodCollection->reduce(Closure<mixed, Period> $closure): mixed
```

```php
Expand Down
23 changes: 23 additions & 0 deletions src/PeriodCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\Period;

use Closure;
use Iterator;
use Countable;
use ArrayAccess;
Expand Down Expand Up @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions tests/PeriodCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 2ee5c19

Please sign in to comment.