1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright Andreas Heigl <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Licenses under the MIT-license. For details see the included file LICENSE.md |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Org_Heigl\Holidaychecker; |
12
|
|
|
|
13
|
|
|
use DateTimeInterface; |
14
|
|
|
use IntlCalendar; |
15
|
|
|
use RuntimeException; |
16
|
|
|
use UnexpectedValueException; |
17
|
|
|
use function method_exists; |
18
|
|
|
use function sprintf; |
19
|
|
|
use function strtolower; |
20
|
|
|
|
21
|
|
|
final class GregorianWeekday |
22
|
|
|
{ |
23
|
|
|
private const MONDAY = 'monday'; |
24
|
|
|
private const TUESDAY = 'tuesday'; |
25
|
|
|
private const WEDNESDAY = 'wednesday'; |
26
|
|
|
private const THURSDAY = 'thursday'; |
27
|
|
|
private const FRIDAY = 'friday'; |
28
|
|
|
private const SATURDAY = 'saturday'; |
29
|
|
|
private const SUNDAY = 'sunday'; |
30
|
|
|
/** @var array<string, GregorianWeekday> */ |
31
|
|
|
private static $instances = []; |
32
|
|
|
/** @var string */ |
33
|
|
|
private $value; |
34
|
|
|
|
35
|
|
|
private function __construct(string $value) |
36
|
|
|
{ |
37
|
|
|
$this->value = $value; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function monday(): self |
41
|
|
|
{ |
42
|
|
|
if (!isset(self::$instances[self::MONDAY])) { |
43
|
|
|
self::$instances[self::MONDAY] = new self(self::MONDAY); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return self::$instances[self::MONDAY]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function tuesday(): self |
50
|
|
|
{ |
51
|
|
|
if (!isset(self::$instances[self::TUESDAY])) { |
52
|
|
|
self::$instances[self::TUESDAY] = new self(self::TUESDAY); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return self::$instances[self::TUESDAY]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function wednesday(): self |
59
|
|
|
{ |
60
|
|
|
if (!isset(self::$instances[self::WEDNESDAY])) { |
61
|
|
|
self::$instances[self::WEDNESDAY] = new self(self::WEDNESDAY); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return self::$instances[self::WEDNESDAY]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public static function thursday(): self |
68
|
|
|
{ |
69
|
|
|
if (!isset(self::$instances[self::THURSDAY])) { |
70
|
|
|
self::$instances[self::THURSDAY] = new self(self::THURSDAY); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return self::$instances[self::THURSDAY]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public static function friday(): self |
77
|
|
|
{ |
78
|
|
|
if (!isset(self::$instances[self::FRIDAY])) { |
79
|
|
|
self::$instances[self::FRIDAY] = new self(self::FRIDAY); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return self::$instances[self::FRIDAY]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function saturday(): self |
86
|
|
|
{ |
87
|
|
|
if (!isset(self::$instances[self::SATURDAY])) { |
88
|
|
|
self::$instances[self::SATURDAY] = new self(self::SATURDAY); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return self::$instances[self::SATURDAY]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public static function sunday(): self |
95
|
|
|
{ |
96
|
|
|
if (!isset(self::$instances[self::SUNDAY])) { |
97
|
|
|
self::$instances[self::SUNDAY] = new self(self::SUNDAY); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return self::$instances[self::SUNDAY]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public static function fromDateTimeInterface(DateTimeInterface $date): self |
104
|
|
|
{ |
105
|
|
|
return self::fromString($date->format('l')); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public static function fromString(string $weekday): self |
109
|
|
|
{ |
110
|
|
|
if (!method_exists(self::class, strtolower($weekday))) { |
111
|
|
|
throw new RuntimeException(sprintf( |
112
|
|
|
'Weekday "%s" is not known', |
113
|
|
|
$weekday |
114
|
|
|
)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** @var GregorianWeekday $gregorianWeekday */ |
118
|
|
|
$gregorianWeekday = [self::class, strtolower($weekday)](); |
119
|
|
|
|
120
|
|
|
return $gregorianWeekday; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public static function fromIntlWeekday(int $weekday): self |
124
|
|
|
{ |
125
|
|
|
$mapper = [ |
126
|
|
|
IntlCalendar::DOW_SUNDAY => 'sunday', |
127
|
|
|
IntlCalendar::DOW_MONDAY => 'monday', |
128
|
|
|
IntlCalendar::DOW_TUESDAY => 'tuesday', |
129
|
|
|
IntlCalendar::DOW_WEDNESDAY => 'wednesday', |
130
|
|
|
IntlCalendar::DOW_THURSDAY => 'thursday', |
131
|
|
|
IntlCalendar::DOW_FRIDAY => 'friday', |
132
|
|
|
IntlCalendar::DOW_SATURDAY => 'saturday', |
133
|
|
|
]; |
134
|
|
|
if (!isset($mapper[$weekday])) { |
135
|
|
|
throw new UnexpectedValueException(sprintf( |
136
|
|
|
'IntlCalendar weekday %s could not be resolved', |
137
|
|
|
$weekday |
138
|
|
|
)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return self::fromString($mapper[$weekday]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function __toString(): string |
145
|
|
|
{ |
146
|
|
|
return $this->getValue(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function getValue(): string |
150
|
|
|
{ |
151
|
|
|
return $this->value; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|