@@ -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 | } |
@@ -34,28 +34,28 @@ |
||
34 | 34 | |
35 | 35 | class Holiday |
36 | 36 | { |
37 | - private $holiday; |
|
38 | - |
|
39 | - private $name; |
|
40 | - |
|
41 | - public function __construct(bool $holiday, string $name = '') |
|
42 | - { |
|
43 | - $this->holiday = $holiday; |
|
44 | - $this->name = $name; |
|
45 | - } |
|
46 | - |
|
47 | - public function isHoliday(): bool |
|
48 | - { |
|
49 | - return $this->holiday; |
|
50 | - } |
|
51 | - |
|
52 | - public function isNamed(): bool |
|
53 | - { |
|
54 | - return $this->name !== ''; |
|
55 | - } |
|
56 | - |
|
57 | - public function getName(): string |
|
58 | - { |
|
59 | - return $this->name; |
|
60 | - } |
|
37 | + private $holiday; |
|
38 | + |
|
39 | + private $name; |
|
40 | + |
|
41 | + public function __construct(bool $holiday, string $name = '') |
|
42 | + { |
|
43 | + $this->holiday = $holiday; |
|
44 | + $this->name = $name; |
|
45 | + } |
|
46 | + |
|
47 | + public function isHoliday(): bool |
|
48 | + { |
|
49 | + return $this->holiday; |
|
50 | + } |
|
51 | + |
|
52 | + public function isNamed(): bool |
|
53 | + { |
|
54 | + return $this->name !== ''; |
|
55 | + } |
|
56 | + |
|
57 | + public function getName(): string |
|
58 | + { |
|
59 | + return $this->name; |
|
60 | + } |
|
61 | 61 | } |
@@ -36,9 +36,9 @@ |
||
36 | 36 | |
37 | 37 | interface HolidayIteratorItemInterface |
38 | 38 | { |
39 | - public function dateMatches(DateTimeInterface $date): bool; |
|
39 | + public function dateMatches(DateTimeInterface $date): bool; |
|
40 | 40 | |
41 | - public function getName(): string; |
|
41 | + public function getName(): string; |
|
42 | 42 | |
43 | - public function isHoliday(): bool; |
|
43 | + public function isHoliday(): bool; |
|
44 | 44 | } |
@@ -52,109 +52,109 @@ |
||
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 | - foreach ($dom->documentElement->childNodes as $child) { |
|
121 | - if (!$child instanceof DOMElement) { |
|
122 | - continue; |
|
123 | - } |
|
124 | - if ($child->nodeName === 'resources') { |
|
125 | - continue; |
|
126 | - } |
|
127 | - |
|
128 | - try { |
|
129 | - $element = $this->getElement($child); |
|
130 | - $element = $this->decorateElement($element, $child); |
|
131 | - $iterator->append($element); |
|
132 | - } catch (Throwable $e) { |
|
133 | - // Do nothing on purpose |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - return $iterator; |
|
138 | - } |
|
139 | - |
|
140 | - private function getElement(DOMElement $child): HolidayIteratorItemInterface |
|
141 | - { |
|
142 | - foreach ($this->factories as $factory) { |
|
143 | - $element = $factory->itemFromDomElement($child); |
|
144 | - if ($element instanceof HolidayIteratorItemInterface) { |
|
145 | - return $element; |
|
146 | - } |
|
147 | - } |
|
148 | - |
|
149 | - throw new RuntimeException('Unknown element encountered'); |
|
150 | - } |
|
151 | - |
|
152 | - private function decorateElement(HolidayIteratorItemInterface $element, DOMElement $child): HolidayIteratorItemInterface |
|
153 | - { |
|
154 | - foreach ($this->decorators as $decorator) { |
|
155 | - $element = $decorator->decorate($element, $child); |
|
156 | - } |
|
157 | - |
|
158 | - return $element; |
|
159 | - } |
|
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 | + foreach ($dom->documentElement->childNodes as $child) { |
|
121 | + if (!$child instanceof DOMElement) { |
|
122 | + continue; |
|
123 | + } |
|
124 | + if ($child->nodeName === 'resources') { |
|
125 | + continue; |
|
126 | + } |
|
127 | + |
|
128 | + try { |
|
129 | + $element = $this->getElement($child); |
|
130 | + $element = $this->decorateElement($element, $child); |
|
131 | + $iterator->append($element); |
|
132 | + } catch (Throwable $e) { |
|
133 | + // Do nothing on purpose |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + return $iterator; |
|
138 | + } |
|
139 | + |
|
140 | + private function getElement(DOMElement $child): HolidayIteratorItemInterface |
|
141 | + { |
|
142 | + foreach ($this->factories as $factory) { |
|
143 | + $element = $factory->itemFromDomElement($child); |
|
144 | + if ($element instanceof HolidayIteratorItemInterface) { |
|
145 | + return $element; |
|
146 | + } |
|
147 | + } |
|
148 | + |
|
149 | + throw new RuntimeException('Unknown element encountered'); |
|
150 | + } |
|
151 | + |
|
152 | + private function decorateElement(HolidayIteratorItemInterface $element, DOMElement $child): HolidayIteratorItemInterface |
|
153 | + { |
|
154 | + foreach ($this->decorators as $decorator) { |
|
155 | + $element = $decorator->decorate($element, $child); |
|
156 | + } |
|
157 | + |
|
158 | + return $element; |
|
159 | + } |
|
160 | 160 | } |
@@ -36,38 +36,38 @@ |
||
36 | 36 | |
37 | 37 | class Calendar |
38 | 38 | { |
39 | - public const BUDDHIST = 'buddhist'; |
|
40 | - public const CHINESE = 'chinese'; |
|
41 | - public const COPTIC = 'coptic'; |
|
42 | - public const ETHIOPIAN = 'ethiopian'; |
|
43 | - public const GREGORIAN = 'gregorian'; |
|
44 | - public const HEBREW = 'hebrew'; |
|
45 | - public const INDIAN = 'indian'; |
|
46 | - public const ISLAMIC = 'islamic'; |
|
47 | - public const ISLAMIC_CIVIL = 'islamic-civil'; |
|
48 | - public const ISLAMIC_UMALQURA = 'islamic-umalqura'; |
|
49 | - public const ISLAMIC_RGSA = 'islamic-rgsa'; |
|
50 | - public const ISLAMIC_TBLA = 'islamic-tbla'; |
|
51 | - public const JAPANESE = 'japanese'; |
|
52 | - public const PERSIAN = 'persian'; |
|
39 | + public const BUDDHIST = 'buddhist'; |
|
40 | + public const CHINESE = 'chinese'; |
|
41 | + public const COPTIC = 'coptic'; |
|
42 | + public const ETHIOPIAN = 'ethiopian'; |
|
43 | + public const GREGORIAN = 'gregorian'; |
|
44 | + public const HEBREW = 'hebrew'; |
|
45 | + public const INDIAN = 'indian'; |
|
46 | + public const ISLAMIC = 'islamic'; |
|
47 | + public const ISLAMIC_CIVIL = 'islamic-civil'; |
|
48 | + public const ISLAMIC_UMALQURA = 'islamic-umalqura'; |
|
49 | + public const ISLAMIC_RGSA = 'islamic-rgsa'; |
|
50 | + public const ISLAMIC_TBLA = 'islamic-tbla'; |
|
51 | + public const JAPANESE = 'japanese'; |
|
52 | + public const PERSIAN = 'persian'; |
|
53 | 53 | |
54 | - public static function isValidCalendarName(string $calendarname): bool |
|
55 | - { |
|
56 | - return in_array($calendarname, [ |
|
57 | - self::BUDDHIST, |
|
58 | - self::CHINESE, |
|
59 | - self::COPTIC, |
|
60 | - self::ETHIOPIAN, |
|
61 | - self::GREGORIAN, |
|
62 | - self::HEBREW, |
|
63 | - self::INDIAN, |
|
64 | - self::ISLAMIC, |
|
65 | - self::ISLAMIC_CIVIL, |
|
66 | - self::ISLAMIC_RGSA, |
|
67 | - self::ISLAMIC_TBLA, |
|
68 | - self::ISLAMIC_UMALQURA, |
|
69 | - self::JAPANESE, |
|
70 | - self::PERSIAN, |
|
71 | - ]); |
|
72 | - } |
|
54 | + public static function isValidCalendarName(string $calendarname): bool |
|
55 | + { |
|
56 | + return in_array($calendarname, [ |
|
57 | + self::BUDDHIST, |
|
58 | + self::CHINESE, |
|
59 | + self::COPTIC, |
|
60 | + self::ETHIOPIAN, |
|
61 | + self::GREGORIAN, |
|
62 | + self::HEBREW, |
|
63 | + self::INDIAN, |
|
64 | + self::ISLAMIC, |
|
65 | + self::ISLAMIC_CIVIL, |
|
66 | + self::ISLAMIC_RGSA, |
|
67 | + self::ISLAMIC_TBLA, |
|
68 | + self::ISLAMIC_UMALQURA, |
|
69 | + self::JAPANESE, |
|
70 | + self::PERSIAN, |
|
71 | + ]); |
|
72 | + } |
|
73 | 73 | } |
@@ -14,47 +14,47 @@ |
||
14 | 14 | |
15 | 15 | final class ObservanceDecorator implements HolidayIteratorItemInterface |
16 | 16 | { |
17 | - /** @var int|null */ |
|
18 | - private $firstObservance; |
|
19 | - |
|
20 | - /** @var int|null */ |
|
21 | - private $lastObservance; |
|
22 | - |
|
23 | - /** @var HolidayIteratorItemInterface */ |
|
24 | - private $wrapped; |
|
25 | - |
|
26 | - public function __construct(HolidayIteratorItemInterface $wrapped, ?int $firstObservance, ?int $lastObservance) |
|
27 | - { |
|
28 | - $this->wrapped = $wrapped; |
|
29 | - $this->firstObservance = $firstObservance; |
|
30 | - $this->lastObservance = $lastObservance; |
|
31 | - } |
|
32 | - |
|
33 | - public function dateMatches(DateTimeInterface $date): bool |
|
34 | - { |
|
35 | - if (!$this->isWithinObservance((int) $date->format('Y'))) { |
|
36 | - return false; |
|
37 | - } |
|
38 | - |
|
39 | - return $this->wrapped->dateMatches($date); |
|
40 | - } |
|
41 | - |
|
42 | - private function isWithinObservance(int $gregorianYear): bool |
|
43 | - { |
|
44 | - if (null !== $this->firstObservance && $this->firstObservance > $gregorianYear) { |
|
45 | - return false; |
|
46 | - } |
|
47 | - |
|
48 | - return null === $this->lastObservance || $this->lastObservance >= $gregorianYear; |
|
49 | - } |
|
50 | - |
|
51 | - public function getName(): string |
|
52 | - { |
|
53 | - return $this->wrapped->getName(); |
|
54 | - } |
|
55 | - |
|
56 | - public function isHoliday(): bool |
|
57 | - { |
|
58 | - return $this->wrapped->isHoliday(); |
|
59 | - } |
|
17 | + /** @var int|null */ |
|
18 | + private $firstObservance; |
|
19 | + |
|
20 | + /** @var int|null */ |
|
21 | + private $lastObservance; |
|
22 | + |
|
23 | + /** @var HolidayIteratorItemInterface */ |
|
24 | + private $wrapped; |
|
25 | + |
|
26 | + public function __construct(HolidayIteratorItemInterface $wrapped, ?int $firstObservance, ?int $lastObservance) |
|
27 | + { |
|
28 | + $this->wrapped = $wrapped; |
|
29 | + $this->firstObservance = $firstObservance; |
|
30 | + $this->lastObservance = $lastObservance; |
|
31 | + } |
|
32 | + |
|
33 | + public function dateMatches(DateTimeInterface $date): bool |
|
34 | + { |
|
35 | + if (!$this->isWithinObservance((int) $date->format('Y'))) { |
|
36 | + return false; |
|
37 | + } |
|
38 | + |
|
39 | + return $this->wrapped->dateMatches($date); |
|
40 | + } |
|
41 | + |
|
42 | + private function isWithinObservance(int $gregorianYear): bool |
|
43 | + { |
|
44 | + if (null !== $this->firstObservance && $this->firstObservance > $gregorianYear) { |
|
45 | + return false; |
|
46 | + } |
|
47 | + |
|
48 | + return null === $this->lastObservance || $this->lastObservance >= $gregorianYear; |
|
49 | + } |
|
50 | + |
|
51 | + public function getName(): string |
|
52 | + { |
|
53 | + return $this->wrapped->getName(); |
|
54 | + } |
|
55 | + |
|
56 | + public function isHoliday(): bool |
|
57 | + { |
|
58 | + return $this->wrapped->isHoliday(); |
|
59 | + } |
|
60 | 60 | } |
@@ -36,21 +36,21 @@ |
||
36 | 36 | |
37 | 37 | class Holidaychecker |
38 | 38 | { |
39 | - private $iterator; |
|
40 | - |
|
41 | - public function __construct(HolidayIterator $iterator) |
|
42 | - { |
|
43 | - $this->iterator = $iterator; |
|
44 | - } |
|
45 | - |
|
46 | - public function check(DateTimeInterface $date): Holiday |
|
47 | - { |
|
48 | - foreach ($this->iterator as $entry) { |
|
49 | - if ($entry->dateMatches($date)) { |
|
50 | - return new Holiday($entry->isHoliday(), $entry->getName()); |
|
51 | - } |
|
52 | - } |
|
53 | - |
|
54 | - return new Holiday(false); |
|
55 | - } |
|
39 | + private $iterator; |
|
40 | + |
|
41 | + public function __construct(HolidayIterator $iterator) |
|
42 | + { |
|
43 | + $this->iterator = $iterator; |
|
44 | + } |
|
45 | + |
|
46 | + public function check(DateTimeInterface $date): Holiday |
|
47 | + { |
|
48 | + foreach ($this->iterator as $entry) { |
|
49 | + if ($entry->dateMatches($date)) { |
|
50 | + return new Holiday($entry->isHoliday(), $entry->getName()); |
|
51 | + } |
|
52 | + } |
|
53 | + |
|
54 | + return new Holiday(false); |
|
55 | + } |
|
56 | 56 | } |