Skip to content

Commit

Permalink
Update collection join method, its readme and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Danoha committed Jun 7, 2017
1 parent fa24495 commit 366c51e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ contain both joined ranges.

- getFrom - returns first date in range,
- getTo - returns last date in range,
- unwrap - get range in array format,
- unwrap - gets range in array format,
- intersect - finds intersection between current and given range,
- overlaps - tests if current and given range overlap,
- join - finds common range between current and given range,
Expand All @@ -105,10 +105,10 @@ That means calling `$coll->add(...)` twice on the same
collection will create two instances and neither of them will
contain both added ranges.

- getRanges - get ranges in current collection,
- unwrap - get underlying date ranges in array format,
- add - add given ranges to collection,
- getRanges - gets ranges in current collection,
- unwrap - gets underlying date ranges in array format,
- add - adds given ranges to collection,
- includes - tests if collection includes given date or range,
- join - joins ranges in current collection if possible,
- join - adds given ranges to current collection and joins ranges in current collection if possible,
- intersect - calculates all intersections with given ranges,
- subtract - subtracts given ranges from current collection.
7 changes: 6 additions & 1 deletion src/DateRangeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,17 @@ public static function wrap($collection)
}

/**
* @param array|self $collection
* @return static
*/
public function join()
public function join($collection = NULL)
{
$ranges = $this->ranges;

if ($collection) {
$ranges = array_merge($ranges, static::wrap($collection)->ranges);
}

for ($i = 0; $i < count($ranges); $i++) {
$a = $ranges[$i];

Expand Down
12 changes: 11 additions & 1 deletion tests/DateRangeCollection/Join.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,14 @@ Assert::equal([
['from' => new \DateTime('2017-01-01'), 'to' => new \DateTime('2017-12-31'),],
['from' => new \DateTime('2015-01-01'), 'to' => new \DateTime('2015-12-31'),],
['from' => new \DateTime('2015-01-01'), 'to' => new \DateTime('2016-12-30'),],
]))->join()->unwrap());
]))->join()->unwrap());

Assert::equal([
['from' => new \DateTime('2015-01-01'), 'to' => new \DateTime('2016-12-30'),],
['from' => new \DateTime('2017-01-01'), 'to' => new \DateTime('2017-12-31'),],
], (new \Danoha\DateRangeCollection([
['from' => new \DateTime('2015-01-01'), 'to' => new \DateTime('2016-12-30'),],
]))->join([
['from' => new \DateTime('2017-01-01'), 'to' => new \DateTime('2017-12-31'),],
['from' => new \DateTime('2015-01-01'), 'to' => new \DateTime('2015-12-31'),],
])->unwrap());

0 comments on commit 366c51e

Please sign in to comment.