From 1c8322ef80fb843815cfdfd7c1777bc60de09da9 Mon Sep 17 00:00:00 2001 From: kylekatarnls Date: Tue, 12 Dec 2023 10:40:58 +0100 Subject: [PATCH] Remplace ->getData() with ->data --- CHANGELOG.md | 4 ++++ README.md | 6 +++--- src/DateTimeRange.php | 17 +++++++++-------- src/Helpers/DataTrait.php | 13 ------------- src/OpeningHours.php | 18 ++++++------------ src/OpeningHoursForDay.php | 6 ++---- src/PreciseTime.php | 9 ++++----- src/Time.php | 23 ++++++++--------------- src/TimeRange.php | 16 +++++----------- tests/OpeningHoursFillTest.php | 30 +++++++++++++++--------------- tests/OpeningHoursTest.php | 8 ++++---- 11 files changed, 60 insertions(+), 90 deletions(-) delete mode 100644 src/Helpers/DataTrait.php diff --git a/CHANGELOG.md b/CHANGELOG.md index ccf23ab..2d456f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `opening-hours` will be documented in this file +## 4.0.0 - upcoming + +- Replace `getData()` with readonly property `->data` + ## 3.0.0 - 2023-11-12 - Add `Time::date()` method diff --git a/README.md b/README.md index a490e0d..aa0e623 100644 --- a/README.md +++ b/README.md @@ -128,9 +128,9 @@ $openingHours = OpeningHours::create([ ], ]); -echo $openingHours->forDay('monday')->getData(); // Typical Monday -echo $openingHours->forDate(new DateTime('2016-12-25'))->getData(); // Closed for Christmas -echo $openingHours->forDay('tuesday')[2]->getData(); // Extra on Tuesday evening +echo $openingHours->forDay('monday')->data; // Typical Monday +echo $openingHours->forDate(new DateTime('2016-12-25'))->data; // Closed for Christmas +echo $openingHours->forDay('tuesday')[2]->data; // Extra on Tuesday evening ``` In the example above, data are strings but it can be any kind of value. So you can embed multiple properties in an array. diff --git a/src/DateTimeRange.php b/src/DateTimeRange.php index eae1110..23f88d5 100644 --- a/src/DateTimeRange.php +++ b/src/DateTimeRange.php @@ -6,11 +6,12 @@ readonly class DateTimeRange extends TimeRange { - protected DateTimeInterface $date; - - protected function __construct(DateTimeInterface $date, Time $start, Time $end, mixed $data = null) - { - $this->date = $date; + protected function __construct( + protected DateTimeInterface $date, + Time $start, + Time $end, + mixed $data = null, + ) { $startDate = $this->copyAndModify($date, $start.( $start > $date->format(self::TIME_FORMAT) ? ' - 1 day' @@ -22,9 +23,9 @@ protected function __construct(DateTimeInterface $date, Time $start, Time $end, : '' )); parent::__construct( - Time::fromString($start, $start->getData(), $startDate), - Time::fromString($end, $start->getData(), $endDate), - $data + Time::fromString($start, $start->data, $startDate), + Time::fromString($end, $start->data, $endDate), + $data, ); } diff --git a/src/Helpers/DataTrait.php b/src/Helpers/DataTrait.php deleted file mode 100644 index c72f19c..0000000 --- a/src/Helpers/DataTrait.php +++ /dev/null @@ -1,13 +0,0 @@ -data; - } -} diff --git a/src/OpeningHours.php b/src/OpeningHours.php index 6525f5b..b08833b 100644 --- a/src/OpeningHours.php +++ b/src/OpeningHours.php @@ -17,7 +17,6 @@ use Spatie\OpeningHours\Exceptions\MaximumLimitExceeded; use Spatie\OpeningHours\Exceptions\SearchLimitReached; use Spatie\OpeningHours\Helpers\Arr; -use Spatie\OpeningHours\Helpers\DataTrait; use Spatie\OpeningHours\Helpers\DateTimeCopier; use Spatie\OpeningHours\Helpers\DiffTrait; @@ -25,7 +24,10 @@ class OpeningHours { public const DEFAULT_DAY_LIMIT = 8; - use DataTrait, DateTimeCopier, DiffTrait; + use DateTimeCopier; + use DiffTrait; + + public readonly mixed $data; /** @var \Spatie\OpeningHours\OpeningHoursForDay[] */ protected array $openingHours = []; @@ -710,20 +712,12 @@ public function exceptionalClosingDates(): array return Arr::map($dates, static fn ($date) => DateTime::createFromFormat('Y-m-d', $date)); } - /** - * @param string|DateTimeZone|null $timezone - * @return void - */ - public function setTimezone($timezone) + public function setTimezone(string|DateTimeZone|null $timezone): void { $this->timezone = $this->parseTimezone($timezone); } - /** - * @param string|DateTimeZone|null $timezone - * @return void - */ - public function setOutputTimezone($timezone) + public function setOutputTimezone(string|DateTimeZone|null $timezone): void { $this->outputTimezone = $this->parseTimezone($timezone); } diff --git a/src/OpeningHoursForDay.php b/src/OpeningHoursForDay.php index 72a1675..d008bd2 100644 --- a/src/OpeningHoursForDay.php +++ b/src/OpeningHoursForDay.php @@ -10,20 +10,18 @@ use Spatie\OpeningHours\Exceptions\NonMutableOffsets; use Spatie\OpeningHours\Exceptions\OverlappingTimeRanges; use Spatie\OpeningHours\Helpers\Arr; -use Spatie\OpeningHours\Helpers\DataTrait; use Spatie\OpeningHours\Helpers\RangeFinder; class OpeningHoursForDay implements ArrayAccess, Countable, IteratorAggregate { - use DataTrait, RangeFinder; + use RangeFinder; private function __construct( /** @var \Spatie\OpeningHours\TimeRange[] */ protected readonly array $openingHours, - mixed $data, + public readonly mixed $data, ) { $this->guardAgainstTimeRangeOverlaps($openingHours); - $this->data = $data; } public static function fromStrings(array $strings, mixed $data = null): static diff --git a/src/PreciseTime.php b/src/PreciseTime.php index 4355a04..342ce7f 100644 --- a/src/PreciseTime.php +++ b/src/PreciseTime.php @@ -10,11 +10,10 @@ readonly class PreciseTime extends Time { - protected DateTimeInterface $dateTime; - - protected function __construct(DateTimeInterface $dateTime, mixed $data = null) - { - $this->dateTime = $dateTime; + protected function __construct( + protected DateTimeInterface $dateTime, + mixed $data = null, + ) { parent::__construct(0, 0, $data); } diff --git a/src/Time.php b/src/Time.php index d924b6d..c6d8e9a 100644 --- a/src/Time.php +++ b/src/Time.php @@ -8,25 +8,18 @@ use DateTimeInterface; use DateTimeZone; use Spatie\OpeningHours\Exceptions\InvalidTimeString; -use Spatie\OpeningHours\Helpers\DataTrait; use Spatie\OpeningHours\Helpers\DateTimeCopier; readonly class Time implements TimeDataContainer { - use DataTrait, DateTimeCopier; - - protected int $hours; - - protected int $minutes; - - protected ?DateTimeInterface $date; - - protected function __construct(int $hours, int $minutes, mixed $data = null, ?DateTimeInterface $date = null) - { - $this->hours = $hours; - $this->minutes = $minutes; - $this->data = $data; - $this->date = $date; + use DateTimeCopier; + + protected function __construct( + protected int $hours, + protected int $minutes, + public mixed $data = null, + protected ?DateTimeInterface $date = null, + ) { } public static function fromString(string $string, mixed $data = null, ?DateTimeInterface $date = null): self diff --git a/src/TimeRange.php b/src/TimeRange.php index 0cee314..e31d8fe 100644 --- a/src/TimeRange.php +++ b/src/TimeRange.php @@ -5,23 +5,17 @@ use Spatie\OpeningHours\Exceptions\InvalidTimeRangeArray; use Spatie\OpeningHours\Exceptions\InvalidTimeRangeList; use Spatie\OpeningHours\Exceptions\InvalidTimeRangeString; -use Spatie\OpeningHours\Helpers\DataTrait; use Spatie\OpeningHours\Helpers\DateTimeCopier; readonly class TimeRange implements TimeDataContainer { use DateTimeCopier; - use DataTrait; - protected Time $start; - - protected Time $end; - - protected function __construct(Time $start, Time $end, $data = null) - { - $this->start = $start; - $this->end = $end; - $this->data = $data; + protected function __construct( + protected Time $start, + protected Time $end, + public mixed $data = null, + ) { } public static function fromString(string $string, $data = null): self diff --git a/tests/OpeningHoursFillTest.php b/tests/OpeningHoursFillTest.php index 99c8ffc..d45214c 100644 --- a/tests/OpeningHoursFillTest.php +++ b/tests/OpeningHoursFillTest.php @@ -184,18 +184,18 @@ public function it_store_meta_data() ], ]); - $this->assertSame('Newyearsday opening times', $hours->exceptions()['2011-01-01']->getData()); - $this->assertSame('Newyearsday opening times', $hours->forDate(new DateTime('2011-01-01'))->getData()); - $this->assertSame('Newyearsday next day', $hours->exceptions()['2011-01-02']->getData()); - $this->assertSame('Christmas', $hours->exceptions()['12-25']->getData()); - $this->assertSame('Christmas', $hours->forDate(new DateTime('2011-12-25'))->getData()); - $this->assertNull($hours->forDay('monday')->getData()); - $this->assertSame('foobar', $hours->forDay('tuesday')->getData()); + $this->assertSame('Newyearsday opening times', $hours->exceptions()['2011-01-01']->data); + $this->assertSame('Newyearsday opening times', $hours->forDate(new DateTime('2011-01-01'))->data); + $this->assertSame('Newyearsday next day', $hours->exceptions()['2011-01-02']->data); + $this->assertSame('Christmas', $hours->exceptions()['12-25']->data); + $this->assertSame('Christmas', $hours->forDate(new DateTime('2011-12-25'))->data); + $this->assertNull($hours->forDay('monday')->data); + $this->assertSame('foobar', $hours->forDay('tuesday')->data); $this->assertSame(2, $hours->forDay('tuesday')->count()); - $this->assertSame(['foobar'], $hours->forDay('wednesday')->getData()); + $this->assertSame(['foobar'], $hours->forDay('wednesday')->data); $this->assertSame(1, $hours->forDay('wednesday')->count()); - $this->assertSame(['foobar'], $hours->forDay('thursday')[0]->getData()); - $this->assertNull($hours->forDay('thursday')[1]->getData()); + $this->assertSame(['foobar'], $hours->forDay('thursday')[0]->data); + $this->assertNull($hours->forDay('thursday')[1]->data); $hours = OpeningHours::create([ 'monday' => [ @@ -204,8 +204,8 @@ public function it_store_meta_data() ], ]); - $this->assertSame('morning', $hours->forDay('monday')[0]->getData()); - $this->assertSame('afternoon', $hours->forDay('monday')[1]->getData()); + $this->assertSame('morning', $hours->forDay('monday')[0]->data); + $this->assertSame('afternoon', $hours->forDay('monday')[1]->data); $hours = OpeningHours::create([ 'tuesday' => [ @@ -219,8 +219,8 @@ public function it_store_meta_data() ]); $this->assertSame('09:00-12:00,13:00-18:00,19:00-21:00', strval($hours->forDay('tuesday'))); - $this->assertNull($hours->forDay('tuesday')[1]->getData()); - $this->assertSame('Extra on Tuesday evening', $hours->forDay('tuesday')[2]->getData()); + $this->assertNull($hours->forDay('tuesday')[1]->data); + $this->assertSame('Extra on Tuesday evening', $hours->forDay('tuesday')[2]->data); } /** @test */ @@ -273,7 +273,7 @@ function (DateTimeImmutable $date) use ($typicalDay) { $this->assertSame('', $hours->forDate(new DateTimeImmutable('2018-04-02'))->__toString()); $this->assertSame('04-03 08:00', $hours->nextOpen(new DateTimeImmutable('2018-03-31'))->format('m-d H:i')); $this->assertSame('12-03 11:00', $hours->nextClose(new DateTimeImmutable('2018-12-03'))->format('m-d H:i')); - $this->assertSame('Month equals day', $hours->forDate(new DateTimeImmutable('2018-12-12'))->getData()); + $this->assertSame('Month equals day', $hours->forDate(new DateTimeImmutable('2018-12-12'))->data); } /** @test */ diff --git a/tests/OpeningHoursTest.php b/tests/OpeningHoursTest.php index 71a9588..411a578 100644 --- a/tests/OpeningHoursTest.php +++ b/tests/OpeningHoursTest.php @@ -1539,8 +1539,8 @@ public function testMergeOverlappingSupportsHoursAndIgnoreData() $monday = OpeningHours::create($data)->forDay('Monday'); - $this->assertNull($monday->getData()); - $this->assertNull($monday[0]->getData()); + $this->assertNull($monday->data); + $this->assertNull($monday[0]->data); $this->assertSame('09:00-23:00', (string) $monday); } @@ -1557,8 +1557,8 @@ public function testHoursRangeAreKept() $monday = OpeningHours::create($data)->forDay('Monday'); - $this->assertNull($monday->getData()); - $this->assertNull($monday[0]->getData()); + $this->assertNull($monday->data); + $this->assertNull($monday[0]->data); $this->assertSame('09:00-12:00,13:00-18:00', (string) $monday); }