@@ -18,24 +18,24 @@ |
||
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 | - } |
|
21 | + public function itemFromDomElement(DOMElement $element): ?HolidayIteratorItemInterface |
|
22 | + { |
|
23 | + if ($element->nodeName !== 'dateFollowUp') { |
|
24 | + return null; |
|
25 | + } |
|
26 | 26 | |
27 | - $day = CalendarDayFactory::createCalendarDay( |
|
28 | - (int) $element->getAttribute('day'), |
|
29 | - (int) $element->getAttribute('month'), |
|
30 | - ($element->hasAttribute('calendar') ? $element->getAttribute('calendar') : 'gregorian') |
|
31 | - ); |
|
27 | + $day = CalendarDayFactory::createCalendarDay( |
|
28 | + (int) $element->getAttribute('day'), |
|
29 | + (int) $element->getAttribute('month'), |
|
30 | + ($element->hasAttribute('calendar') ? $element->getAttribute('calendar') : 'gregorian') |
|
31 | + ); |
|
32 | 32 | |
33 | - return new DateFollowUp( |
|
34 | - $element->textContent, |
|
35 | - $element->getAttribute('free') === "true", |
|
36 | - $day, |
|
37 | - $element->getAttribute('followup'), |
|
38 | - ($element->hasAttribute('replaced') ? explode(' ', $element->getAttribute('replaced')) : []) |
|
39 | - ); |
|
40 | - } |
|
33 | + return new DateFollowUp( |
|
34 | + $element->textContent, |
|
35 | + $element->getAttribute('free') === "true", |
|
36 | + $day, |
|
37 | + $element->getAttribute('followup'), |
|
38 | + ($element->hasAttribute('replaced') ? explode(' ', $element->getAttribute('replaced')) : []) |
|
39 | + ); |
|
40 | + } |
|
41 | 41 | } |
@@ -16,16 +16,16 @@ |
||
16 | 16 | |
17 | 17 | class EasterOrthodoxFactory implements ItemFromDomElementCreator |
18 | 18 | { |
19 | - public function itemFromDomElement(DOMElement $element): ?HolidayIteratorItemInterface |
|
20 | - { |
|
21 | - if ($element->nodeName !== 'easterorthodox') { |
|
22 | - return null; |
|
23 | - } |
|
19 | + public function itemFromDomElement(DOMElement $element): ?HolidayIteratorItemInterface |
|
20 | + { |
|
21 | + if ($element->nodeName !== 'easterorthodox') { |
|
22 | + return null; |
|
23 | + } |
|
24 | 24 | |
25 | - return new EasterOrthodox( |
|
26 | - $element->textContent, |
|
25 | + return new EasterOrthodox( |
|
26 | + $element->textContent, |
|
27 | 27 | $element->getAttribute('free') === "true", |
28 | - (int) $element->getAttribute('offset') |
|
29 | - ); |
|
30 | - } |
|
28 | + (int) $element->getAttribute('offset') |
|
29 | + ); |
|
30 | + } |
|
31 | 31 | } |
@@ -15,5 +15,5 @@ |
||
15 | 15 | |
16 | 16 | interface ItemFromDomElementCreator |
17 | 17 | { |
18 | - public function itemFromDomElement(DOMElement $element): ?HolidayIteratorItemInterface; |
|
18 | + public function itemFromDomElement(DOMElement $element): ?HolidayIteratorItemInterface; |
|
19 | 19 | } |
@@ -16,16 +16,16 @@ |
||
16 | 16 | |
17 | 17 | class ObservanceDecoratorFactory implements DecorateFromDomElement |
18 | 18 | { |
19 | - public function decorate(HolidayIteratorItemInterface $element, DOMElement $domElement): HolidayIteratorItemInterface |
|
20 | - { |
|
21 | - if (! $domElement->hasAttribute('firstobservance') && ! $domElement->hasAttribute('lastobservance')) { |
|
22 | - return $element; |
|
23 | - } |
|
19 | + public function decorate(HolidayIteratorItemInterface $element, DOMElement $domElement): HolidayIteratorItemInterface |
|
20 | + { |
|
21 | + if (! $domElement->hasAttribute('firstobservance') && ! $domElement->hasAttribute('lastobservance')) { |
|
22 | + return $element; |
|
23 | + } |
|
24 | 24 | |
25 | - return new ObservanceDecorator( |
|
26 | - $element, |
|
27 | - $domElement->hasAttribute('firstobservance') ? (int) $domElement->getAttribute('firstobservance') : null, |
|
28 | - $domElement->hasAttribute('lastobservance') ? (int) $domElement->getAttribute('lastobservance') : null, |
|
29 | - ); |
|
30 | - } |
|
25 | + return new ObservanceDecorator( |
|
26 | + $element, |
|
27 | + $domElement->hasAttribute('firstobservance') ? (int) $domElement->getAttribute('firstobservance') : null, |
|
28 | + $domElement->hasAttribute('lastobservance') ? (int) $domElement->getAttribute('lastobservance') : null, |
|
29 | + ); |
|
30 | + } |
|
31 | 31 | } |
@@ -18,7 +18,7 @@ |
||
18 | 18 | { |
19 | 19 | public function decorate(HolidayIteratorItemInterface $element, DOMElement $domElement): HolidayIteratorItemInterface |
20 | 20 | { |
21 | - if (! $domElement->hasAttribute('firstobservance') && ! $domElement->hasAttribute('lastobservance')) { |
|
21 | + if (!$domElement->hasAttribute('firstobservance') && !$domElement->hasAttribute('lastobservance')) { |
|
22 | 22 | return $element; |
23 | 23 | } |
24 | 24 |
@@ -16,18 +16,18 @@ |
||
16 | 16 | |
17 | 17 | class RelativeFactory implements ItemFromDomElementCreator |
18 | 18 | { |
19 | - public function itemFromDomElement(DOMElement $element): ?HolidayIteratorItemInterface |
|
20 | - { |
|
21 | - if ($element->nodeName !== 'relative') { |
|
22 | - return null; |
|
23 | - } |
|
19 | + public function itemFromDomElement(DOMElement $element): ?HolidayIteratorItemInterface |
|
20 | + { |
|
21 | + if ($element->nodeName !== 'relative') { |
|
22 | + return null; |
|
23 | + } |
|
24 | 24 | |
25 | - return new Relative( |
|
26 | - $element->textContent, |
|
27 | - $element->getAttribute('free') === "true", |
|
28 | - (int) $element->getAttribute('day'), |
|
29 | - (int) $element->getAttribute('month'), |
|
30 | - $element->getAttribute('relation') |
|
31 | - ); |
|
32 | - } |
|
25 | + return new Relative( |
|
26 | + $element->textContent, |
|
27 | + $element->getAttribute('free') === "true", |
|
28 | + (int) $element->getAttribute('day'), |
|
29 | + (int) $element->getAttribute('month'), |
|
30 | + $element->getAttribute('relation') |
|
31 | + ); |
|
32 | + } |
|
33 | 33 | } |
@@ -15,5 +15,5 @@ |
||
15 | 15 | |
16 | 16 | interface DecorateFromDomElement |
17 | 17 | { |
18 | - public function decorate(HolidayIteratorItemInterface $element, DOMElement $domElement): HolidayIteratorItemInterface; |
|
18 | + public function decorate(HolidayIteratorItemInterface $element, DOMElement $domElement): HolidayIteratorItemInterface; |
|
19 | 19 | } |
@@ -51,108 +51,108 @@ |
||
51 | 51 | |
52 | 52 | class HolidayIteratorFactory |
53 | 53 | { |
54 | - /** @var ItemFromDomElementCreator[] */ |
|
55 | - private $factories; |
|
56 | - |
|
57 | - /** @var DecorateFromDomElement[] */ |
|
58 | - private $decorators; |
|
59 | - |
|
60 | - public function __construct() |
|
61 | - { |
|
62 | - $this->factories = [ |
|
63 | - new EasterFactory(), |
|
64 | - new EasterOrthodoxFactory(), |
|
65 | - new DateFactory(), |
|
66 | - new DateFollowupFactory(), |
|
67 | - new RelativeFactory(), |
|
68 | - ]; |
|
69 | - |
|
70 | - $this->decorators = [ |
|
71 | - new ObservanceDecoratorFactory(), |
|
72 | - ]; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Create a HolidayIterator from an XML-File |
|
77 | - * |
|
78 | - * The provided XML-File has to validate against the holiday.xsd-file you |
|
79 | - * can find in this projects "share" folder. |
|
80 | - * |
|
81 | - * @param string $file |
|
82 | - * |
|
83 | - * @return HolidayIterator |
|
84 | - */ |
|
85 | - public function createIteratorFromXmlFile(string $file): HolidayIterator |
|
86 | - { |
|
87 | - $iterator = new HolidayIterator(); |
|
88 | - |
|
89 | - $dom = new DOMDocument('1.0', 'UTF-8'); |
|
90 | - $dom->load($file); |
|
91 | - $dom->xinclude(); |
|
92 | - |
|
93 | - if (!@$dom->schemaValidate(__DIR__ . '/../share/holidays.xsd')) { |
|
94 | - throw new Exception('XML-File does not validate agains schema'); |
|
95 | - } |
|
96 | - foreach ($dom->documentElement->childNodes as $child) { |
|
97 | - if (!$child instanceof DOMElement) { |
|
98 | - continue; |
|
99 | - } |
|
100 | - if ($child->nodeName === 'resources') { |
|
101 | - continue; |
|
102 | - } |
|
103 | - |
|
104 | - try { |
|
105 | - $element = $this->getElement($child); |
|
106 | - $element = $this->decorateElement($element, $child); |
|
107 | - $iterator->append($element); |
|
108 | - } catch (Throwable $e) { |
|
109 | - // Do nothing on purpose |
|
110 | - } |
|
111 | - } |
|
112 | - |
|
113 | - return $iterator; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Create a HolidayIterator from an ISO 3166-code. |
|
118 | - * |
|
119 | - * @param string $isoCode |
|
120 | - * |
|
121 | - * @return HolidayIterator |
|
122 | - */ |
|
123 | - public function createIteratorFromISO3166(string $isoCode): HolidayIterator |
|
124 | - { |
|
125 | - $file = __DIR__ . '/../share/%s.xml'; |
|
126 | - $file1 = sprintf($file, $isoCode); |
|
127 | - |
|
128 | - if (!is_readable($file1)) { |
|
129 | - throw new UnexpectedValueException(sprintf( |
|
130 | - 'There is no holiday-file for %s', |
|
131 | - $isoCode |
|
132 | - )); |
|
133 | - } |
|
134 | - |
|
135 | - return $this->createIteratorFromXmlFile($file1); |
|
136 | - } |
|
137 | - |
|
138 | - private function getElement(DOMElement $child): HolidayIteratorItemInterface |
|
139 | - { |
|
140 | - foreach ($this->factories as $factory) { |
|
141 | - $element = $factory->itemFromDomElement($child); |
|
142 | - if ($element instanceof HolidayIteratorItemInterface) { |
|
143 | - return $element; |
|
144 | - } |
|
145 | - } |
|
146 | - |
|
147 | - throw new RuntimeException('Unknown element encountered'); |
|
148 | - } |
|
149 | - |
|
150 | - private function decorateElement(HolidayIteratorItemInterface $element, DOMElement $child): HolidayIteratorItemInterface |
|
151 | - { |
|
152 | - foreach ($this->decorators as $decorator) { |
|
153 | - $element = $decorator->decorate($element, $child); |
|
154 | - } |
|
155 | - |
|
156 | - return $element; |
|
157 | - } |
|
54 | + /** @var ItemFromDomElementCreator[] */ |
|
55 | + private $factories; |
|
56 | + |
|
57 | + /** @var DecorateFromDomElement[] */ |
|
58 | + private $decorators; |
|
59 | + |
|
60 | + public function __construct() |
|
61 | + { |
|
62 | + $this->factories = [ |
|
63 | + new EasterFactory(), |
|
64 | + new EasterOrthodoxFactory(), |
|
65 | + new DateFactory(), |
|
66 | + new DateFollowupFactory(), |
|
67 | + new RelativeFactory(), |
|
68 | + ]; |
|
69 | + |
|
70 | + $this->decorators = [ |
|
71 | + new ObservanceDecoratorFactory(), |
|
72 | + ]; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Create a HolidayIterator from an XML-File |
|
77 | + * |
|
78 | + * The provided XML-File has to validate against the holiday.xsd-file you |
|
79 | + * can find in this projects "share" folder. |
|
80 | + * |
|
81 | + * @param string $file |
|
82 | + * |
|
83 | + * @return HolidayIterator |
|
84 | + */ |
|
85 | + public function createIteratorFromXmlFile(string $file): HolidayIterator |
|
86 | + { |
|
87 | + $iterator = new HolidayIterator(); |
|
88 | + |
|
89 | + $dom = new DOMDocument('1.0', 'UTF-8'); |
|
90 | + $dom->load($file); |
|
91 | + $dom->xinclude(); |
|
92 | + |
|
93 | + if (!@$dom->schemaValidate(__DIR__ . '/../share/holidays.xsd')) { |
|
94 | + throw new Exception('XML-File does not validate agains schema'); |
|
95 | + } |
|
96 | + foreach ($dom->documentElement->childNodes as $child) { |
|
97 | + if (!$child instanceof DOMElement) { |
|
98 | + continue; |
|
99 | + } |
|
100 | + if ($child->nodeName === 'resources') { |
|
101 | + continue; |
|
102 | + } |
|
103 | + |
|
104 | + try { |
|
105 | + $element = $this->getElement($child); |
|
106 | + $element = $this->decorateElement($element, $child); |
|
107 | + $iterator->append($element); |
|
108 | + } catch (Throwable $e) { |
|
109 | + // Do nothing on purpose |
|
110 | + } |
|
111 | + } |
|
112 | + |
|
113 | + return $iterator; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Create a HolidayIterator from an ISO 3166-code. |
|
118 | + * |
|
119 | + * @param string $isoCode |
|
120 | + * |
|
121 | + * @return HolidayIterator |
|
122 | + */ |
|
123 | + public function createIteratorFromISO3166(string $isoCode): HolidayIterator |
|
124 | + { |
|
125 | + $file = __DIR__ . '/../share/%s.xml'; |
|
126 | + $file1 = sprintf($file, $isoCode); |
|
127 | + |
|
128 | + if (!is_readable($file1)) { |
|
129 | + throw new UnexpectedValueException(sprintf( |
|
130 | + 'There is no holiday-file for %s', |
|
131 | + $isoCode |
|
132 | + )); |
|
133 | + } |
|
134 | + |
|
135 | + return $this->createIteratorFromXmlFile($file1); |
|
136 | + } |
|
137 | + |
|
138 | + private function getElement(DOMElement $child): HolidayIteratorItemInterface |
|
139 | + { |
|
140 | + foreach ($this->factories as $factory) { |
|
141 | + $element = $factory->itemFromDomElement($child); |
|
142 | + if ($element instanceof HolidayIteratorItemInterface) { |
|
143 | + return $element; |
|
144 | + } |
|
145 | + } |
|
146 | + |
|
147 | + throw new RuntimeException('Unknown element encountered'); |
|
148 | + } |
|
149 | + |
|
150 | + private function decorateElement(HolidayIteratorItemInterface $element, DOMElement $child): HolidayIteratorItemInterface |
|
151 | + { |
|
152 | + foreach ($this->decorators as $decorator) { |
|
153 | + $element = $decorator->decorate($element, $child); |
|
154 | + } |
|
155 | + |
|
156 | + return $element; |
|
157 | + } |
|
158 | 158 | } |
@@ -116,7 +116,7 @@ |
||
116 | 116 | $day = $cal->toDateTime(); |
117 | 117 | $day->modify($direction . ' ' . $modifiedDay); |
118 | 118 | $cal->setTime($day->getTimestamp() * 1000); |
119 | - $cal2 = clone $this->calendar; |
|
119 | + $cal2 = clone $this->calendar; |
|
120 | 120 | $cal2->setTime($dateTime->getTimestamp() * 1000); |
121 | 121 | |
122 | 122 | if (null !== $this->year && $cal->get(IntlCalendar::FIELD_YEAR) !== $cal2->get(IntlCalendar::FIELD_YEAR)) { |
@@ -95,34 +95,34 @@ |
||
95 | 95 | |
96 | 96 | public function isFollowUpDay(DateTimeInterface $dateTime, string $followUpDay): bool |
97 | 97 | { |
98 | - return $this->isModifiedDate($dateTime, $followUpDay, 'next'); |
|
98 | + return $this->isModifiedDate($dateTime, $followUpDay, 'next'); |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | public function isPreviousDay(DateTimeInterface $dateTime, string $previousDay): bool |
102 | 102 | { |
103 | - return $this->isModifiedDate($dateTime, $previousDay, 'previous'); |
|
103 | + return $this->isModifiedDate($dateTime, $previousDay, 'previous'); |
|
104 | 104 | } |
105 | 105 | |
106 | - private function isModifiedDate(DateTimeInterface $dateTime, string $modifiedDay, string $direction): bool |
|
107 | - { |
|
108 | - $cal = clone $this->calendar; |
|
109 | - $cal = self::setGregorianYearForDate((int) $dateTime->format('Y'), $cal); |
|
110 | - $day = $cal->toDateTime(); |
|
111 | - $day->modify($direction . ' ' . $modifiedDay); |
|
112 | - $cal->setTime($day->getTimestamp() * 1000); |
|
113 | - $cal2 = clone $this->calendar; |
|
114 | - $cal2->setTime($dateTime->getTimestamp() * 1000); |
|
115 | - |
|
116 | - if (null !== $this->year && $cal->get(IntlCalendar::FIELD_YEAR) !== $cal2->get(IntlCalendar::FIELD_YEAR)) { |
|
117 | - return false; |
|
118 | - } |
|
119 | - |
|
120 | - if ($cal->get(IntlCalendar::FIELD_MONTH) !== $cal2->get(IntlCalendar::FIELD_MONTH)) { |
|
121 | - return false; |
|
122 | - } |
|
123 | - |
|
124 | - return $cal->get(IntlCalendar::FIELD_DAY_OF_MONTH) === $cal2->get(IntlCalendar::FIELD_DAY_OF_MONTH); |
|
125 | - } |
|
106 | + private function isModifiedDate(DateTimeInterface $dateTime, string $modifiedDay, string $direction): bool |
|
107 | + { |
|
108 | + $cal = clone $this->calendar; |
|
109 | + $cal = self::setGregorianYearForDate((int) $dateTime->format('Y'), $cal); |
|
110 | + $day = $cal->toDateTime(); |
|
111 | + $day->modify($direction . ' ' . $modifiedDay); |
|
112 | + $cal->setTime($day->getTimestamp() * 1000); |
|
113 | + $cal2 = clone $this->calendar; |
|
114 | + $cal2->setTime($dateTime->getTimestamp() * 1000); |
|
115 | + |
|
116 | + if (null !== $this->year && $cal->get(IntlCalendar::FIELD_YEAR) !== $cal2->get(IntlCalendar::FIELD_YEAR)) { |
|
117 | + return false; |
|
118 | + } |
|
119 | + |
|
120 | + if ($cal->get(IntlCalendar::FIELD_MONTH) !== $cal2->get(IntlCalendar::FIELD_MONTH)) { |
|
121 | + return false; |
|
122 | + } |
|
123 | + |
|
124 | + return $cal->get(IntlCalendar::FIELD_DAY_OF_MONTH) === $cal2->get(IntlCalendar::FIELD_DAY_OF_MONTH); |
|
125 | + } |
|
126 | 126 | |
127 | 127 | public function getWeekdayForGregorianYear(int $year): int |
128 | 128 | { |