Skip to content

Commit

Permalink
Added closing days methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiandedeyne committed Jun 1, 2017
1 parent f12cf1c commit 80426b7
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All Notable changes to `opening-hours` will be documented in this file

## 1.3.0 - 2017-06-01
- Added `regularClosingDays`, `regularClosingDaysISO` and `exceptionalClosingDates` methods

## 1.2.0 - 2017-01-03
- Added `asStructuredData` to retrieve the opening hours as a Schema.org structured data array
- Added `nextOpen` method to determine the next time the business will be open
Expand Down
5 changes: 5 additions & 0 deletions src/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public static function onDateTime(DateTimeInterface $dateTime): string
{
return static::days()[$dateTime->format('N') - 1];
}

public static function toISO(string $day): int
{
return array_search($day, static::days()) + 1;
}
}
5 changes: 5 additions & 0 deletions src/Helpers/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

class Arr
{
public static function filter(array $array, callable $callback): array
{
return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
}

public static function map(array $array, callable $callback): array
{
$keys = array_keys($array);
Expand Down
39 changes: 36 additions & 3 deletions src/OpeningHours.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
class OpeningHours
{
/** @var \Spatie\OpeningHours\Day[] */
protected $openingHours;
protected $openingHours = [];

/** @var array */
protected $exceptions = [];

/** @var DateTimeZone|null */
protected $timezone;
protected $timezone = null;

public function __construct($timezone = null)
{
Expand Down Expand Up @@ -127,7 +127,7 @@ public function isClosed(): bool
return $this->isClosedAt(new DateTime());
}

public function nextOpen(DateTimeInterface $dateTime) : DateTime
public function nextOpen(DateTimeInterface $dateTime): DateTime
{
$openingHoursForDay = $this->forDate($dateTime);
$nextOpen = $openingHoursForDay->nextOpen(Time::fromDateTime($dateTime));
Expand All @@ -148,6 +148,29 @@ public function nextOpen(DateTimeInterface $dateTime) : DateTime
return $dateTime;
}

public function regularClosingDays(): array
{
return array_keys($this->filter(function (OpeningHoursForDay $openingHoursForDay) {
return $openingHoursForDay->isEmpty();
}));
}

public function regularClosingDaysISO(): array
{
return Arr::map($this->regularClosingDays(), [Day::class, 'toISO']);
}

public function exceptionalClosingDates(): array
{
$dates = array_keys($this->filterExceptions(function (OpeningHoursForDay $openingHoursForDay, $date) {
return $openingHoursForDay->isEmpty();
}));

return Arr::map($dates, function ($date) {
return DateTime::createFromFormat('Y-m-d', $date);
});
}

public function setTimezone($timezone)
{
$this->timezone = new DateTimeZone($timezone);
Expand Down Expand Up @@ -205,6 +228,11 @@ protected function applyTimezone(DateTimeInterface $date)
return $date;
}

public function filter(callable $callback): array
{
return Arr::filter($this->openingHours, $callback);
}

public function map(callable $callback): array
{
return Arr::map($this->openingHours, $callback);
Expand All @@ -215,6 +243,11 @@ public function flatMap(callable $callback): array
return Arr::flatMap($this->openingHours, $callback);
}

public function filterExceptions(callable $callback): array
{
return Arr::filter($this->exceptions, $callback);
}

public function mapExceptions(callable $callback): array
{
return Arr::map($this->exceptions, $callback);
Expand Down
49 changes: 49 additions & 0 deletions tests/OpeningHoursTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,53 @@ public function it_can_determine_that_its_closed_now()

$this->assertTrue($openingHours->isClosed());
}

/** @test */
public function it_can_retrieve_regular_closing_days_as_strings()
{
$openingHours = OpeningHours::create([
'monday' => ['09:00-18:00'],
'tuesday' => ['09:00-18:00'],
'wednesday' => ['09:00-18:00'],
'thursday' => ['09:00-18:00'],
'friday' => ['09:00-18:00'],
'saturday' => [],
'sunday' => [],
]);

$this->assertEquals(['saturday', 'sunday'], $openingHours->regularClosingDays());
}

/** @test */
public function it_can_retrieve_regular_closing_days_as_iso_numbers()
{
$openingHours = OpeningHours::create([
'monday' => ['09:00-18:00'],
'tuesday' => ['09:00-18:00'],
'wednesday' => ['09:00-18:00'],
'thursday' => ['09:00-18:00'],
'friday' => ['09:00-18:00'],
'saturday' => [],
'sunday' => [],
]);

$this->assertEquals([6, 7], $openingHours->regularClosingDaysISO());
}

/** @test */
public function it_can_retrieve_a_list_of_exceptional_closing_dates()
{
$openingHours = OpeningHours::create([
'exceptions' => [
'2017-06-01' => [],
'2017-06-02' => [],
],
]);

$exceptionalClosingDates = $openingHours->exceptionalClosingDates();

$this->assertCount(2, $exceptionalClosingDates);
$this->assertEquals('2017-06-01', $exceptionalClosingDates[0]->format('Y-m-d'));
$this->assertEquals('2017-06-02', $exceptionalClosingDates[1]->format('Y-m-d'));
}
}

0 comments on commit 80426b7

Please sign in to comment.