Skip to content

Commit

Permalink
Merge pull request #149 from spatie/feature/issue-148-isOpenOn-date
Browse files Browse the repository at this point in the history
Implement #148 Allow isOpenOn() to take date string as parameter
  • Loading branch information
kylekatarnls authored Sep 3, 2020
2 parents 2cca6d0 + 1ba3364 commit a4833fd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ It can also be queried for a specific date and time:
$openingHours->isOpenAt(new DateTime('2016-09-26 19:00:00')); // false

// Closed because Christmas was set as an exception
$openingHours->isOpenAt(new DateTime('2016-12-25')); // false
$openingHours->isOpenOn('2016-12-25'); // false
```

It can also return arrays of opening hours for a week or a day:
Expand Down Expand Up @@ -320,12 +320,20 @@ $openingHours->exceptions();

#### `OpeningHours::isOpenOn(string $day): bool`

Checks if the business is open on a day in the regular schedule.
Checks if the business is open (contains at least 1 range of open hours) on a day in the regular schedule.

```php
$openingHours->isOpenOn('saturday');
```

If the given string is a date, it will check if it's open (contains at least 1 range of open hours) considering
both regular day schedule and possible exceptions.

```php
$openingHours->isOpenOn('2020-09-03');
$openingHours->isOpenOn('09-03'); // If year is omitted, current year is used instead
```

#### `OpeningHours::isClosedOn(string $day): bool`

Checks if the business is closed on a day in the regular schedule.
Expand Down
7 changes: 7 additions & 0 deletions src/OpeningHours.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,13 @@ public function exceptions(): array

public function isOpenOn(string $day): bool
{
if (preg_match('/^(?:(\d+)-)?(\d{1,2})-(\d{1,2})$/', $day, $match)) {
list(, $year, $month, $day) = $match;
$year = $year ?: date('Y');

return count($this->forDate(new DateTime("$year-$month-$day"))) > 0;
}

return count($this->forDay($day)) > 0;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/OpeningHoursTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ public function it_can_determine_that_its_regularly_open_on_a_week_day()

$this->assertTrue($openingHours->isOpenOn('monday'));
$this->assertFalse($openingHours->isOpenOn('tuesday'));
$this->assertFalse($openingHours->isOpenOn('2019-08-31'));
$this->assertFalse($openingHours->isOpenOn('2019-09-01'));
$this->assertTrue($openingHours->isOpenOn('2020-08-31'));
$this->assertFalse($openingHours->isOpenOn('2020-09-01'));
$this->assertTrue($openingHours->isOpenOn((new DateTime('First Monday of January'))->format('m-d')));
$this->assertFalse($openingHours->isOpenOn((new DateTime('First Tuesday of January'))->format('m-d')));
}

/** @test */
Expand All @@ -206,6 +212,12 @@ public function it_can_determine_that_its_regularly_closed_on_a_week_day()

$this->assertFalse($openingHours->isClosedOn('monday'));
$this->assertTrue($openingHours->isClosedOn('tuesday'));
$this->assertTrue($openingHours->isClosedOn('2019-08-31'));
$this->assertTrue($openingHours->isClosedOn('2019-09-01'));
$this->assertFalse($openingHours->isClosedOn('2020-08-31'));
$this->assertTrue($openingHours->isClosedOn('2020-09-01'));
$this->assertFalse($openingHours->isClosedOn((new DateTime('First Monday of January'))->format('m-d')));
$this->assertTrue($openingHours->isClosedOn((new DateTime('First Tuesday of January'))->format('m-d')));
}

/** @test */
Expand Down

0 comments on commit a4833fd

Please sign in to comment.