Skip to content

Commit

Permalink
Remplace ->getData() with ->data
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Dec 12, 2023
1 parent dd78539 commit 1c8322e
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 90 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 9 additions & 8 deletions src/DateTimeRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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,
);
}

Expand Down
13 changes: 0 additions & 13 deletions src/Helpers/DataTrait.php

This file was deleted.

18 changes: 6 additions & 12 deletions src/OpeningHours.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@
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;

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 = [];
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 2 additions & 4 deletions src/OpeningHoursForDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions src/PreciseTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
23 changes: 8 additions & 15 deletions src/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 5 additions & 11 deletions src/TimeRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 15 additions & 15 deletions tests/OpeningHoursFillTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand All @@ -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' => [
Expand All @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down
8 changes: 4 additions & 4 deletions tests/OpeningHoursTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down

0 comments on commit 1c8322e

Please sign in to comment.