@@ -17,28 +17,28 @@ |
||
17 | 17 | |
18 | 18 | class DateFactory implements ItemFromDomElementCreator |
19 | 19 | { |
20 | - public function itemFromDomElement(DOMElement $element): ?HolidayIteratorItemInterface |
|
21 | - { |
|
22 | - if ($element->nodeName !== 'date') { |
|
23 | - return null; |
|
24 | - } |
|
25 | - |
|
26 | - $day = CalendarDayFactory::createCalendarDay( |
|
27 | - (int) $element->getAttribute('day'), |
|
28 | - (int) $element->getAttribute('month'), |
|
29 | - ($element->hasAttribute('calendar') ? $element->getAttribute('calendar') : 'gregorian') |
|
30 | - ); |
|
31 | - |
|
32 | - if ($element->hasAttribute('year')) { |
|
33 | - $day->setYear((int) $element->getAttribute('year')); |
|
34 | - } |
|
35 | - |
|
36 | - $date = new Date( |
|
37 | - $element->textContent, |
|
38 | - $element->getAttribute('free') === "true", |
|
39 | - $day, |
|
40 | - ); |
|
41 | - |
|
42 | - return $date; |
|
43 | - } |
|
20 | + public function itemFromDomElement(DOMElement $element): ?HolidayIteratorItemInterface |
|
21 | + { |
|
22 | + if ($element->nodeName !== 'date') { |
|
23 | + return null; |
|
24 | + } |
|
25 | + |
|
26 | + $day = CalendarDayFactory::createCalendarDay( |
|
27 | + (int) $element->getAttribute('day'), |
|
28 | + (int) $element->getAttribute('month'), |
|
29 | + ($element->hasAttribute('calendar') ? $element->getAttribute('calendar') : 'gregorian') |
|
30 | + ); |
|
31 | + |
|
32 | + if ($element->hasAttribute('year')) { |
|
33 | + $day->setYear((int) $element->getAttribute('year')); |
|
34 | + } |
|
35 | + |
|
36 | + $date = new Date( |
|
37 | + $element->textContent, |
|
38 | + $element->getAttribute('free') === "true", |
|
39 | + $day, |
|
40 | + ); |
|
41 | + |
|
42 | + return $date; |
|
43 | + } |
|
44 | 44 | } |
@@ -40,41 +40,41 @@ |
||
40 | 40 | |
41 | 41 | class EasterOrthodox extends Easter |
42 | 42 | { |
43 | - protected function getEaster(int $year): DateTimeImmutable |
|
44 | - { |
|
45 | - $jewishYear = 3760 + $year; |
|
46 | - $endOfPessach = new DateTimeImmutable( |
|
47 | - '@' . jdtounix(jewishtojd(1, 20, $jewishYear)) |
|
48 | - ); |
|
49 | - $orthodoxEaster = $this->getOrthodoxEaster($year); |
|
50 | - if ($endOfPessach > $orthodoxEaster) { |
|
51 | - $weekday = (int) $endOfPessach->format('w'); |
|
52 | - return $endOfPessach->add(new DateInterval('P' . (7 - $weekday) . 'D')); |
|
53 | - } |
|
43 | + protected function getEaster(int $year): DateTimeImmutable |
|
44 | + { |
|
45 | + $jewishYear = 3760 + $year; |
|
46 | + $endOfPessach = new DateTimeImmutable( |
|
47 | + '@' . jdtounix(jewishtojd(1, 20, $jewishYear)) |
|
48 | + ); |
|
49 | + $orthodoxEaster = $this->getOrthodoxEaster($year); |
|
50 | + if ($endOfPessach > $orthodoxEaster) { |
|
51 | + $weekday = (int) $endOfPessach->format('w'); |
|
52 | + return $endOfPessach->add(new DateInterval('P' . (7 - $weekday) . 'D')); |
|
53 | + } |
|
54 | 54 | |
55 | - return $orthodoxEaster; |
|
56 | - } |
|
55 | + return $orthodoxEaster; |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * @param int $year |
|
60 | - * |
|
61 | - * @return DateTimeImmutable |
|
62 | - * @see http://www.smart.net/~mmontes/ortheast.html |
|
63 | - */ |
|
64 | - private function getOrthodoxEaster(int $year): DateTimeImmutable |
|
65 | - { |
|
66 | - $r1 = $year % 19; |
|
67 | - $r2 = $year % 4; |
|
68 | - $r3 = $year % 7; |
|
69 | - $rA = 19 * $r1 + 16; |
|
70 | - $r4 = $rA % 30; |
|
71 | - $rB = 2 * $r2 + 4 * $r3 + 6 * $r4; |
|
72 | - $r5 = $rB % 7; |
|
73 | - $rC = $r4 + $r5; |
|
58 | + /** |
|
59 | + * @param int $year |
|
60 | + * |
|
61 | + * @return DateTimeImmutable |
|
62 | + * @see http://www.smart.net/~mmontes/ortheast.html |
|
63 | + */ |
|
64 | + private function getOrthodoxEaster(int $year): DateTimeImmutable |
|
65 | + { |
|
66 | + $r1 = $year % 19; |
|
67 | + $r2 = $year % 4; |
|
68 | + $r3 = $year % 7; |
|
69 | + $rA = 19 * $r1 + 16; |
|
70 | + $r4 = $rA % 30; |
|
71 | + $rB = 2 * $r2 + 4 * $r3 + 6 * $r4; |
|
72 | + $r5 = $rB % 7; |
|
73 | + $rC = $r4 + $r5; |
|
74 | 74 | |
75 | - // Don't touch this. It just seems to work… |
|
76 | - // And doing the "same" in DateTime (adding a period of $rC days doesn't |
|
77 | - // yield the same result… |
|
78 | - return new DateTimeImmutable('@' . jdtounix(juliantojd(3, 21, $year) + $rC)); |
|
79 | - } |
|
75 | + // Don't touch this. It just seems to work… |
|
76 | + // And doing the "same" in DateTime (adding a period of $rC days doesn't |
|
77 | + // yield the same result… |
|
78 | + return new DateTimeImmutable('@' . jdtounix(juliantojd(3, 21, $year) + $rC)); |
|
79 | + } |
|
80 | 80 | } |
@@ -38,34 +38,34 @@ |
||
38 | 38 | |
39 | 39 | class Date implements HolidayIteratorItemInterface |
40 | 40 | { |
41 | - /** @var CalendarDay */ |
|
42 | - private $calendarDay; |
|
41 | + /** @var CalendarDay */ |
|
42 | + private $calendarDay; |
|
43 | 43 | |
44 | - /** @var bool */ |
|
45 | - private $holiday; |
|
44 | + /** @var bool */ |
|
45 | + private $holiday; |
|
46 | 46 | |
47 | - /** @var string */ |
|
48 | - private $name; |
|
47 | + /** @var string */ |
|
48 | + private $name; |
|
49 | 49 | |
50 | - public function __construct(string $name, bool $holiday, CalendarDay $day) |
|
51 | - { |
|
52 | - $this->calendarDay = $day; |
|
53 | - $this->holiday = $holiday; |
|
54 | - $this->name = $name; |
|
55 | - } |
|
50 | + public function __construct(string $name, bool $holiday, CalendarDay $day) |
|
51 | + { |
|
52 | + $this->calendarDay = $day; |
|
53 | + $this->holiday = $holiday; |
|
54 | + $this->name = $name; |
|
55 | + } |
|
56 | 56 | |
57 | - public function dateMatches(DateTimeInterface $date): bool |
|
58 | - { |
|
59 | - return $this->calendarDay->isSameDay($date); |
|
60 | - } |
|
57 | + public function dateMatches(DateTimeInterface $date): bool |
|
58 | + { |
|
59 | + return $this->calendarDay->isSameDay($date); |
|
60 | + } |
|
61 | 61 | |
62 | - public function getName(): string |
|
63 | - { |
|
64 | - return $this->name; |
|
65 | - } |
|
62 | + public function getName(): string |
|
63 | + { |
|
64 | + return $this->name; |
|
65 | + } |
|
66 | 66 | |
67 | - public function isHoliday(): bool |
|
68 | - { |
|
69 | - return $this->holiday; |
|
70 | - } |
|
67 | + public function isHoliday(): bool |
|
68 | + { |
|
69 | + return $this->holiday; |
|
70 | + } |
|
71 | 71 | } |
@@ -39,53 +39,53 @@ |
||
39 | 39 | |
40 | 40 | class Relative implements HolidayIteratorItemInterface |
41 | 41 | { |
42 | - private $day; |
|
42 | + private $day; |
|
43 | 43 | |
44 | - private $month; |
|
44 | + private $month; |
|
45 | 45 | |
46 | - private $relation; |
|
46 | + private $relation; |
|
47 | 47 | |
48 | - private $holiday; |
|
48 | + private $holiday; |
|
49 | 49 | |
50 | - private $name; |
|
50 | + private $name; |
|
51 | 51 | |
52 | - public function __construct(string $name, bool $holiday, int $day, int $month, string $relation) |
|
53 | - { |
|
54 | - $this->day = $day; |
|
55 | - $this->month = $month; |
|
56 | - $this->relation = $relation; |
|
57 | - $this->holiday = $holiday; |
|
58 | - $this->name = $name; |
|
59 | - } |
|
52 | + public function __construct(string $name, bool $holiday, int $day, int $month, string $relation) |
|
53 | + { |
|
54 | + $this->day = $day; |
|
55 | + $this->month = $month; |
|
56 | + $this->relation = $relation; |
|
57 | + $this->holiday = $holiday; |
|
58 | + $this->name = $name; |
|
59 | + } |
|
60 | 60 | |
61 | - public function dateMatches(DateTimeInterface $date): bool |
|
62 | - { |
|
63 | - $year = (int) $date->format('Y'); |
|
61 | + public function dateMatches(DateTimeInterface $date): bool |
|
62 | + { |
|
63 | + $year = (int) $date->format('Y'); |
|
64 | 64 | |
65 | - $day = new DateTimeImmutable(sprintf( |
|
66 | - '%s-%s-%s', |
|
67 | - $year, |
|
68 | - $this->month, |
|
69 | - $this->day |
|
70 | - )); |
|
65 | + $day = new DateTimeImmutable(sprintf( |
|
66 | + '%s-%s-%s', |
|
67 | + $year, |
|
68 | + $this->month, |
|
69 | + $this->day |
|
70 | + )); |
|
71 | 71 | |
72 | - /** @var DateTimeImmutable|false $day */ |
|
73 | - $day = $day->modify($this->relation); |
|
72 | + /** @var DateTimeImmutable|false $day */ |
|
73 | + $day = $day->modify($this->relation); |
|
74 | 74 | |
75 | - if ($day === false) { |
|
76 | - return false; |
|
77 | - } |
|
75 | + if ($day === false) { |
|
76 | + return false; |
|
77 | + } |
|
78 | 78 | |
79 | - return $date->format('Y-m-d') === $day->format('Y-m-d'); |
|
80 | - } |
|
79 | + return $date->format('Y-m-d') === $day->format('Y-m-d'); |
|
80 | + } |
|
81 | 81 | |
82 | - public function getName(): string |
|
83 | - { |
|
84 | - return $this->name; |
|
85 | - } |
|
82 | + public function getName(): string |
|
83 | + { |
|
84 | + return $this->name; |
|
85 | + } |
|
86 | 86 | |
87 | - public function isHoliday(): bool |
|
88 | - { |
|
89 | - return $this->holiday; |
|
90 | - } |
|
87 | + public function isHoliday(): bool |
|
88 | + { |
|
89 | + return $this->holiday; |
|
90 | + } |
|
91 | 91 | } |
@@ -41,76 +41,76 @@ |
||
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 array */ |
|
57 | - private $replaced; |
|
58 | - |
|
59 | - public function __construct(string $name, bool $holiday, CalendarDay $day, string $followup, array $replaced = []) |
|
60 | - { |
|
61 | - $this->day = $day; |
|
62 | - $this->followup = $followup; |
|
63 | - $this->holiday = $holiday; |
|
64 | - $this->name = $name; |
|
65 | - $this->replaced = $this->replacedDays($replaced); |
|
66 | - } |
|
67 | - |
|
68 | - private static function replacedDays(array $replaced): array |
|
69 | - { |
|
70 | - $daymap = [ |
|
71 | - 'sunday' => IntlCalendar::DOW_SUNDAY, |
|
72 | - 'monday' => IntlCalendar::DOW_MONDAY, |
|
73 | - 'tuesday' => IntlCalendar::DOW_TUESDAY, |
|
74 | - 'wednesday' => IntlCalendar::DOW_WEDNESDAY, |
|
75 | - 'thursday' => IntlCalendar::DOW_THURSDAY, |
|
76 | - 'friday' => IntlCalendar::DOW_FRIDAY, |
|
77 | - 'saturday' => IntlCalendar::DOW_SATURDAY, |
|
78 | - ]; |
|
79 | - |
|
80 | - if ([] === $replaced) { |
|
81 | - return [ |
|
82 | - IntlCalendar::DOW_SATURDAY, |
|
83 | - IntlCalendar::DOW_SUNDAY, |
|
84 | - ]; |
|
85 | - } |
|
86 | - |
|
87 | - return array_map(function (string $day) use ($daymap) { |
|
88 | - if (!isset($daymap[$day])) { |
|
89 | - return null; |
|
90 | - } |
|
91 | - return $daymap[$day]; |
|
92 | - }, $replaced); |
|
93 | - } |
|
94 | - |
|
95 | - public function dateMatches(DateTimeInterface $date): bool |
|
96 | - { |
|
97 | - $gregorianYear = (int) $date->format('Y'); |
|
98 | - $weekday = $this->day->getWeekdayForGregorianYear($gregorianYear); |
|
99 | - |
|
100 | - if (in_array($weekday, $this->replaced)) { |
|
101 | - return $this->day->isFollowUpDay($date, $this->followup); |
|
102 | - } |
|
103 | - |
|
104 | - return $this->day->isSameDay($date); |
|
105 | - } |
|
106 | - |
|
107 | - public function getName(): string |
|
108 | - { |
|
109 | - return $this->name; |
|
110 | - } |
|
111 | - |
|
112 | - public function isHoliday(): bool |
|
113 | - { |
|
114 | - return $this->holiday; |
|
115 | - } |
|
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 array */ |
|
57 | + private $replaced; |
|
58 | + |
|
59 | + public function __construct(string $name, bool $holiday, CalendarDay $day, string $followup, array $replaced = []) |
|
60 | + { |
|
61 | + $this->day = $day; |
|
62 | + $this->followup = $followup; |
|
63 | + $this->holiday = $holiday; |
|
64 | + $this->name = $name; |
|
65 | + $this->replaced = $this->replacedDays($replaced); |
|
66 | + } |
|
67 | + |
|
68 | + private static function replacedDays(array $replaced): array |
|
69 | + { |
|
70 | + $daymap = [ |
|
71 | + 'sunday' => IntlCalendar::DOW_SUNDAY, |
|
72 | + 'monday' => IntlCalendar::DOW_MONDAY, |
|
73 | + 'tuesday' => IntlCalendar::DOW_TUESDAY, |
|
74 | + 'wednesday' => IntlCalendar::DOW_WEDNESDAY, |
|
75 | + 'thursday' => IntlCalendar::DOW_THURSDAY, |
|
76 | + 'friday' => IntlCalendar::DOW_FRIDAY, |
|
77 | + 'saturday' => IntlCalendar::DOW_SATURDAY, |
|
78 | + ]; |
|
79 | + |
|
80 | + if ([] === $replaced) { |
|
81 | + return [ |
|
82 | + IntlCalendar::DOW_SATURDAY, |
|
83 | + IntlCalendar::DOW_SUNDAY, |
|
84 | + ]; |
|
85 | + } |
|
86 | + |
|
87 | + return array_map(function (string $day) use ($daymap) { |
|
88 | + if (!isset($daymap[$day])) { |
|
89 | + return null; |
|
90 | + } |
|
91 | + return $daymap[$day]; |
|
92 | + }, $replaced); |
|
93 | + } |
|
94 | + |
|
95 | + public function dateMatches(DateTimeInterface $date): bool |
|
96 | + { |
|
97 | + $gregorianYear = (int) $date->format('Y'); |
|
98 | + $weekday = $this->day->getWeekdayForGregorianYear($gregorianYear); |
|
99 | + |
|
100 | + if (in_array($weekday, $this->replaced)) { |
|
101 | + return $this->day->isFollowUpDay($date, $this->followup); |
|
102 | + } |
|
103 | + |
|
104 | + return $this->day->isSameDay($date); |
|
105 | + } |
|
106 | + |
|
107 | + public function getName(): string |
|
108 | + { |
|
109 | + return $this->name; |
|
110 | + } |
|
111 | + |
|
112 | + public function isHoliday(): bool |
|
113 | + { |
|
114 | + return $this->holiday; |
|
115 | + } |
|
116 | 116 | } |
@@ -84,7 +84,7 @@ |
||
84 | 84 | ]; |
85 | 85 | } |
86 | 86 | |
87 | - return array_map(function (string $day) use ($daymap) { |
|
87 | + return array_map(function(string $day) use ($daymap) { |
|
88 | 88 | if (!isset($daymap[$day])) { |
89 | 89 | return null; |
90 | 90 | } |
@@ -43,58 +43,58 @@ |
||
43 | 43 | |
44 | 44 | class Easter implements HolidayIteratorItemInterface |
45 | 45 | { |
46 | - private $offset; |
|
47 | - |
|
48 | - private $holiday; |
|
49 | - |
|
50 | - private $name; |
|
51 | - |
|
52 | - public function __construct(string $name, bool $holiday, int $offset) |
|
53 | - { |
|
54 | - $this->offset = $offset; |
|
55 | - $this->holiday = $holiday; |
|
56 | - $this->name = $name; |
|
57 | - } |
|
58 | - |
|
59 | - public function dateMatches(DateTimeInterface $date): bool |
|
60 | - { |
|
61 | - $year = (int) $date->format('Y'); |
|
62 | - |
|
63 | - $easter = $this->getEaster($year); |
|
64 | - $day = $this->getOffsetDay($easter, $this->offset); |
|
65 | - |
|
66 | - $comparator = new DateIntervalComparator(); |
|
67 | - return 0 > $comparator->compare($day->diff($date), new DateInterval('P1D')); |
|
68 | - } |
|
69 | - |
|
70 | - protected function getEaster(int $year): DateTimeImmutable |
|
71 | - { |
|
72 | - $base = new DateTimeImmutable($year . "-03-21", new DateTimeZone('UTC')); |
|
73 | - $days = easter_days($year); |
|
74 | - |
|
75 | - return $base->add(new DateInterval("P{$days}D")); |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * @param DateTime|DateTimeImmutable $date |
|
80 | - * @return DateTime|DateTimeImmutable |
|
81 | - */ |
|
82 | - private function getOffsetDay($date, int $offset) |
|
83 | - { |
|
84 | - if ($offset < 0) { |
|
85 | - return $date->sub(new DateInterval('P' . $offset * -1 . 'D')); |
|
86 | - } |
|
87 | - |
|
88 | - return $date->add(new DateInterval('P' . $offset . 'D')); |
|
89 | - } |
|
90 | - |
|
91 | - public function getName(): string |
|
92 | - { |
|
93 | - return $this->name; |
|
94 | - } |
|
95 | - |
|
96 | - public function isHoliday(): bool |
|
97 | - { |
|
98 | - return $this->holiday; |
|
99 | - } |
|
46 | + private $offset; |
|
47 | + |
|
48 | + private $holiday; |
|
49 | + |
|
50 | + private $name; |
|
51 | + |
|
52 | + public function __construct(string $name, bool $holiday, int $offset) |
|
53 | + { |
|
54 | + $this->offset = $offset; |
|
55 | + $this->holiday = $holiday; |
|
56 | + $this->name = $name; |
|
57 | + } |
|
58 | + |
|
59 | + public function dateMatches(DateTimeInterface $date): bool |
|
60 | + { |
|
61 | + $year = (int) $date->format('Y'); |
|
62 | + |
|
63 | + $easter = $this->getEaster($year); |
|
64 | + $day = $this->getOffsetDay($easter, $this->offset); |
|
65 | + |
|
66 | + $comparator = new DateIntervalComparator(); |
|
67 | + return 0 > $comparator->compare($day->diff($date), new DateInterval('P1D')); |
|
68 | + } |
|
69 | + |
|
70 | + protected function getEaster(int $year): DateTimeImmutable |
|
71 | + { |
|
72 | + $base = new DateTimeImmutable($year . "-03-21", new DateTimeZone('UTC')); |
|
73 | + $days = easter_days($year); |
|
74 | + |
|
75 | + return $base->add(new DateInterval("P{$days}D")); |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * @param DateTime|DateTimeImmutable $date |
|
80 | + * @return DateTime|DateTimeImmutable |
|
81 | + */ |
|
82 | + private function getOffsetDay($date, int $offset) |
|
83 | + { |
|
84 | + if ($offset < 0) { |
|
85 | + return $date->sub(new DateInterval('P' . $offset * -1 . 'D')); |
|
86 | + } |
|
87 | + |
|
88 | + return $date->add(new DateInterval('P' . $offset . 'D')); |
|
89 | + } |
|
90 | + |
|
91 | + public function getName(): string |
|
92 | + { |
|
93 | + return $this->name; |
|
94 | + } |
|
95 | + |
|
96 | + public function isHoliday(): bool |
|
97 | + { |
|
98 | + return $this->holiday; |
|
99 | + } |
|
100 | 100 | } |
@@ -21,68 +21,68 @@ |
||
21 | 21 | |
22 | 22 | class SwapDecorator implements HolidayIteratorItemInterface |
23 | 23 | { |
24 | - /** @var HolidayIteratorItemInterface */ |
|
25 | - private $rule; |
|
26 | - |
|
27 | - /** @var CalendarDay */ |
|
28 | - private $day; |
|
29 | - |
|
30 | - /** @var SwapRule[] */ |
|
31 | - private $swapRules; |
|
32 | - |
|
33 | - public function __construct(HolidayIteratorItemInterface $rule, CalendarDay $day, SwapRule ...$swapRules) |
|
34 | - { |
|
35 | - $this->rule = $rule; |
|
36 | - $this->day = $day; |
|
37 | - $this->swapRules = $swapRules; |
|
38 | - } |
|
39 | - |
|
40 | - public function dateMatches(DateTimeInterface $date): bool |
|
41 | - { |
|
42 | - $year = (int) $date->format('Y'); |
|
43 | - $weekday = GregorianWeekday::fromIntlWeekday($this->day->getWeekdayForGregorianYear($year)); |
|
44 | - foreach ($this->swapRules as $rule) { |
|
45 | - if ($this->ruleMatches($rule, $weekday)) { |
|
46 | - return $this->isModifiedDate($date, $rule->getSwapToDay(), $rule->getDirection()); |
|
47 | - } |
|
48 | - } |
|
49 | - |
|
50 | - return $this->rule->dateMatches($date); |
|
51 | - } |
|
52 | - |
|
53 | - private function ruleMatches(SwapRule $rule, GregorianWeekday $weekday): bool |
|
54 | - { |
|
55 | - return in_array($weekday, $rule->getSwapWhenDays(), true); |
|
56 | - } |
|
57 | - |
|
58 | - private function isModifiedDate(DateTimeInterface $dateTime, GregorianWeekday $modifiedDay, SwapDirection $direction): bool |
|
59 | - { |
|
60 | - $cal = $this->day->getCalendar(); |
|
61 | - $cal = CalendarDay::setGregorianYearForDate((int) $dateTime->format('Y'), $cal); |
|
62 | - $day = $cal->toDateTime(); |
|
63 | - $day->modify($direction->getDateTimeDirection() . ' ' . $modifiedDay); |
|
64 | - $cal->setTime($day->getTimestamp() * 1000); |
|
65 | - $cal2 = $this->day->getCalendar(); |
|
66 | - $cal2->setTime($dateTime->getTimestamp() * 1000); |
|
67 | - |
|
68 | - if ($this->day->hasYearSet() && $cal->get(IntlCalendar::FIELD_YEAR) !== $cal2->get(IntlCalendar::FIELD_YEAR)) { |
|
69 | - return false; |
|
70 | - } |
|
71 | - |
|
72 | - if ($cal->get(IntlCalendar::FIELD_MONTH) !== $cal2->get(IntlCalendar::FIELD_MONTH)) { |
|
73 | - return false; |
|
74 | - } |
|
75 | - |
|
76 | - return $cal->get(IntlCalendar::FIELD_DAY_OF_MONTH) === $cal2->get(IntlCalendar::FIELD_DAY_OF_MONTH); |
|
77 | - } |
|
78 | - |
|
79 | - public function getName(): string |
|
80 | - { |
|
81 | - return $this->rule->getName(); |
|
82 | - } |
|
83 | - |
|
84 | - public function isHoliday(): bool |
|
85 | - { |
|
86 | - return $this->rule->isHoliday(); |
|
87 | - } |
|
24 | + /** @var HolidayIteratorItemInterface */ |
|
25 | + private $rule; |
|
26 | + |
|
27 | + /** @var CalendarDay */ |
|
28 | + private $day; |
|
29 | + |
|
30 | + /** @var SwapRule[] */ |
|
31 | + private $swapRules; |
|
32 | + |
|
33 | + public function __construct(HolidayIteratorItemInterface $rule, CalendarDay $day, SwapRule ...$swapRules) |
|
34 | + { |
|
35 | + $this->rule = $rule; |
|
36 | + $this->day = $day; |
|
37 | + $this->swapRules = $swapRules; |
|
38 | + } |
|
39 | + |
|
40 | + public function dateMatches(DateTimeInterface $date): bool |
|
41 | + { |
|
42 | + $year = (int) $date->format('Y'); |
|
43 | + $weekday = GregorianWeekday::fromIntlWeekday($this->day->getWeekdayForGregorianYear($year)); |
|
44 | + foreach ($this->swapRules as $rule) { |
|
45 | + if ($this->ruleMatches($rule, $weekday)) { |
|
46 | + return $this->isModifiedDate($date, $rule->getSwapToDay(), $rule->getDirection()); |
|
47 | + } |
|
48 | + } |
|
49 | + |
|
50 | + return $this->rule->dateMatches($date); |
|
51 | + } |
|
52 | + |
|
53 | + private function ruleMatches(SwapRule $rule, GregorianWeekday $weekday): bool |
|
54 | + { |
|
55 | + return in_array($weekday, $rule->getSwapWhenDays(), true); |
|
56 | + } |
|
57 | + |
|
58 | + private function isModifiedDate(DateTimeInterface $dateTime, GregorianWeekday $modifiedDay, SwapDirection $direction): bool |
|
59 | + { |
|
60 | + $cal = $this->day->getCalendar(); |
|
61 | + $cal = CalendarDay::setGregorianYearForDate((int) $dateTime->format('Y'), $cal); |
|
62 | + $day = $cal->toDateTime(); |
|
63 | + $day->modify($direction->getDateTimeDirection() . ' ' . $modifiedDay); |
|
64 | + $cal->setTime($day->getTimestamp() * 1000); |
|
65 | + $cal2 = $this->day->getCalendar(); |
|
66 | + $cal2->setTime($dateTime->getTimestamp() * 1000); |
|
67 | + |
|
68 | + if ($this->day->hasYearSet() && $cal->get(IntlCalendar::FIELD_YEAR) !== $cal2->get(IntlCalendar::FIELD_YEAR)) { |
|
69 | + return false; |
|
70 | + } |
|
71 | + |
|
72 | + if ($cal->get(IntlCalendar::FIELD_MONTH) !== $cal2->get(IntlCalendar::FIELD_MONTH)) { |
|
73 | + return false; |
|
74 | + } |
|
75 | + |
|
76 | + return $cal->get(IntlCalendar::FIELD_DAY_OF_MONTH) === $cal2->get(IntlCalendar::FIELD_DAY_OF_MONTH); |
|
77 | + } |
|
78 | + |
|
79 | + public function getName(): string |
|
80 | + { |
|
81 | + return $this->rule->getName(); |
|
82 | + } |
|
83 | + |
|
84 | + public function isHoliday(): bool |
|
85 | + { |
|
86 | + return $this->rule->isHoliday(); |
|
87 | + } |
|
88 | 88 | } |
@@ -40,17 +40,17 @@ |
||
40 | 40 | */ |
41 | 41 | class HolidayIterator extends ArrayObject |
42 | 42 | { |
43 | - /** |
|
44 | - * @param mixed $value |
|
45 | - * @return void |
|
46 | - */ |
|
47 | - #[ReturnTypeWillChange] |
|
48 | - public function append($value) |
|
49 | - { |
|
50 | - if (!$value instanceof HolidayIteratorItemInterface) { |
|
51 | - return; |
|
52 | - } |
|
43 | + /** |
|
44 | + * @param mixed $value |
|
45 | + * @return void |
|
46 | + */ |
|
47 | + #[ReturnTypeWillChange] |
|
48 | + public function append($value) |
|
49 | + { |
|
50 | + if (!$value instanceof HolidayIteratorItemInterface) { |
|
51 | + return; |
|
52 | + } |
|
53 | 53 | |
54 | - parent::append($value); |
|
55 | - } |
|
54 | + parent::append($value); |
|
55 | + } |
|
56 | 56 | } |
@@ -20,134 +20,134 @@ |
||
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 | - 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 | - } |
|
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 | 153 | } |