1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\OpeningHours; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use DateTimeInterface; |
7
|
|
|
use DateTimeZone; |
8
|
|
|
use Spatie\OpeningHours\Exceptions\InvalidTimeString; |
9
|
|
|
use Spatie\OpeningHours\Helpers\DataTrait; |
10
|
|
|
use Spatie\OpeningHours\Helpers\DateTimeCopier; |
11
|
|
|
|
12
|
|
|
class Time |
13
|
|
|
{ |
14
|
|
|
use DataTrait, DateTimeCopier; |
15
|
|
|
|
16
|
|
|
/** @var int */ |
17
|
|
|
protected $hours; |
18
|
|
|
|
19
|
|
|
/** @var int */ |
20
|
|
|
protected $minutes; |
21
|
|
|
|
22
|
|
|
protected function __construct(int $hours, int $minutes) |
23
|
|
|
{ |
24
|
|
|
$this->hours = $hours; |
25
|
|
|
$this->minutes = $minutes; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public static function fromString(string $string): self |
29
|
|
|
{ |
30
|
|
|
if (! preg_match('/^(([0-1][0-9]|2[0-3]):[0-5][0-9]|24:00)$/', $string)) { |
31
|
|
|
throw InvalidTimeString::forString($string); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
list($hours, $minutes) = explode(':', $string); |
35
|
|
|
|
36
|
|
|
return new self($hours, $minutes); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function hours(): int |
40
|
|
|
{ |
41
|
|
|
return $this->hours; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function minutes(): int |
45
|
|
|
{ |
46
|
|
|
return $this->minutes; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function fromDateTime(DateTimeInterface $dateTime): self |
50
|
|
|
{ |
51
|
|
|
return static::fromString($dateTime->format('H:i')); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function isSame(self $time): bool |
55
|
|
|
{ |
56
|
|
|
return $this->hours === $time->hours && $this->minutes === $time->minutes; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function isAfter(self $time): bool |
60
|
|
|
{ |
61
|
|
|
if ($this->isSame($time)) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($this->hours > $time->hours) { |
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->hours === $time->hours && $this->minutes >= $time->minutes; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function isBefore(self $time): bool |
73
|
|
|
{ |
74
|
|
|
if ($this->isSame($time)) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return ! $this->isAfter($time); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function isSameOrAfter(self $time): bool |
82
|
|
|
{ |
83
|
|
|
return $this->isSame($time) || $this->isAfter($time); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function diff(self $time): \DateInterval |
87
|
|
|
{ |
88
|
|
|
return $this->toDateTime()->diff($time->toDateTime()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function toDateTime(DateTimeInterface $date = null): DateTimeInterface |
92
|
|
|
{ |
93
|
|
|
$date = $date ? $this->copyDateTime($date) : new DateTime('1970-01-01 00:00:00'); |
94
|
|
|
|
95
|
|
|
return $date->setTime($this->hours, $this->minutes); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function format(string $format = 'H:i', $timezone = null): string |
99
|
|
|
{ |
100
|
|
|
$date = $timezone |
101
|
|
|
? new DateTime('1970-01-01 00:00:00', $timezone instanceof DateTimeZone |
102
|
|
|
? $timezone |
103
|
|
|
: new DateTimeZone($timezone) |
104
|
|
|
) |
105
|
|
|
: null; |
106
|
|
|
|
107
|
|
|
if ($format === 'H:i' && $this->hours === 24 && $this->minutes === 0) { |
108
|
|
|
return '24:00'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this->toDateTime($date)->format($format); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function __toString(): string |
115
|
|
|
{ |
116
|
|
|
return $this->format(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|