From 7ca024ac2f295881efbd822280c1f6e1da8ef062 Mon Sep 17 00:00:00 2001 From: Bobby Bouwmann Date: Thu, 13 Oct 2016 13:44:21 +0200 Subject: [PATCH 1/8] Add the posibility to set a timezone --- src/OpeningHours.php | 25 ++++++++++++++++++++++++- tests/OpeningHoursTest.php | 21 ++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/OpeningHours.php b/src/OpeningHours.php index 72504d9..6eb11e0 100644 --- a/src/OpeningHours.php +++ b/src/OpeningHours.php @@ -4,6 +4,7 @@ use DateTime; use DateTimeInterface; +use DateTimeZone; use Spatie\OpeningHours\Exceptions\Exception; use Spatie\OpeningHours\Exceptions\InvalidDate; use Spatie\OpeningHours\Exceptions\InvalidDayName; @@ -17,8 +18,13 @@ class OpeningHours /** @var array */ protected $exceptions = []; - public function __construct() + /** @var DateTimeZone|null */ + protected $timezone; + + public function __construct($timezone = null) { + $this->timezone = $timezone ? new DateTimeZone($timezone) : null; + $this->openingHours = Day::mapDays(function () { return new OpeningHoursForDay(); }); @@ -77,6 +83,8 @@ public function forDay(string $day): OpeningHoursForDay public function forDate(DateTimeInterface $date): OpeningHoursForDay { + $date = $this->setTimezone($date); + return $this->exceptions[$date->format('Y-m-d')] ?? $this->forDay(Day::onDateTime($date)); } @@ -97,6 +105,10 @@ public function isClosedOn(string $day): bool public function isOpenAt(DateTimeInterface $dateTime): bool { + $dateTime = $this->setTimezone($dateTime); + + var_dump($dateTime, $this->timezone); + $openingHoursForDay = $this->forDate($dateTime); return $openingHoursForDay->isOpenAt(Time::fromDateTime($dateTime)); @@ -104,6 +116,8 @@ public function isOpenAt(DateTimeInterface $dateTime): bool public function isClosedAt(DateTimeInterface $dateTime): bool { + $dateTime = $this->setTimezone($dateTime); + return ! $this->isOpenAt($dateTime); } @@ -159,4 +173,13 @@ protected function normalizeDayName(string $day) return $day; } + + protected function setTimezone(DateTimeInterface $date) + { + if ($this->timezone) { + $date->setTimezone($this->timezone); + } + + return $date; + } } diff --git a/tests/OpeningHoursTest.php b/tests/OpeningHoursTest.php index b957580..ddc18d2 100644 --- a/tests/OpeningHoursTest.php +++ b/tests/OpeningHoursTest.php @@ -3,6 +3,7 @@ namespace Spatie\OpeningHours\Test; use DateTime; +use DateTimeZone; use Spatie\OpeningHours\OpeningHours; class OpeningHoursTest extends \PHPUnit_Framework_TestCase @@ -125,4 +126,22 @@ public function it_can_determine_that_its_open_at_a_certain_date_and_time_on_an_ $this->assertFalse($openingHours->isOpenAt($shouldBeClosed)); $this->assertTrue($openingHours->isClosedAt($shouldBeClosed)); } -} + + /** @test */ + public function it_can_set_the_timezone_on_the_openings_hours_object() + { + $openingHours = new OpeningHours('Europe/Amsterdam'); + $openingHours->fill([ + 'monday' => ['09:00-18:00'], + ]); + + $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 10:00'))); + $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 15:59'))); // 2 Hours difference + $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 08:00'))); // 2 Hours difference + $this->assertFalse($openingHours->isOpenAt(new DateTime('2016-10-10 06:00'))); // 2 Hours difference + + $this->assertFalse($openingHours->isOpenAt(new DateTime('2016-10-10 06:00', new DateTimeZone('Europe/Amsterdam')))); + $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 09:00', new DateTimeZone('Europe/Amsterdam')))); + $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 17:59', new DateTimeZone('Europe/Amsterdam')))); + } +} \ No newline at end of file From eece1d5f4374048c2888766e6979acbdd638d378 Mon Sep 17 00:00:00 2001 From: Bobby Bouwmann Date: Thu, 13 Oct 2016 13:46:53 +0200 Subject: [PATCH 2/8] Fix styleci --- tests/OpeningHoursTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/OpeningHoursTest.php b/tests/OpeningHoursTest.php index ddc18d2..4eff5d6 100644 --- a/tests/OpeningHoursTest.php +++ b/tests/OpeningHoursTest.php @@ -144,4 +144,4 @@ public function it_can_set_the_timezone_on_the_openings_hours_object() $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 09:00', new DateTimeZone('Europe/Amsterdam')))); $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 17:59', new DateTimeZone('Europe/Amsterdam')))); } -} \ No newline at end of file +} From c71509c9b53d01afe4e51c63200dd2432c5efe3f Mon Sep 17 00:00:00 2001 From: Bobby Bouwmann Date: Thu, 13 Oct 2016 13:55:16 +0200 Subject: [PATCH 3/8] Remove var_dump --- src/OpeningHours.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/OpeningHours.php b/src/OpeningHours.php index 6eb11e0..03bfcc6 100644 --- a/src/OpeningHours.php +++ b/src/OpeningHours.php @@ -107,8 +107,6 @@ public function isOpenAt(DateTimeInterface $dateTime): bool { $dateTime = $this->setTimezone($dateTime); - var_dump($dateTime, $this->timezone); - $openingHoursForDay = $this->forDate($dateTime); return $openingHoursForDay->isOpenAt(Time::fromDateTime($dateTime)); From f9f5d1dabc880a5ab7415ef5634f5c92aaa5fafd Mon Sep 17 00:00:00 2001 From: Bobby Bouwmann Date: Fri, 14 Oct 2016 08:45:13 +0200 Subject: [PATCH 4/8] Add extra tests --- tests/OpeningHoursTest.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/tests/OpeningHoursTest.php b/tests/OpeningHoursTest.php index 4eff5d6..a2f7b5c 100644 --- a/tests/OpeningHoursTest.php +++ b/tests/OpeningHoursTest.php @@ -133,15 +133,28 @@ public function it_can_set_the_timezone_on_the_openings_hours_object() $openingHours = new OpeningHours('Europe/Amsterdam'); $openingHours->fill([ 'monday' => ['09:00-18:00'], + 'exceptions' => [ + '2016-11-14' => ['09:00-13:00'], + ] ]); $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 10:00'))); - $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 15:59'))); // 2 Hours difference - $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 08:00'))); // 2 Hours difference - $this->assertFalse($openingHours->isOpenAt(new DateTime('2016-10-10 06:00'))); // 2 Hours difference + $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 15:59'))); + $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 08:00'))); + $this->assertFalse($openingHours->isOpenAt(new DateTime('2016-10-10 06:00'))); $this->assertFalse($openingHours->isOpenAt(new DateTime('2016-10-10 06:00', new DateTimeZone('Europe/Amsterdam')))); $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 09:00', new DateTimeZone('Europe/Amsterdam')))); $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 17:59', new DateTimeZone('Europe/Amsterdam')))); + + $this->assertFalse($openingHours->isOpenAt(new DateTime('2016-11-14 17:59', new DateTimeZone('Europe/Amsterdam')))); + $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-11-14 12:59', new DateTimeZone('Europe/Amsterdam')))); + + $this->assertFalse($openingHours->isOpenAt(new DateTime('2016-11-14 15:59', new DateTimeZone('America/Denver')))); + $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 09:59', new DateTimeZone('America/Denver')))); + + date_default_timezone_set ( 'America/Denver' ); + $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 09:59'))); + $this->assertFalse($openingHours->isOpenAt(new DateTime('2016-10-10 10:00'))); } } From 584bd939da83945c807bed91df6f18ba14af5ed1 Mon Sep 17 00:00:00 2001 From: Bobby Bouwmann Date: Fri, 14 Oct 2016 08:46:52 +0200 Subject: [PATCH 5/8] Fix StyleCI --- tests/OpeningHoursTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/OpeningHoursTest.php b/tests/OpeningHoursTest.php index a2f7b5c..b528294 100644 --- a/tests/OpeningHoursTest.php +++ b/tests/OpeningHoursTest.php @@ -135,12 +135,12 @@ public function it_can_set_the_timezone_on_the_openings_hours_object() 'monday' => ['09:00-18:00'], 'exceptions' => [ '2016-11-14' => ['09:00-13:00'], - ] + ], ]); $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 10:00'))); $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 15:59'))); - $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 08:00'))); + $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 08:00'))); $this->assertFalse($openingHours->isOpenAt(new DateTime('2016-10-10 06:00'))); $this->assertFalse($openingHours->isOpenAt(new DateTime('2016-10-10 06:00', new DateTimeZone('Europe/Amsterdam')))); @@ -153,8 +153,8 @@ public function it_can_set_the_timezone_on_the_openings_hours_object() $this->assertFalse($openingHours->isOpenAt(new DateTime('2016-11-14 15:59', new DateTimeZone('America/Denver')))); $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 09:59', new DateTimeZone('America/Denver')))); - date_default_timezone_set ( 'America/Denver' ); + date_default_timezone_set('America/Denver'); $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 09:59'))); - $this->assertFalse($openingHours->isOpenAt(new DateTime('2016-10-10 10:00'))); + $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 10:00'))); } } From 8859a67982296baa0efccc9eb120cc119eb008c6 Mon Sep 17 00:00:00 2001 From: Bobby Bouwmann Date: Fri, 14 Oct 2016 08:49:57 +0200 Subject: [PATCH 6/8] Fix tests --- tests/OpeningHoursTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/OpeningHoursTest.php b/tests/OpeningHoursTest.php index b528294..14fdab2 100644 --- a/tests/OpeningHoursTest.php +++ b/tests/OpeningHoursTest.php @@ -155,6 +155,6 @@ public function it_can_set_the_timezone_on_the_openings_hours_object() date_default_timezone_set('America/Denver'); $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 09:59'))); - $this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 10:00'))); + $this->assertFalse($openingHours->isOpenAt(new DateTime('2016-10-10 10:00'))); } } From 5db4bd95ad8f444803812eba494342e0bc449b61 Mon Sep 17 00:00:00 2001 From: Bobby Bouwmann Date: Fri, 14 Oct 2016 13:00:23 +0200 Subject: [PATCH 7/8] Fix for immutable date objects --- src/OpeningHours.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpeningHours.php b/src/OpeningHours.php index 03bfcc6..5cb18d2 100644 --- a/src/OpeningHours.php +++ b/src/OpeningHours.php @@ -175,7 +175,7 @@ protected function normalizeDayName(string $day) protected function setTimezone(DateTimeInterface $date) { if ($this->timezone) { - $date->setTimezone($this->timezone); + $date = $date->setTimezone($this->timezone); } return $date; From 14929985f6e2fdbd0c08d3d055ed69aa1ef874a3 Mon Sep 17 00:00:00 2001 From: Bobby Bouwmann Date: Tue, 8 Nov 2016 15:15:41 +0100 Subject: [PATCH 8/8] Rename to applyTimezone + fluently set timezone --- src/OpeningHours.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/OpeningHours.php b/src/OpeningHours.php index 5cb18d2..8e7bc36 100644 --- a/src/OpeningHours.php +++ b/src/OpeningHours.php @@ -83,7 +83,7 @@ public function forDay(string $day): OpeningHoursForDay public function forDate(DateTimeInterface $date): OpeningHoursForDay { - $date = $this->setTimezone($date); + $date = $this->applyTimezone($date); return $this->exceptions[$date->format('Y-m-d')] ?? $this->forDay(Day::onDateTime($date)); } @@ -105,7 +105,7 @@ public function isClosedOn(string $day): bool public function isOpenAt(DateTimeInterface $dateTime): bool { - $dateTime = $this->setTimezone($dateTime); + $dateTime = $this->applyTimezone($dateTime); $openingHoursForDay = $this->forDate($dateTime); @@ -114,8 +114,6 @@ public function isOpenAt(DateTimeInterface $dateTime): bool public function isClosedAt(DateTimeInterface $dateTime): bool { - $dateTime = $this->setTimezone($dateTime); - return ! $this->isOpenAt($dateTime); } @@ -129,6 +127,11 @@ public function isClosed(): bool return $this->isClosedAt(new DateTime()); } + public function setTimezone($timezone) + { + $this->timezone = new DateTimeZone($timezone); + } + protected function parseOpeningHoursAndExceptions(array $data): array { $exceptions = Arr::pull($data, 'exceptions', []); @@ -172,7 +175,7 @@ protected function normalizeDayName(string $day) return $day; } - protected function setTimezone(DateTimeInterface $date) + protected function applyTimezone(DateTimeInterface $date) { if ($this->timezone) { $date = $date->setTimezone($this->timezone);