Skip to content

Commit

Permalink
Support PeriodColllection::make()
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Apr 23, 2019
1 parent e039e9d commit a56ba78
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to `period` will be documented in this file

## 1.4.1 - 2019-04-23

- Support PeriodCollection::make()
- Improved PeriodCollection doc blocks

## 1.4.0 - 2019-04-23

- Add `map` and `reduce` to `PeriodCollection`
Expand Down
50 changes: 47 additions & 3 deletions src/PeriodCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ class PeriodCollection implements ArrayAccess, Iterator, Countable
/** @var \Spatie\Period\Period[] */
protected $periods;

/**
* @param \Spatie\Period\Period ...$periods
*
* @return static
*/
public static function make(Period ...$periods): PeriodCollection
{
return new static(...$periods);
}

public function __construct(Period ...$periods)
{
$this->periods = $periods;
Expand All @@ -24,9 +34,14 @@ public function current(): Period
return $this->periods[$this->position];
}

/**
* @param \Spatie\Period\PeriodCollection $periodCollection
*
* @return static
*/
public function overlapSingle(PeriodCollection $periodCollection): PeriodCollection
{
$overlaps = new PeriodCollection();
$overlaps = static::make();

foreach ($this as $period) {
foreach ($periodCollection as $otherPeriod) {
Expand All @@ -41,6 +56,11 @@ public function overlapSingle(PeriodCollection $periodCollection): PeriodCollect
return $overlaps;
}

/**
* @param \Spatie\Period\PeriodCollection ...$periodCollections
*
* @return static
*/
public function overlap(PeriodCollection ...$periodCollections): PeriodCollection
{
$overlap = clone $this;
Expand Down Expand Up @@ -81,20 +101,28 @@ public function boundaries(): ?Period
);
}

/**
* @return static
*/
public function gaps(): PeriodCollection
{
$boundaries = $this->boundaries();

if (! $boundaries) {
return new PeriodCollection();
return static::make();
}

return $boundaries->diff(...$this);
}

/**
* @param \Spatie\Period\Period $intersection
*
* @return static
*/
public function intersect(Period $intersection): PeriodCollection
{
$intersected = new PeriodCollection();
$intersected = static::make();

foreach ($this as $period) {
$overlap = $intersection->overlapSingle($period);
Expand All @@ -109,6 +137,11 @@ public function intersect(Period $intersection): PeriodCollection
return $intersected;
}

/**
* @param \Spatie\Period\Period ...$periods
*
* @return static
*/
public function add(Period ...$periods): PeriodCollection
{
$collection = clone $this;
Expand All @@ -120,6 +153,11 @@ public function add(Period ...$periods): PeriodCollection
return $collection;
}

/**
* @param \Closure $closure
*
* @return static
*/
public function map(Closure $closure): PeriodCollection
{
$collection = clone $this;
Expand All @@ -131,6 +169,12 @@ public function map(Closure $closure): PeriodCollection
return $collection;
}

/**
* @param \Closure $closure
* @param mixed $initial
*
* @return mixed|null
*/
public function reduce(Closure $closure, $initial = null)
{
$carry = $initial;
Expand Down

0 comments on commit a56ba78

Please sign in to comment.