@@ -34,30 +34,30 @@ |
||
34 | 34 | |
35 | 35 | class Holiday |
36 | 36 | { |
37 | - /** @var bool */ |
|
38 | - private $holiday; |
|
39 | - |
|
40 | - /** @var string */ |
|
41 | - private $name; |
|
42 | - |
|
43 | - public function __construct(bool $holiday, string $name = '') |
|
44 | - { |
|
45 | - $this->holiday = $holiday; |
|
46 | - $this->name = $name; |
|
47 | - } |
|
48 | - |
|
49 | - public function isHoliday(): bool |
|
50 | - { |
|
51 | - return $this->holiday; |
|
52 | - } |
|
53 | - |
|
54 | - public function isNamed(): bool |
|
55 | - { |
|
56 | - return $this->name !== ''; |
|
57 | - } |
|
58 | - |
|
59 | - public function getName(): string |
|
60 | - { |
|
61 | - return $this->name; |
|
62 | - } |
|
37 | + /** @var bool */ |
|
38 | + private $holiday; |
|
39 | + |
|
40 | + /** @var string */ |
|
41 | + private $name; |
|
42 | + |
|
43 | + public function __construct(bool $holiday, string $name = '') |
|
44 | + { |
|
45 | + $this->holiday = $holiday; |
|
46 | + $this->name = $name; |
|
47 | + } |
|
48 | + |
|
49 | + public function isHoliday(): bool |
|
50 | + { |
|
51 | + return $this->holiday; |
|
52 | + } |
|
53 | + |
|
54 | + public function isNamed(): bool |
|
55 | + { |
|
56 | + return $this->name !== ''; |
|
57 | + } |
|
58 | + |
|
59 | + public function getName(): string |
|
60 | + { |
|
61 | + return $this->name; |
|
62 | + } |
|
63 | 63 | } |
@@ -52,114 +52,114 @@ |
||
52 | 52 | |
53 | 53 | class HolidayIteratorFactory |
54 | 54 | { |
55 | - /** @var ItemFromDomElementCreator[] */ |
|
56 | - private $factories; |
|
57 | - |
|
58 | - /** @var DecorateFromDomElement[] */ |
|
59 | - private $decorators; |
|
60 | - |
|
61 | - public function __construct() |
|
62 | - { |
|
63 | - $this->factories = [ |
|
64 | - new EasterFactory(), |
|
65 | - new EasterOrthodoxFactory(), |
|
66 | - new DateFactory(), |
|
67 | - new DateFollowupFactory(), |
|
68 | - new RelativeFactory(), |
|
69 | - ]; |
|
70 | - |
|
71 | - $this->decorators = [ |
|
72 | - new ObservanceDecoratorFactory(), |
|
73 | - new SwapDecoratorFactory(), |
|
74 | - ]; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Create a HolidayIterator from an ISO 3166-code. |
|
79 | - * |
|
80 | - * @param string $isoCode |
|
81 | - * |
|
82 | - * @return HolidayIterator |
|
83 | - */ |
|
84 | - public function createIteratorFromISO3166(string $isoCode): HolidayIterator |
|
85 | - { |
|
86 | - $file = __DIR__ . '/../share/%s.xml'; |
|
87 | - $file1 = sprintf($file, $isoCode); |
|
88 | - |
|
89 | - if (!is_readable($file1)) { |
|
90 | - throw new UnexpectedValueException(sprintf( |
|
91 | - 'There is no holiday-file for %s', |
|
92 | - $isoCode |
|
93 | - )); |
|
94 | - } |
|
95 | - |
|
96 | - return $this->createIteratorFromXmlFile($file1); |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Create a HolidayIterator from an XML-File |
|
101 | - * |
|
102 | - * The provided XML-File has to validate against the holiday.xsd-file you |
|
103 | - * can find in this projects "share" folder. |
|
104 | - * |
|
105 | - * @param string $file |
|
106 | - * |
|
107 | - * @return HolidayIterator |
|
108 | - */ |
|
109 | - public function createIteratorFromXmlFile(string $file): HolidayIterator |
|
110 | - { |
|
111 | - $iterator = new HolidayIterator(); |
|
112 | - |
|
113 | - $dom = new DOMDocument('1.0', 'UTF-8'); |
|
114 | - $dom->load($file); |
|
115 | - $dom->xinclude(); |
|
116 | - |
|
117 | - if (!@$dom->schemaValidate(__DIR__ . '/../share/holidays.xsd')) { |
|
118 | - throw new Exception('XML-File does not validate agains schema'); |
|
119 | - } |
|
120 | - |
|
121 | - $element = $dom->documentElement; |
|
122 | - if ($element === null) { |
|
123 | - return $iterator; |
|
124 | - } |
|
125 | - foreach ($element->childNodes as $child) { |
|
126 | - if (!$child instanceof DOMElement) { |
|
127 | - continue; |
|
128 | - } |
|
129 | - if ($child->nodeName === 'resources') { |
|
130 | - continue; |
|
131 | - } |
|
132 | - |
|
133 | - try { |
|
134 | - $element = $this->getElement($child); |
|
135 | - $element = $this->decorateElement($element, $child); |
|
136 | - $iterator->append($element); |
|
137 | - } catch (Throwable $e) { |
|
138 | - // Do nothing on purpose |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - return $iterator; |
|
143 | - } |
|
144 | - |
|
145 | - private function getElement(DOMElement $child): HolidayIteratorItemInterface |
|
146 | - { |
|
147 | - foreach ($this->factories as $factory) { |
|
148 | - $element = $factory->itemFromDomElement($child); |
|
149 | - if ($element instanceof HolidayIteratorItemInterface) { |
|
150 | - return $element; |
|
151 | - } |
|
152 | - } |
|
153 | - |
|
154 | - throw new RuntimeException('Unknown element encountered'); |
|
155 | - } |
|
156 | - |
|
157 | - private function decorateElement(HolidayIteratorItemInterface $element, DOMElement $child): HolidayIteratorItemInterface |
|
158 | - { |
|
159 | - foreach ($this->decorators as $decorator) { |
|
160 | - $element = $decorator->decorate($element, $child); |
|
161 | - } |
|
162 | - |
|
163 | - return $element; |
|
164 | - } |
|
55 | + /** @var ItemFromDomElementCreator[] */ |
|
56 | + private $factories; |
|
57 | + |
|
58 | + /** @var DecorateFromDomElement[] */ |
|
59 | + private $decorators; |
|
60 | + |
|
61 | + public function __construct() |
|
62 | + { |
|
63 | + $this->factories = [ |
|
64 | + new EasterFactory(), |
|
65 | + new EasterOrthodoxFactory(), |
|
66 | + new DateFactory(), |
|
67 | + new DateFollowupFactory(), |
|
68 | + new RelativeFactory(), |
|
69 | + ]; |
|
70 | + |
|
71 | + $this->decorators = [ |
|
72 | + new ObservanceDecoratorFactory(), |
|
73 | + new SwapDecoratorFactory(), |
|
74 | + ]; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Create a HolidayIterator from an ISO 3166-code. |
|
79 | + * |
|
80 | + * @param string $isoCode |
|
81 | + * |
|
82 | + * @return HolidayIterator |
|
83 | + */ |
|
84 | + public function createIteratorFromISO3166(string $isoCode): HolidayIterator |
|
85 | + { |
|
86 | + $file = __DIR__ . '/../share/%s.xml'; |
|
87 | + $file1 = sprintf($file, $isoCode); |
|
88 | + |
|
89 | + if (!is_readable($file1)) { |
|
90 | + throw new UnexpectedValueException(sprintf( |
|
91 | + 'There is no holiday-file for %s', |
|
92 | + $isoCode |
|
93 | + )); |
|
94 | + } |
|
95 | + |
|
96 | + return $this->createIteratorFromXmlFile($file1); |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Create a HolidayIterator from an XML-File |
|
101 | + * |
|
102 | + * The provided XML-File has to validate against the holiday.xsd-file you |
|
103 | + * can find in this projects "share" folder. |
|
104 | + * |
|
105 | + * @param string $file |
|
106 | + * |
|
107 | + * @return HolidayIterator |
|
108 | + */ |
|
109 | + public function createIteratorFromXmlFile(string $file): HolidayIterator |
|
110 | + { |
|
111 | + $iterator = new HolidayIterator(); |
|
112 | + |
|
113 | + $dom = new DOMDocument('1.0', 'UTF-8'); |
|
114 | + $dom->load($file); |
|
115 | + $dom->xinclude(); |
|
116 | + |
|
117 | + if (!@$dom->schemaValidate(__DIR__ . '/../share/holidays.xsd')) { |
|
118 | + throw new Exception('XML-File does not validate agains schema'); |
|
119 | + } |
|
120 | + |
|
121 | + $element = $dom->documentElement; |
|
122 | + if ($element === null) { |
|
123 | + return $iterator; |
|
124 | + } |
|
125 | + foreach ($element->childNodes as $child) { |
|
126 | + if (!$child instanceof DOMElement) { |
|
127 | + continue; |
|
128 | + } |
|
129 | + if ($child->nodeName === 'resources') { |
|
130 | + continue; |
|
131 | + } |
|
132 | + |
|
133 | + try { |
|
134 | + $element = $this->getElement($child); |
|
135 | + $element = $this->decorateElement($element, $child); |
|
136 | + $iterator->append($element); |
|
137 | + } catch (Throwable $e) { |
|
138 | + // Do nothing on purpose |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + return $iterator; |
|
143 | + } |
|
144 | + |
|
145 | + private function getElement(DOMElement $child): HolidayIteratorItemInterface |
|
146 | + { |
|
147 | + foreach ($this->factories as $factory) { |
|
148 | + $element = $factory->itemFromDomElement($child); |
|
149 | + if ($element instanceof HolidayIteratorItemInterface) { |
|
150 | + return $element; |
|
151 | + } |
|
152 | + } |
|
153 | + |
|
154 | + throw new RuntimeException('Unknown element encountered'); |
|
155 | + } |
|
156 | + |
|
157 | + private function decorateElement(HolidayIteratorItemInterface $element, DOMElement $child): HolidayIteratorItemInterface |
|
158 | + { |
|
159 | + foreach ($this->decorators as $decorator) { |
|
160 | + $element = $decorator->decorate($element, $child); |
|
161 | + } |
|
162 | + |
|
163 | + return $element; |
|
164 | + } |
|
165 | 165 | } |
@@ -18,30 +18,30 @@ |
||
18 | 18 | |
19 | 19 | class DateFollowupFactory implements ItemFromDomElementCreator |
20 | 20 | { |
21 | - public function itemFromDomElement(DOMElement $element): ?HolidayIteratorItemInterface |
|
22 | - { |
|
23 | - if ($element->nodeName !== 'dateFollowUp') { |
|
24 | - return null; |
|
25 | - } |
|
26 | - |
|
27 | - $day = CalendarDayFactory::createCalendarDay( |
|
28 | - (int) $element->getAttribute('day'), |
|
29 | - (int) $element->getAttribute('month'), |
|
30 | - ($element->hasAttribute('calendar') ? $element->getAttribute('calendar') : 'gregorian') |
|
31 | - ); |
|
32 | - |
|
33 | - $replaced = []; |
|
34 | - if ($element->hasAttribute('replaced')) { |
|
35 | - /** @var array<"sunday"|"monday"|"tuesday"|"wednesday"|"thursday"|"friday"|"saturday"> $replaced */ |
|
36 | - $replaced = explode(' ', $element->getAttribute('replaced')); |
|
37 | - } |
|
38 | - |
|
39 | - return new DateFollowUp( |
|
40 | - $element->textContent, |
|
41 | - $element->getAttribute('free') === "true", |
|
42 | - $day, |
|
43 | - $element->getAttribute('followup'), |
|
44 | - $replaced |
|
45 | - ); |
|
46 | - } |
|
21 | + public function itemFromDomElement(DOMElement $element): ?HolidayIteratorItemInterface |
|
22 | + { |
|
23 | + if ($element->nodeName !== 'dateFollowUp') { |
|
24 | + return null; |
|
25 | + } |
|
26 | + |
|
27 | + $day = CalendarDayFactory::createCalendarDay( |
|
28 | + (int) $element->getAttribute('day'), |
|
29 | + (int) $element->getAttribute('month'), |
|
30 | + ($element->hasAttribute('calendar') ? $element->getAttribute('calendar') : 'gregorian') |
|
31 | + ); |
|
32 | + |
|
33 | + $replaced = []; |
|
34 | + if ($element->hasAttribute('replaced')) { |
|
35 | + /** @var array<"sunday"|"monday"|"tuesday"|"wednesday"|"thursday"|"friday"|"saturday"> $replaced */ |
|
36 | + $replaced = explode(' ', $element->getAttribute('replaced')); |
|
37 | + } |
|
38 | + |
|
39 | + return new DateFollowUp( |
|
40 | + $element->textContent, |
|
41 | + $element->getAttribute('free') === "true", |
|
42 | + $day, |
|
43 | + $element->getAttribute('followup'), |
|
44 | + $replaced |
|
45 | + ); |
|
46 | + } |
|
47 | 47 | } |
@@ -43,61 +43,61 @@ |
||
43 | 43 | |
44 | 44 | class Easter implements HolidayIteratorItemInterface |
45 | 45 | { |
46 | - /** @var int */ |
|
47 | - private $offset; |
|
48 | - |
|
49 | - /** @var bool */ |
|
50 | - private $holiday; |
|
51 | - |
|
52 | - /** @var string */ |
|
53 | - private $name; |
|
54 | - |
|
55 | - public function __construct(string $name, bool $holiday, int $offset) |
|
56 | - { |
|
57 | - $this->offset = $offset; |
|
58 | - $this->holiday = $holiday; |
|
59 | - $this->name = $name; |
|
60 | - } |
|
61 | - |
|
62 | - public function dateMatches(DateTimeInterface $date): bool |
|
63 | - { |
|
64 | - $year = (int) $date->format('Y'); |
|
65 | - |
|
66 | - $easter = $this->getEaster($year); |
|
67 | - $day = $this->getOffsetDay($easter, $this->offset); |
|
68 | - |
|
69 | - $comparator = new DateIntervalComparator(); |
|
70 | - return 0 > $comparator->compare($day->diff($date), new DateInterval('P1D')); |
|
71 | - } |
|
72 | - |
|
73 | - protected function getEaster(int $year): DateTimeImmutable |
|
74 | - { |
|
75 | - $base = new DateTimeImmutable($year . "-03-21", new DateTimeZone('UTC')); |
|
76 | - $days = easter_days($year); |
|
77 | - |
|
78 | - return $base->add(new DateInterval("P{$days}D")); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * @param DateTime|DateTimeImmutable $date |
|
83 | - * @return DateTime|DateTimeImmutable |
|
84 | - */ |
|
85 | - private function getOffsetDay($date, int $offset) |
|
86 | - { |
|
87 | - if ($offset < 0) { |
|
88 | - return $date->sub(new DateInterval('P' . $offset * -1 . 'D')); |
|
89 | - } |
|
90 | - |
|
91 | - return $date->add(new DateInterval('P' . $offset . 'D')); |
|
92 | - } |
|
93 | - |
|
94 | - public function getName(): string |
|
95 | - { |
|
96 | - return $this->name; |
|
97 | - } |
|
98 | - |
|
99 | - public function isHoliday(): bool |
|
100 | - { |
|
101 | - return $this->holiday; |
|
102 | - } |
|
46 | + /** @var int */ |
|
47 | + private $offset; |
|
48 | + |
|
49 | + /** @var bool */ |
|
50 | + private $holiday; |
|
51 | + |
|
52 | + /** @var string */ |
|
53 | + private $name; |
|
54 | + |
|
55 | + public function __construct(string $name, bool $holiday, int $offset) |
|
56 | + { |
|
57 | + $this->offset = $offset; |
|
58 | + $this->holiday = $holiday; |
|
59 | + $this->name = $name; |
|
60 | + } |
|
61 | + |
|
62 | + public function dateMatches(DateTimeInterface $date): bool |
|
63 | + { |
|
64 | + $year = (int) $date->format('Y'); |
|
65 | + |
|
66 | + $easter = $this->getEaster($year); |
|
67 | + $day = $this->getOffsetDay($easter, $this->offset); |
|
68 | + |
|
69 | + $comparator = new DateIntervalComparator(); |
|
70 | + return 0 > $comparator->compare($day->diff($date), new DateInterval('P1D')); |
|
71 | + } |
|
72 | + |
|
73 | + protected function getEaster(int $year): DateTimeImmutable |
|
74 | + { |
|
75 | + $base = new DateTimeImmutable($year . "-03-21", new DateTimeZone('UTC')); |
|
76 | + $days = easter_days($year); |
|
77 | + |
|
78 | + return $base->add(new DateInterval("P{$days}D")); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * @param DateTime|DateTimeImmutable $date |
|
83 | + * @return DateTime|DateTimeImmutable |
|
84 | + */ |
|
85 | + private function getOffsetDay($date, int $offset) |
|
86 | + { |
|
87 | + if ($offset < 0) { |
|
88 | + return $date->sub(new DateInterval('P' . $offset * -1 . 'D')); |
|
89 | + } |
|
90 | + |
|
91 | + return $date->add(new DateInterval('P' . $offset . 'D')); |
|
92 | + } |
|
93 | + |
|
94 | + public function getName(): string |
|
95 | + { |
|
96 | + return $this->name; |
|
97 | + } |
|
98 | + |
|
99 | + public function isHoliday(): bool |
|
100 | + { |
|
101 | + return $this->holiday; |
|
102 | + } |
|
103 | 103 | } |
@@ -41,83 +41,83 @@ |
||
41 | 41 | |
42 | 42 | class DateFollowUp implements HolidayIteratorItemInterface |
43 | 43 | { |
44 | - /** @var CalendarDay */ |
|
45 | - private $day; |
|
46 | - |
|
47 | - /** @var bool */ |
|
48 | - private $holiday; |
|
49 | - |
|
50 | - /** @var string */ |
|
51 | - private $name; |
|
52 | - |
|
53 | - /** @var string */ |
|
54 | - private $followup; |
|
55 | - |
|
56 | - /** @var IntlCalendar::DOW*[] */ |
|
57 | - private $replaced; |
|
58 | - |
|
59 | - /** |
|
60 | - * @param array<"sunday"|"monday"|"tuesday"|"wednesday"|"thursday"|"friday"|"saturday"> $replaced |
|
61 | - */ |
|
62 | - public function __construct(string $name, bool $holiday, CalendarDay $day, string $followup, array $replaced = []) |
|
63 | - { |
|
64 | - $this->day = $day; |
|
65 | - $this->followup = $followup; |
|
66 | - $this->holiday = $holiday; |
|
67 | - $this->name = $name; |
|
68 | - $this->replaced = $this->replacedDays($replaced); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @param array<"sunday"|"monday"|"tuesday"|"wednesday"|"thursday"|"friday"|"saturday"|"foo"> $replaced |
|
73 | - * @return IntlCalendar::DOW_*[] |
|
74 | - */ |
|
75 | - private static function replacedDays(array $replaced): array |
|
76 | - { |
|
77 | - $daymap = [ |
|
78 | - 'sunday' => IntlCalendar::DOW_SUNDAY, |
|
79 | - 'monday' => IntlCalendar::DOW_MONDAY, |
|
80 | - 'tuesday' => IntlCalendar::DOW_TUESDAY, |
|
81 | - 'wednesday' => IntlCalendar::DOW_WEDNESDAY, |
|
82 | - 'thursday' => IntlCalendar::DOW_THURSDAY, |
|
83 | - 'friday' => IntlCalendar::DOW_FRIDAY, |
|
84 | - 'saturday' => IntlCalendar::DOW_SATURDAY, |
|
85 | - ]; |
|
86 | - |
|
87 | - if ([] === $replaced) { |
|
88 | - return [ |
|
89 | - IntlCalendar::DOW_SATURDAY, |
|
90 | - IntlCalendar::DOW_SUNDAY, |
|
91 | - ]; |
|
92 | - } |
|
93 | - |
|
94 | - return array_filter(array_map(function (string $day) use ($daymap) { |
|
95 | - if (!isset($daymap[$day])) { |
|
96 | - return null; |
|
97 | - } |
|
98 | - return $daymap[$day]; |
|
99 | - }, $replaced)); |
|
100 | - } |
|
101 | - |
|
102 | - public function dateMatches(DateTimeInterface $date): bool |
|
103 | - { |
|
104 | - $gregorianYear = (int) $date->format('Y'); |
|
105 | - $weekday = $this->day->getWeekdayForGregorianYear($gregorianYear); |
|
106 | - |
|
107 | - if (in_array($weekday, $this->replaced)) { |
|
108 | - return $this->day->isFollowUpDay($date, $this->followup); |
|
109 | - } |
|
110 | - |
|
111 | - return $this->day->isSameDay($date); |
|
112 | - } |
|
113 | - |
|
114 | - public function getName(): string |
|
115 | - { |
|
116 | - return $this->name; |
|
117 | - } |
|
118 | - |
|
119 | - public function isHoliday(): bool |
|
120 | - { |
|
121 | - return $this->holiday; |
|
122 | - } |
|
44 | + /** @var CalendarDay */ |
|
45 | + private $day; |
|
46 | + |
|
47 | + /** @var bool */ |
|
48 | + private $holiday; |
|
49 | + |
|
50 | + /** @var string */ |
|
51 | + private $name; |
|
52 | + |
|
53 | + /** @var string */ |
|
54 | + private $followup; |
|
55 | + |
|
56 | + /** @var IntlCalendar::DOW*[] */ |
|
57 | + private $replaced; |
|
58 | + |
|
59 | + /** |
|
60 | + * @param array<"sunday"|"monday"|"tuesday"|"wednesday"|"thursday"|"friday"|"saturday"> $replaced |
|
61 | + */ |
|
62 | + public function __construct(string $name, bool $holiday, CalendarDay $day, string $followup, array $replaced = []) |
|
63 | + { |
|
64 | + $this->day = $day; |
|
65 | + $this->followup = $followup; |
|
66 | + $this->holiday = $holiday; |
|
67 | + $this->name = $name; |
|
68 | + $this->replaced = $this->replacedDays($replaced); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @param array<"sunday"|"monday"|"tuesday"|"wednesday"|"thursday"|"friday"|"saturday"|"foo"> $replaced |
|
73 | + * @return IntlCalendar::DOW_*[] |
|
74 | + */ |
|
75 | + private static function replacedDays(array $replaced): array |
|
76 | + { |
|
77 | + $daymap = [ |
|
78 | + 'sunday' => IntlCalendar::DOW_SUNDAY, |
|
79 | + 'monday' => IntlCalendar::DOW_MONDAY, |
|
80 | + 'tuesday' => IntlCalendar::DOW_TUESDAY, |
|
81 | + 'wednesday' => IntlCalendar::DOW_WEDNESDAY, |
|
82 | + 'thursday' => IntlCalendar::DOW_THURSDAY, |
|
83 | + 'friday' => IntlCalendar::DOW_FRIDAY, |
|
84 | + 'saturday' => IntlCalendar::DOW_SATURDAY, |
|
85 | + ]; |
|
86 | + |
|
87 | + if ([] === $replaced) { |
|
88 | + return [ |
|
89 | + IntlCalendar::DOW_SATURDAY, |
|
90 | + IntlCalendar::DOW_SUNDAY, |
|
91 | + ]; |
|
92 | + } |
|
93 | + |
|
94 | + return array_filter(array_map(function (string $day) use ($daymap) { |
|
95 | + if (!isset($daymap[$day])) { |
|
96 | + return null; |
|
97 | + } |
|
98 | + return $daymap[$day]; |
|
99 | + }, $replaced)); |
|
100 | + } |
|
101 | + |
|
102 | + public function dateMatches(DateTimeInterface $date): bool |
|
103 | + { |
|
104 | + $gregorianYear = (int) $date->format('Y'); |
|
105 | + $weekday = $this->day->getWeekdayForGregorianYear($gregorianYear); |
|
106 | + |
|
107 | + if (in_array($weekday, $this->replaced)) { |
|
108 | + return $this->day->isFollowUpDay($date, $this->followup); |
|
109 | + } |
|
110 | + |
|
111 | + return $this->day->isSameDay($date); |
|
112 | + } |
|
113 | + |
|
114 | + public function getName(): string |
|
115 | + { |
|
116 | + return $this->name; |
|
117 | + } |
|
118 | + |
|
119 | + public function isHoliday(): bool |
|
120 | + { |
|
121 | + return $this->holiday; |
|
122 | + } |
|
123 | 123 | } |
@@ -91,7 +91,7 @@ |
||
91 | 91 | ]; |
92 | 92 | } |
93 | 93 | |
94 | - return array_filter(array_map(function (string $day) use ($daymap) { |
|
94 | + return array_filter(array_map(function(string $day) use ($daymap) { |
|
95 | 95 | if (!isset($daymap[$day])) { |
96 | 96 | return null; |
97 | 97 | } |
@@ -39,58 +39,58 @@ |
||
39 | 39 | |
40 | 40 | class Relative implements HolidayIteratorItemInterface |
41 | 41 | { |
42 | - /** @var int */ |
|
43 | - private $day; |
|
44 | - |
|
45 | - /** @var int */ |
|
46 | - private $month; |
|
47 | - |
|
48 | - /** @var string */ |
|
49 | - private $relation; |
|
50 | - |
|
51 | - /** @var bool */ |
|
52 | - private $holiday; |
|
53 | - |
|
54 | - /** @var string */ |
|
55 | - private $name; |
|
56 | - |
|
57 | - public function __construct(string $name, bool $holiday, int $day, int $month, string $relation) |
|
58 | - { |
|
59 | - $this->day = $day; |
|
60 | - $this->month = $month; |
|
61 | - $this->relation = $relation; |
|
62 | - $this->holiday = $holiday; |
|
63 | - $this->name = $name; |
|
64 | - } |
|
65 | - |
|
66 | - public function dateMatches(DateTimeInterface $date): bool |
|
67 | - { |
|
68 | - $year = (int) $date->format('Y'); |
|
69 | - |
|
70 | - $day = new DateTimeImmutable(sprintf( |
|
71 | - '%s-%s-%s', |
|
72 | - $year, |
|
73 | - $this->month, |
|
74 | - $this->day |
|
75 | - )); |
|
76 | - |
|
77 | - /** @var DateTimeImmutable|false $day */ |
|
78 | - $day = $day->modify($this->relation); |
|
79 | - |
|
80 | - if ($day === false) { |
|
81 | - return false; |
|
82 | - } |
|
83 | - |
|
84 | - return $date->format('Y-m-d') === $day->format('Y-m-d'); |
|
85 | - } |
|
86 | - |
|
87 | - public function getName(): string |
|
88 | - { |
|
89 | - return $this->name; |
|
90 | - } |
|
91 | - |
|
92 | - public function isHoliday(): bool |
|
93 | - { |
|
94 | - return $this->holiday; |
|
95 | - } |
|
42 | + /** @var int */ |
|
43 | + private $day; |
|
44 | + |
|
45 | + /** @var int */ |
|
46 | + private $month; |
|
47 | + |
|
48 | + /** @var string */ |
|
49 | + private $relation; |
|
50 | + |
|
51 | + /** @var bool */ |
|
52 | + private $holiday; |
|
53 | + |
|
54 | + /** @var string */ |
|
55 | + private $name; |
|
56 | + |
|
57 | + public function __construct(string $name, bool $holiday, int $day, int $month, string $relation) |
|
58 | + { |
|
59 | + $this->day = $day; |
|
60 | + $this->month = $month; |
|
61 | + $this->relation = $relation; |
|
62 | + $this->holiday = $holiday; |
|
63 | + $this->name = $name; |
|
64 | + } |
|
65 | + |
|
66 | + public function dateMatches(DateTimeInterface $date): bool |
|
67 | + { |
|
68 | + $year = (int) $date->format('Y'); |
|
69 | + |
|
70 | + $day = new DateTimeImmutable(sprintf( |
|
71 | + '%s-%s-%s', |
|
72 | + $year, |
|
73 | + $this->month, |
|
74 | + $this->day |
|
75 | + )); |
|
76 | + |
|
77 | + /** @var DateTimeImmutable|false $day */ |
|
78 | + $day = $day->modify($this->relation); |
|
79 | + |
|
80 | + if ($day === false) { |
|
81 | + return false; |
|
82 | + } |
|
83 | + |
|
84 | + return $date->format('Y-m-d') === $day->format('Y-m-d'); |
|
85 | + } |
|
86 | + |
|
87 | + public function getName(): string |
|
88 | + { |
|
89 | + return $this->name; |
|
90 | + } |
|
91 | + |
|
92 | + public function isHoliday(): bool |
|
93 | + { |
|
94 | + return $this->holiday; |
|
95 | + } |
|
96 | 96 | } |
@@ -12,37 +12,37 @@ |
||
12 | 12 | |
13 | 13 | class SwapRule |
14 | 14 | { |
15 | - /** @var SwapDirection */ |
|
16 | - private $swapDirection; |
|
17 | - |
|
18 | - /** @var GregorianWeekday */ |
|
19 | - private $swapToDay; |
|
20 | - |
|
21 | - /** @var GregorianWeekday[] */ |
|
22 | - private $swapWhenDay; |
|
23 | - |
|
24 | - public function __construct(SwapDirection $direction, GregorianWeekday $swapToGregorianDay, GregorianWeekday ...$swapWhenGregorianDay) |
|
25 | - { |
|
26 | - $this->swapDirection = $direction; |
|
27 | - $this->swapToDay = $swapToGregorianDay; |
|
28 | - $this->swapWhenDay = $swapWhenGregorianDay; |
|
29 | - } |
|
30 | - |
|
31 | - public function getDirection(): SwapDirection |
|
32 | - { |
|
33 | - return $this->swapDirection; |
|
34 | - } |
|
35 | - |
|
36 | - public function getSwapToDay(): GregorianWeekday |
|
37 | - { |
|
38 | - return $this->swapToDay; |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * @return GregorianWeekday[] |
|
43 | - */ |
|
44 | - public function getSwapWhenDays(): array |
|
45 | - { |
|
46 | - return $this->swapWhenDay; |
|
47 | - } |
|
15 | + /** @var SwapDirection */ |
|
16 | + private $swapDirection; |
|
17 | + |
|
18 | + /** @var GregorianWeekday */ |
|
19 | + private $swapToDay; |
|
20 | + |
|
21 | + /** @var GregorianWeekday[] */ |
|
22 | + private $swapWhenDay; |
|
23 | + |
|
24 | + public function __construct(SwapDirection $direction, GregorianWeekday $swapToGregorianDay, GregorianWeekday ...$swapWhenGregorianDay) |
|
25 | + { |
|
26 | + $this->swapDirection = $direction; |
|
27 | + $this->swapToDay = $swapToGregorianDay; |
|
28 | + $this->swapWhenDay = $swapWhenGregorianDay; |
|
29 | + } |
|
30 | + |
|
31 | + public function getDirection(): SwapDirection |
|
32 | + { |
|
33 | + return $this->swapDirection; |
|
34 | + } |
|
35 | + |
|
36 | + public function getSwapToDay(): GregorianWeekday |
|
37 | + { |
|
38 | + return $this->swapToDay; |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * @return GregorianWeekday[] |
|
43 | + */ |
|
44 | + public function getSwapWhenDays(): array |
|
45 | + { |
|
46 | + return $this->swapWhenDay; |
|
47 | + } |
|
48 | 48 | } |
@@ -20,136 +20,136 @@ |
||
20 | 20 | |
21 | 21 | final class GregorianWeekday |
22 | 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 | - $weekday = strtolower($weekday); |
|
111 | - $callable = [self::class, $weekday]; |
|
112 | - if (! is_callable($callable)) { |
|
113 | - throw new RuntimeException(sprintf( |
|
114 | - 'Weekday "%s" is not known', |
|
115 | - $weekday |
|
116 | - )); |
|
117 | - } |
|
118 | - |
|
119 | - /** @var GregorianWeekday $gregorianWeekday */ |
|
120 | - $gregorianWeekday = $callable(); |
|
121 | - |
|
122 | - return $gregorianWeekday; |
|
123 | - } |
|
124 | - |
|
125 | - public static function fromIntlWeekday(int $weekday): self |
|
126 | - { |
|
127 | - $mapper = [ |
|
128 | - IntlCalendar::DOW_SUNDAY => 'sunday', |
|
129 | - IntlCalendar::DOW_MONDAY => 'monday', |
|
130 | - IntlCalendar::DOW_TUESDAY => 'tuesday', |
|
131 | - IntlCalendar::DOW_WEDNESDAY => 'wednesday', |
|
132 | - IntlCalendar::DOW_THURSDAY => 'thursday', |
|
133 | - IntlCalendar::DOW_FRIDAY => 'friday', |
|
134 | - IntlCalendar::DOW_SATURDAY => 'saturday', |
|
135 | - ]; |
|
136 | - if (!isset($mapper[$weekday])) { |
|
137 | - throw new UnexpectedValueException(sprintf( |
|
138 | - 'IntlCalendar weekday %s could not be resolved', |
|
139 | - $weekday |
|
140 | - )); |
|
141 | - } |
|
142 | - |
|
143 | - return self::fromString($mapper[$weekday]); |
|
144 | - } |
|
145 | - |
|
146 | - public function __toString(): string |
|
147 | - { |
|
148 | - return $this->getValue(); |
|
149 | - } |
|
150 | - |
|
151 | - public function getValue(): string |
|
152 | - { |
|
153 | - return $this->value; |
|
154 | - } |
|
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 | + $weekday = strtolower($weekday); |
|
111 | + $callable = [self::class, $weekday]; |
|
112 | + if (! is_callable($callable)) { |
|
113 | + throw new RuntimeException(sprintf( |
|
114 | + 'Weekday "%s" is not known', |
|
115 | + $weekday |
|
116 | + )); |
|
117 | + } |
|
118 | + |
|
119 | + /** @var GregorianWeekday $gregorianWeekday */ |
|
120 | + $gregorianWeekday = $callable(); |
|
121 | + |
|
122 | + return $gregorianWeekday; |
|
123 | + } |
|
124 | + |
|
125 | + public static function fromIntlWeekday(int $weekday): self |
|
126 | + { |
|
127 | + $mapper = [ |
|
128 | + IntlCalendar::DOW_SUNDAY => 'sunday', |
|
129 | + IntlCalendar::DOW_MONDAY => 'monday', |
|
130 | + IntlCalendar::DOW_TUESDAY => 'tuesday', |
|
131 | + IntlCalendar::DOW_WEDNESDAY => 'wednesday', |
|
132 | + IntlCalendar::DOW_THURSDAY => 'thursday', |
|
133 | + IntlCalendar::DOW_FRIDAY => 'friday', |
|
134 | + IntlCalendar::DOW_SATURDAY => 'saturday', |
|
135 | + ]; |
|
136 | + if (!isset($mapper[$weekday])) { |
|
137 | + throw new UnexpectedValueException(sprintf( |
|
138 | + 'IntlCalendar weekday %s could not be resolved', |
|
139 | + $weekday |
|
140 | + )); |
|
141 | + } |
|
142 | + |
|
143 | + return self::fromString($mapper[$weekday]); |
|
144 | + } |
|
145 | + |
|
146 | + public function __toString(): string |
|
147 | + { |
|
148 | + return $this->getValue(); |
|
149 | + } |
|
150 | + |
|
151 | + public function getValue(): string |
|
152 | + { |
|
153 | + return $this->value; |
|
154 | + } |
|
155 | 155 | } |
@@ -109,7 +109,7 @@ |
||
109 | 109 | { |
110 | 110 | $weekday = strtolower($weekday); |
111 | 111 | $callable = [self::class, $weekday]; |
112 | - if (! is_callable($callable)) { |
|
112 | + if (!is_callable($callable)) { |
|
113 | 113 | throw new RuntimeException(sprintf( |
114 | 114 | 'Weekday "%s" is not known', |
115 | 115 | $weekday |
@@ -36,22 +36,22 @@ |
||
36 | 36 | |
37 | 37 | class Holidaychecker |
38 | 38 | { |
39 | - /** @var HolidayIterator */ |
|
40 | - private $iterator; |
|
41 | - |
|
42 | - public function __construct(HolidayIterator $iterator) |
|
43 | - { |
|
44 | - $this->iterator = $iterator; |
|
45 | - } |
|
46 | - |
|
47 | - public function check(DateTimeInterface $date): Holiday |
|
48 | - { |
|
49 | - foreach ($this->iterator as $entry) { |
|
50 | - if ($entry->dateMatches($date)) { |
|
51 | - return new Holiday($entry->isHoliday(), $entry->getName()); |
|
52 | - } |
|
53 | - } |
|
54 | - |
|
55 | - return new Holiday(false); |
|
56 | - } |
|
39 | + /** @var HolidayIterator */ |
|
40 | + private $iterator; |
|
41 | + |
|
42 | + public function __construct(HolidayIterator $iterator) |
|
43 | + { |
|
44 | + $this->iterator = $iterator; |
|
45 | + } |
|
46 | + |
|
47 | + public function check(DateTimeInterface $date): Holiday |
|
48 | + { |
|
49 | + foreach ($this->iterator as $entry) { |
|
50 | + if ($entry->dateMatches($date)) { |
|
51 | + return new Holiday($entry->isHoliday(), $entry->getName()); |
|
52 | + } |
|
53 | + } |
|
54 | + |
|
55 | + return new Holiday(false); |
|
56 | + } |
|
57 | 57 | } |