Skip to content

Commit

Permalink
- Make data and openingHours readonly
Browse files Browse the repository at this point in the history
- Make DateTimeRange, Time and PreciseTime readonly
- Add stronger types
- Drop PHP < 8.2
- Make Day an enum
  • Loading branch information
kylekatarnls committed Nov 12, 2023
1 parent cb696c2 commit 25a7042
Show file tree
Hide file tree
Showing 18 changed files with 200 additions and 288 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
php: ['7.4']
php: ['8.2']
setup: ['stable']

steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['7.4', '8.0', '8.1', '8.2', '8.3']
php: ['8.2', '8.3']
setup: ['lowest', 'stable']

name: PHP ${{ matrix.php }} - ${{ matrix.setup || 'stable' }}
Expand Down
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@
"email": "[email protected]",
"homepage": "https://spatie.be",
"role": "Developer"
},
{
"name": "kylekatarnls",
"homepage": "https://github.com/kylekatarnls",
"role": "Developer"
}
],
"require": {
"php": "^7.4 || ^8.0"
"php": "^8.2"
},
"require-dev": {
"kylekatarnls/multi-tester": "^1.1",
"phpunit/phpunit": "^9.4"
"kylekatarnls/multi-tester": "^1.4",
"phpunit/phpunit": "^10.4.2"
},
"autoload": {
"psr-4": {
Expand Down
42 changes: 20 additions & 22 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/9.4/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
cacheDirectory=".phpunit.cache"
>
<testsuites>
<testsuite name="Spatie Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage
processUncoveredFiles="true"
ignoreDeprecatedCodeUnits="true"
disableCodeCoverageIgnore="true"
>
<include>
<directory>src</directory>
</include>
<report>
<html outputDirectory="coverage" />
</report>
</coverage>
<php>
<ini name="date.timezone" value="UTC"/>
</php>
<testsuites>
<testsuite name="Spatie Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage ignoreDeprecatedCodeUnits="true" disableCodeCoverageIgnore="true">
<report>
<html outputDirectory="coverage"/>
</report>
</coverage>
<php>
<ini name="date.timezone" value="UTC"/>
</php>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
6 changes: 3 additions & 3 deletions src/DateTimeRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use DateTimeInterface;

class DateTimeRange extends TimeRange
readonly class DateTimeRange extends TimeRange
{
protected DateTimeInterface $date;

protected function __construct(DateTimeInterface $date, Time $start, Time $end, $data = null)
protected function __construct(DateTimeInterface $date, Time $start, Time $end, mixed $data = null)
{
$this->date = $date;
$startDate = $this->copyAndModify($date, $start.(
Expand All @@ -28,7 +28,7 @@ protected function __construct(DateTimeInterface $date, Time $start, Time $end,
);
}

public static function fromTimeRange(DateTimeInterface $date, TimeRange $timeRange, $data = null)
public static function fromTimeRange(DateTimeInterface $date, TimeRange $timeRange, mixed $data = null)
{
return new self($date, $timeRange->start, $timeRange->end, $data);
}
Expand Down
48 changes: 15 additions & 33 deletions src/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,28 @@
use DateTimeInterface;
use Spatie\OpeningHours\Helpers\Arr;

class Day
enum Day: string
{
const MONDAY = 'monday';
const TUESDAY = 'tuesday';
const WEDNESDAY = 'wednesday';
const THURSDAY = 'thursday';
const FRIDAY = 'friday';
const SATURDAY = 'saturday';
const SUNDAY = 'sunday';

public static function days(): array
{
return [
static::MONDAY,
static::TUESDAY,
static::WEDNESDAY,
static::THURSDAY,
static::FRIDAY,
static::SATURDAY,
static::SUNDAY,
];
}

public static function mapDays(callable $callback): array
{
return Arr::map(Arr::mirror(static::days()), $callback);
}

public static function isValid(string $day): bool
case MONDAY = 'monday';
case TUESDAY = 'tuesday';
case WEDNESDAY = 'wednesday';
case THURSDAY = 'thursday';
case FRIDAY = 'friday';
case SATURDAY = 'saturday';
case SUNDAY = 'sunday';

public static function onDateTime(DateTimeInterface $dateTime): self
{
return in_array($day, static::days());
return self::fromName($dateTime->format('l'));
}

public static function onDateTime(DateTimeInterface $dateTime): string
public static function fromName(string $day): self
{
return static::days()[$dateTime->format('N') - 1];
return self::from(strtolower($day));
}

public static function toISO(string $day): int
public function toISO(): int
{
return array_search($day, static::days()) + 1;
return array_search($this, self::cases()) + 1;
}
}
7 changes: 1 addition & 6 deletions src/Helpers/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static function flatMap(array $array, callable $callback): array
return $flattened;
}

public static function pull(&$array, $key, $default = null)
public static function pull(array &$array, string $key, mixed $default = null): mixed
{
$value = $array[$key] ?? $default;

Expand All @@ -44,11 +44,6 @@ public static function pull(&$array, $key, $default = null)
return $value;
}

public static function mirror(array $array): array
{
return array_combine($array, $array);
}

public static function createUniquePairs(array $array): array
{
$pairs = [];
Expand Down
5 changes: 2 additions & 3 deletions src/Helpers/DataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

trait DataTrait
{
/** @var mixed */
protected $data = null;
protected readonly mixed $data;

public function getData()
public function getData(): mixed
{
return $this->data;
}
Expand Down
13 changes: 9 additions & 4 deletions src/Helpers/DiffTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@

trait DiffTrait
{
private function diffInSeconds(string $stateCheckMethod, string $nextDateMethod, string $skipDateMethod, DateTimeInterface $startDate, DateTimeInterface $endDate): float
{
$time = 0;
private function diffInSeconds(
string $stateCheckMethod,
string $nextDateMethod,
string $skipDateMethod,
DateTimeInterface $startDate,
DateTimeInterface $endDate,
): float {
$time = 0.0;

if ($endDate < $startDate) {
return -$this->diffInSeconds($stateCheckMethod, $nextDateMethod, $skipDateMethod, $endDate, $startDate);
Expand All @@ -24,7 +29,7 @@ private function diffInSeconds(string $stateCheckMethod, string $nextDateMethod,
}

$nextDate = min($endDate, $this->$nextDateMethod($date, null, $endDate));
$time += floatval($nextDate->format('U.u')) - floatval($date->format('U.u'));
$time += ((float) $nextDate->format('U.u')) - ((float) $date->format('U.u'));
$date = $nextDate;
}

Expand Down
16 changes: 4 additions & 12 deletions src/Helpers/RangeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ protected function findRangeInFreeTime(Time $time, TimeRange $timeRange): ?TimeR

protected function findOpenInFreeTime(Time $time, TimeRange $timeRange): ?Time
{
$range = $this->findRangeInFreeTime($time, $timeRange);

return $range ? $range->start() : null;
return $this->findRangeInFreeTime($time, $timeRange)?->start();
}

protected function findOpenRangeInWorkingHours(Time $time, TimeRange $timeRange): ?TimeRange
Expand All @@ -26,9 +24,7 @@ protected function findOpenRangeInWorkingHours(Time $time, TimeRange $timeRange)

protected function findOpenInWorkingHours(Time $time, TimeRange $timeRange): ?Time
{
$range = $this->findOpenRangeInWorkingHours($time, $timeRange);

return $range ? $range->start() : null;
return $this->findOpenRangeInWorkingHours($time, $timeRange)?->start();
}

protected function findCloseInWorkingHours(Time $time, TimeRange $timeRange): ?Time
Expand All @@ -43,9 +39,7 @@ protected function findCloseRangeInWorkingHours(Time $time, TimeRange $timeRange

protected function findCloseInFreeTime(Time $time, TimeRange $timeRange): ?Time
{
$range = $this->findRangeInFreeTime($time, $timeRange);

return $range ? $range->end() : null;
return $this->findRangeInFreeTime($time, $timeRange)?->end();
}

protected function findPreviousRangeInFreeTime(Time $time, TimeRange $timeRange): ?TimeRange
Expand All @@ -55,9 +49,7 @@ protected function findPreviousRangeInFreeTime(Time $time, TimeRange $timeRange)

protected function findPreviousOpenInFreeTime(Time $time, TimeRange $timeRange): ?Time
{
$range = $this->findPreviousRangeInFreeTime($time, $timeRange);

return $range ? $range->start() : null;
return $this->findPreviousRangeInFreeTime($time, $timeRange)?->start();
}

protected function findPreviousCloseInWorkingHours(Time $time, TimeRange $timeRange): ?Time
Expand Down
Loading

0 comments on commit 25a7042

Please sign in to comment.