Skip to content

Commit

Permalink
Merge pull request #105 from aliowacom/patch-1
Browse files Browse the repository at this point in the history
Error "Undefined array key 0" fix
  • Loading branch information
brendt authored Dec 23, 2021
2 parents 66f504e + 8248b83 commit bb78dbd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/PeriodTraits/PeriodOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public function gap(Period $period): ?static

public function overlap(Period ...$others): ?static
{
if (count($others) > 1) {
if (count($others) === 0) {
return null;
} else if (count($others) > 1) {
return $this->overlapAll(...$others);
} else {
$other = $others[0];
Expand Down Expand Up @@ -115,7 +117,9 @@ public function overlapAny(Period ...$others): PeriodCollection
*/
public function subtract(Period ...$others): PeriodCollection
{
if (count($others) > 1) {
if (count($others) === 0) {
return PeriodCollection::make($this);
} else if (count($others) > 1) {
return $this->subtractAll(...$others);
} else {
$other = $others[0];
Expand Down
12 changes: 12 additions & 0 deletions tests/Operations/OverlapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Generator;
use PHPUnit\Framework\TestCase;
use Spatie\Period\Period;
use Spatie\Period\PeriodCollection;

class OverlapTest extends TestCase
{
Expand Down Expand Up @@ -178,4 +179,15 @@ public function noOverlappingDates()
*/
yield [Period::make('2018-02-01', '2018-02-28'), Period::make('2018-01-01', '2018-01-31')];
}

/** @test */
public function passing_empty_period_collection_returns_null()
{
$current = Period::make('2018-01-01', '2018-01-31');
$emptyCollection = new PeriodCollection;

$diff = $current->overlap(... $emptyCollection);

$this->assertNull($diff);
}
}
14 changes: 14 additions & 0 deletions tests/Operations/SubtractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use Spatie\Period\Period;
use Spatie\Period\PeriodCollection;

class SubtractTest extends TestCase
{
Expand Down Expand Up @@ -223,4 +224,17 @@ public function it_can_determine_multiple_diffs_for_sure()
$this->assertTrue($diff[1]->equals(Period::make('2018-01-11', '2018-01-14')));
$this->assertTrue($diff[2]->equals(Period::make('2018-01-21', '2018-01-31')));
}

/** @test */
public function passing_empty_period_collection_returns_same_period_within_collection()
{
$current = Period::make('2018-01-01', '2018-01-31');
$emptyCollection = new PeriodCollection;

$diff = $current->subtract(... $emptyCollection);

$this->assertInstanceOf(PeriodCollection::class, $diff);
$this->assertCount(1, $diff);
$this->assertTrue($diff[0]->equals($current));
}
}

0 comments on commit bb78dbd

Please sign in to comment.