From 366c51e08af36639634b3a68266a32e4934ec618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20St=C5=99=C3=ADbrn=C3=BD?= Date: Wed, 7 Jun 2017 02:40:36 +0200 Subject: [PATCH] Update collection join method, its readme and test --- README.md | 10 +++++----- src/DateRangeCollection.php | 7 ++++++- tests/DateRangeCollection/Join.phpt | 12 +++++++++++- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 452d6c1..c46694d 100644 --- a/README.md +++ b/README.md @@ -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, @@ -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. \ No newline at end of file diff --git a/src/DateRangeCollection.php b/src/DateRangeCollection.php index 989e46b..4b19ab8 100644 --- a/src/DateRangeCollection.php +++ b/src/DateRangeCollection.php @@ -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]; diff --git a/tests/DateRangeCollection/Join.phpt b/tests/DateRangeCollection/Join.phpt index 92ac5e1..3fbbd55 100644 --- a/tests/DateRangeCollection/Join.phpt +++ b/tests/DateRangeCollection/Join.phpt @@ -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()); \ No newline at end of file +]))->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()); \ No newline at end of file