@@ -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 | } |
@@ -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)) { |
@@ -91,41 +91,41 @@ |
||
| 91 | 91 | return $cal->get(IntlCalendar::FIELD_DAY_OF_MONTH) === $this->calendar->get(IntlCalendar::FIELD_DAY_OF_MONTH); |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - public function getCalendar(): IntlCalendar |
|
| 95 | - { |
|
| 96 | - return clone $this->calendar; |
|
| 97 | - } |
|
| 94 | + public function getCalendar(): IntlCalendar |
|
| 95 | + { |
|
| 96 | + return clone $this->calendar; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - public function hasYearSet(): bool |
|
| 100 | - { |
|
| 101 | - return null !== $this->year; |
|
| 102 | - } |
|
| 99 | + public function hasYearSet(): bool |
|
| 100 | + { |
|
| 101 | + return null !== $this->year; |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | 104 | public function isFollowUpDay(DateTimeInterface $dateTime, string $followUpDay): bool |
| 105 | 105 | { |
| 106 | - return $this->isModifiedDate($dateTime, $followUpDay, 'next'); |
|
| 106 | + return $this->isModifiedDate($dateTime, $followUpDay, 'next'); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - private function isModifiedDate(DateTimeInterface $dateTime, string $modifiedDay, string $direction): bool |
|
| 110 | - { |
|
| 111 | - $cal = clone $this->calendar; |
|
| 112 | - $cal = self::setGregorianYearForDate((int) $dateTime->format('Y'), $cal); |
|
| 113 | - $day = $cal->toDateTime(); |
|
| 114 | - $day->modify($direction . ' ' . $modifiedDay); |
|
| 115 | - $cal->setTime($day->getTimestamp() * 1000); |
|
| 116 | - $cal2 = clone $this->calendar; |
|
| 117 | - $cal2->setTime($dateTime->getTimestamp() * 1000); |
|
| 118 | - |
|
| 119 | - if (null !== $this->year && $cal->get(IntlCalendar::FIELD_YEAR) !== $cal2->get(IntlCalendar::FIELD_YEAR)) { |
|
| 120 | - return false; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - if ($cal->get(IntlCalendar::FIELD_MONTH) !== $cal2->get(IntlCalendar::FIELD_MONTH)) { |
|
| 124 | - return false; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - return $cal->get(IntlCalendar::FIELD_DAY_OF_MONTH) === $cal2->get(IntlCalendar::FIELD_DAY_OF_MONTH); |
|
| 128 | - } |
|
| 109 | + private function isModifiedDate(DateTimeInterface $dateTime, string $modifiedDay, string $direction): bool |
|
| 110 | + { |
|
| 111 | + $cal = clone $this->calendar; |
|
| 112 | + $cal = self::setGregorianYearForDate((int) $dateTime->format('Y'), $cal); |
|
| 113 | + $day = $cal->toDateTime(); |
|
| 114 | + $day->modify($direction . ' ' . $modifiedDay); |
|
| 115 | + $cal->setTime($day->getTimestamp() * 1000); |
|
| 116 | + $cal2 = clone $this->calendar; |
|
| 117 | + $cal2->setTime($dateTime->getTimestamp() * 1000); |
|
| 118 | + |
|
| 119 | + if (null !== $this->year && $cal->get(IntlCalendar::FIELD_YEAR) !== $cal2->get(IntlCalendar::FIELD_YEAR)) { |
|
| 120 | + return false; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + if ($cal->get(IntlCalendar::FIELD_MONTH) !== $cal2->get(IntlCalendar::FIELD_MONTH)) { |
|
| 124 | + return false; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + return $cal->get(IntlCalendar::FIELD_DAY_OF_MONTH) === $cal2->get(IntlCalendar::FIELD_DAY_OF_MONTH); |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | 130 | public function getWeekdayForGregorianYear(int $year): int |
| 131 | 131 | { |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | public static function forward(): self |
| 45 | 45 | { |
| 46 | - if (! isset(self::$instances[self::FORWARD])) { |
|
| 46 | + if (!isset(self::$instances[self::FORWARD])) { |
|
| 47 | 47 | self::$instances[self::FORWARD] = new self(self::FORWARD); |
| 48 | 48 | } |
| 49 | 49 | |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | |
| 53 | 53 | public static function rewind(): self |
| 54 | 54 | { |
| 55 | - if (! isset(self::$instances[self::REWIND])) { |
|
| 55 | + if (!isset(self::$instances[self::REWIND])) { |
|
| 56 | 56 | self::$instances[self::REWIND] = new self(self::REWIND); |
| 57 | 57 | } |
| 58 | 58 | |
@@ -12,52 +12,52 @@ |
||
| 12 | 12 | |
| 13 | 13 | final class SwapDirection |
| 14 | 14 | { |
| 15 | - /** @var string */ |
|
| 16 | - private $value; |
|
| 15 | + /** @var string */ |
|
| 16 | + private $value; |
|
| 17 | 17 | |
| 18 | - /** @var array<string, self> */ |
|
| 19 | - private static $instances = []; |
|
| 18 | + /** @var array<string, self> */ |
|
| 19 | + private static $instances = []; |
|
| 20 | 20 | |
| 21 | - private const FORWARD = 'forward'; |
|
| 22 | - private const REWIND = 'rewind'; |
|
| 21 | + private const FORWARD = 'forward'; |
|
| 22 | + private const REWIND = 'rewind'; |
|
| 23 | 23 | |
| 24 | - private function __construct(string $value) |
|
| 24 | + private function __construct(string $value) |
|
| 25 | 25 | { |
| 26 | - $this->value = $value; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - public function getValue(): string |
|
| 30 | - { |
|
| 31 | - return $this->value; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - public function getDateTimeDirection(): string |
|
| 35 | - { |
|
| 36 | - switch ($this->value) { |
|
| 37 | - case self::REWIND: |
|
| 38 | - return 'previous'; |
|
| 39 | - case self::FORWARD: |
|
| 40 | - return 'next'; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - return ''; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public static function forward(): self |
|
| 47 | - { |
|
| 48 | - if (! isset(self::$instances[self::FORWARD])) { |
|
| 49 | - self::$instances[self::FORWARD] = new self(self::FORWARD); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - return self::$instances[self::FORWARD]; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - public static function rewind(): self |
|
| 56 | - { |
|
| 57 | - if (! isset(self::$instances[self::REWIND])) { |
|
| 58 | - self::$instances[self::REWIND] = new self(self::REWIND); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - return self::$instances[self::REWIND]; |
|
| 62 | - } |
|
| 26 | + $this->value = $value; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + public function getValue(): string |
|
| 30 | + { |
|
| 31 | + return $this->value; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + public function getDateTimeDirection(): string |
|
| 35 | + { |
|
| 36 | + switch ($this->value) { |
|
| 37 | + case self::REWIND: |
|
| 38 | + return 'previous'; |
|
| 39 | + case self::FORWARD: |
|
| 40 | + return 'next'; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + return ''; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public static function forward(): self |
|
| 47 | + { |
|
| 48 | + if (! isset(self::$instances[self::FORWARD])) { |
|
| 49 | + self::$instances[self::FORWARD] = new self(self::FORWARD); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + return self::$instances[self::FORWARD]; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + public static function rewind(): self |
|
| 56 | + { |
|
| 57 | + if (! isset(self::$instances[self::REWIND])) { |
|
| 58 | + self::$instances[self::REWIND] = new self(self::REWIND); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + return self::$instances[self::REWIND]; |
|
| 62 | + } |
|
| 63 | 63 | } |
@@ -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 XML-File |
|
| 79 | - * |
|
| 80 | - * The provided XML-File has to validate against the holiday.xsd-file you |
|
| 81 | - * can find in this projects "share" folder. |
|
| 82 | - * |
|
| 83 | - * @param string $file |
|
| 84 | - * |
|
| 85 | - * @return HolidayIterator |
|
| 86 | - */ |
|
| 87 | - public function createIteratorFromXmlFile(string $file): HolidayIterator |
|
| 88 | - { |
|
| 89 | - $iterator = new HolidayIterator(); |
|
| 90 | - |
|
| 91 | - $dom = new DOMDocument('1.0', 'UTF-8'); |
|
| 92 | - $dom->load($file); |
|
| 93 | - $dom->xinclude(); |
|
| 94 | - |
|
| 95 | - if (!@$dom->schemaValidate(__DIR__ . '/../share/holidays.xsd')) { |
|
| 96 | - throw new Exception('XML-File does not validate agains schema'); |
|
| 97 | - } |
|
| 98 | - foreach ($dom->documentElement->childNodes as $child) { |
|
| 99 | - if (!$child instanceof DOMElement) { |
|
| 100 | - continue; |
|
| 101 | - } |
|
| 102 | - if ($child->nodeName === 'resources') { |
|
| 103 | - continue; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - try { |
|
| 107 | - $element = $this->getElement($child); |
|
| 108 | - $element = $this->decorateElement($element, $child); |
|
| 109 | - $iterator->append($element); |
|
| 110 | - } catch (Throwable $e) { |
|
| 111 | - // Do nothing on purpose |
|
| 112 | - } |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - return $iterator; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Create a HolidayIterator from an ISO 3166-code. |
|
| 120 | - * |
|
| 121 | - * @param string $isoCode |
|
| 122 | - * |
|
| 123 | - * @return HolidayIterator |
|
| 124 | - */ |
|
| 125 | - public function createIteratorFromISO3166(string $isoCode): HolidayIterator |
|
| 126 | - { |
|
| 127 | - $file = __DIR__ . '/../share/%s.xml'; |
|
| 128 | - $file1 = sprintf($file, $isoCode); |
|
| 129 | - |
|
| 130 | - if (!is_readable($file1)) { |
|
| 131 | - throw new UnexpectedValueException(sprintf( |
|
| 132 | - 'There is no holiday-file for %s', |
|
| 133 | - $isoCode |
|
| 134 | - )); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - return $this->createIteratorFromXmlFile($file1); |
|
| 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 XML-File |
|
| 79 | + * |
|
| 80 | + * The provided XML-File has to validate against the holiday.xsd-file you |
|
| 81 | + * can find in this projects "share" folder. |
|
| 82 | + * |
|
| 83 | + * @param string $file |
|
| 84 | + * |
|
| 85 | + * @return HolidayIterator |
|
| 86 | + */ |
|
| 87 | + public function createIteratorFromXmlFile(string $file): HolidayIterator |
|
| 88 | + { |
|
| 89 | + $iterator = new HolidayIterator(); |
|
| 90 | + |
|
| 91 | + $dom = new DOMDocument('1.0', 'UTF-8'); |
|
| 92 | + $dom->load($file); |
|
| 93 | + $dom->xinclude(); |
|
| 94 | + |
|
| 95 | + if (!@$dom->schemaValidate(__DIR__ . '/../share/holidays.xsd')) { |
|
| 96 | + throw new Exception('XML-File does not validate agains schema'); |
|
| 97 | + } |
|
| 98 | + foreach ($dom->documentElement->childNodes as $child) { |
|
| 99 | + if (!$child instanceof DOMElement) { |
|
| 100 | + continue; |
|
| 101 | + } |
|
| 102 | + if ($child->nodeName === 'resources') { |
|
| 103 | + continue; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + try { |
|
| 107 | + $element = $this->getElement($child); |
|
| 108 | + $element = $this->decorateElement($element, $child); |
|
| 109 | + $iterator->append($element); |
|
| 110 | + } catch (Throwable $e) { |
|
| 111 | + // Do nothing on purpose |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + return $iterator; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Create a HolidayIterator from an ISO 3166-code. |
|
| 120 | + * |
|
| 121 | + * @param string $isoCode |
|
| 122 | + * |
|
| 123 | + * @return HolidayIterator |
|
| 124 | + */ |
|
| 125 | + public function createIteratorFromISO3166(string $isoCode): HolidayIterator |
|
| 126 | + { |
|
| 127 | + $file = __DIR__ . '/../share/%s.xml'; |
|
| 128 | + $file1 = sprintf($file, $isoCode); |
|
| 129 | + |
|
| 130 | + if (!is_readable($file1)) { |
|
| 131 | + throw new UnexpectedValueException(sprintf( |
|
| 132 | + 'There is no holiday-file for %s', |
|
| 133 | + $isoCode |
|
| 134 | + )); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + return $this->createIteratorFromXmlFile($file1); |
|
| 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 | } |
@@ -61,7 +61,7 @@ |
||
| 61 | 61 | return new SwapRule( |
| 62 | 62 | $direction, |
| 63 | 63 | GregorianWeekday::fromString($to), |
| 64 | - ...array_map(function ($item) { |
|
| 64 | + ...array_map(function($item) { |
|
| 65 | 65 | return GregorianWeekday::fromString($item); |
| 66 | 66 | }, explode(' ', $when)) |
| 67 | 67 | ); |
@@ -22,57 +22,57 @@ |
||
| 22 | 22 | |
| 23 | 23 | final class SwapDecoratorFactory implements DecorateFromDomElement |
| 24 | 24 | { |
| 25 | - public function decorate(HolidayIteratorItemInterface $element, DOMElement $domElement): HolidayIteratorItemInterface |
|
| 26 | - { |
|
| 27 | - $rules = $this->getRulesFromDomElement($domElement); |
|
| 25 | + public function decorate(HolidayIteratorItemInterface $element, DOMElement $domElement): HolidayIteratorItemInterface |
|
| 26 | + { |
|
| 27 | + $rules = $this->getRulesFromDomElement($domElement); |
|
| 28 | 28 | |
| 29 | - if ($rules === []) { |
|
| 30 | - return $element; |
|
| 31 | - } |
|
| 29 | + if ($rules === []) { |
|
| 30 | + return $element; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - $day = CalendarDayFactory::createCalendarDay( |
|
| 34 | - (int) $domElement->getAttribute('day'), |
|
| 35 | - (int) $domElement->getAttribute('month'), |
|
| 36 | - ($domElement->hasAttribute('calendar') ? $domElement->getAttribute('calendar') : 'gregorian') |
|
| 37 | - ); |
|
| 33 | + $day = CalendarDayFactory::createCalendarDay( |
|
| 34 | + (int) $domElement->getAttribute('day'), |
|
| 35 | + (int) $domElement->getAttribute('month'), |
|
| 36 | + ($domElement->hasAttribute('calendar') ? $domElement->getAttribute('calendar') : 'gregorian') |
|
| 37 | + ); |
|
| 38 | 38 | |
| 39 | - if ($domElement->hasAttribute('year')) { |
|
| 40 | - $day->setYear((int) $domElement->getAttribute('year')); |
|
| 41 | - } |
|
| 39 | + if ($domElement->hasAttribute('year')) { |
|
| 40 | + $day->setYear((int) $domElement->getAttribute('year')); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - return new SwapDecorator($element, $day, ...$rules); |
|
| 44 | - } |
|
| 43 | + return new SwapDecorator($element, $day, ...$rules); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - private function createRuleFrom(string $to, string $when, SwapDirection $direction): SwapRule |
|
| 47 | - { |
|
| 48 | - return new SwapRule( |
|
| 49 | - $direction, |
|
| 50 | - GregorianWeekday::fromString($to), |
|
| 51 | - ...array_map(function ($item) { |
|
| 52 | - return GregorianWeekday::fromString($item); |
|
| 53 | - }, explode(' ', $when)) |
|
| 54 | - ); |
|
| 55 | - } |
|
| 46 | + private function createRuleFrom(string $to, string $when, SwapDirection $direction): SwapRule |
|
| 47 | + { |
|
| 48 | + return new SwapRule( |
|
| 49 | + $direction, |
|
| 50 | + GregorianWeekday::fromString($to), |
|
| 51 | + ...array_map(function ($item) { |
|
| 52 | + return GregorianWeekday::fromString($item); |
|
| 53 | + }, explode(' ', $when)) |
|
| 54 | + ); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @return SwapRule[] |
|
| 59 | - */ |
|
| 60 | - private function getRulesFromDomElement(DOMElement $domElement): array |
|
| 61 | - { |
|
| 62 | - $attributes = [ |
|
| 63 | - 'forward' => SwapDirection::forward(), |
|
| 64 | - 'alternateforward' => SwapDirection::forward(), |
|
| 65 | - 'rewind' => SwapDirection::rewind(), |
|
| 66 | - 'alternaterewind' => SwapDirection::rewind(), |
|
| 67 | - ]; |
|
| 57 | + /** |
|
| 58 | + * @return SwapRule[] |
|
| 59 | + */ |
|
| 60 | + private function getRulesFromDomElement(DOMElement $domElement): array |
|
| 61 | + { |
|
| 62 | + $attributes = [ |
|
| 63 | + 'forward' => SwapDirection::forward(), |
|
| 64 | + 'alternateforward' => SwapDirection::forward(), |
|
| 65 | + 'rewind' => SwapDirection::rewind(), |
|
| 66 | + 'alternaterewind' => SwapDirection::rewind(), |
|
| 67 | + ]; |
|
| 68 | 68 | |
| 69 | - $rules = []; |
|
| 70 | - foreach ($attributes as $attribute => $direction) { |
|
| 71 | - if ($domElement->hasAttribute($attribute . 'to') && $domElement->hasAttribute($attribute . 'when')) { |
|
| 72 | - $rules[] = $this->createRuleFrom($domElement->getAttribute($attribute . 'to'), $domElement->getAttribute($attribute . 'when'), $direction); |
|
| 73 | - } |
|
| 74 | - } |
|
| 69 | + $rules = []; |
|
| 70 | + foreach ($attributes as $attribute => $direction) { |
|
| 71 | + if ($domElement->hasAttribute($attribute . 'to') && $domElement->hasAttribute($attribute . 'when')) { |
|
| 72 | + $rules[] = $this->createRuleFrom($domElement->getAttribute($attribute . 'to'), $domElement->getAttribute($attribute . 'when'), $direction); |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - return $rules; |
|
| 77 | - } |
|
| 76 | + return $rules; |
|
| 77 | + } |
|
| 78 | 78 | } |
@@ -22,28 +22,28 @@ |
||
| 22 | 22 | |
| 23 | 23 | class DateFactory implements ItemFromDomElementCreator |
| 24 | 24 | { |
| 25 | - public function itemFromDomElement(DOMElement $element): ?HolidayIteratorItemInterface |
|
| 26 | - { |
|
| 27 | - if ($element->nodeName !== 'date') { |
|
| 28 | - return null; |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - $day = CalendarDayFactory::createCalendarDay( |
|
| 32 | - (int) $element->getAttribute('day'), |
|
| 33 | - (int) $element->getAttribute('month'), |
|
| 34 | - ($element->hasAttribute('calendar') ? $element->getAttribute('calendar') : 'gregorian') |
|
| 35 | - ); |
|
| 36 | - |
|
| 37 | - if ($element->hasAttribute('year')) { |
|
| 38 | - $day->setYear((int) $element->getAttribute('year')); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - $date = new Date( |
|
| 42 | - $element->textContent, |
|
| 25 | + public function itemFromDomElement(DOMElement $element): ?HolidayIteratorItemInterface |
|
| 26 | + { |
|
| 27 | + if ($element->nodeName !== 'date') { |
|
| 28 | + return null; |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + $day = CalendarDayFactory::createCalendarDay( |
|
| 32 | + (int) $element->getAttribute('day'), |
|
| 33 | + (int) $element->getAttribute('month'), |
|
| 34 | + ($element->hasAttribute('calendar') ? $element->getAttribute('calendar') : 'gregorian') |
|
| 35 | + ); |
|
| 36 | + |
|
| 37 | + if ($element->hasAttribute('year')) { |
|
| 38 | + $day->setYear((int) $element->getAttribute('year')); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + $date = new Date( |
|
| 42 | + $element->textContent, |
|
| 43 | 43 | $element->getAttribute('free') === "true", |
| 44 | - $day, |
|
| 45 | - ); |
|
| 44 | + $day, |
|
| 45 | + ); |
|
| 46 | 46 | |
| 47 | - return $date; |
|
| 48 | - } |
|
| 47 | + return $date; |
|
| 48 | + } |
|
| 49 | 49 | } |
@@ -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 | - public function getName(): string |
|
| 54 | - { |
|
| 55 | - return $this->rule->getName(); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - public function isHoliday(): bool |
|
| 59 | - { |
|
| 60 | - return $this->rule->isHoliday(); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - private function ruleMatches(SwapRule $rule, GregorianWeekday $weekday): bool |
|
| 64 | - { |
|
| 65 | - return in_array($weekday, $rule->getSwapWhenDays(), true); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - private function isModifiedDate(DateTimeInterface $dateTime, GregorianWeekday $modifiedDay, SwapDirection $direction): bool |
|
| 69 | - { |
|
| 70 | - $cal = $this->day->getCalendar(); |
|
| 71 | - $cal = CalendarDay::setGregorianYearForDate((int) $dateTime->format('Y'), $cal); |
|
| 72 | - $day = $cal->toDateTime(); |
|
| 73 | - $day->modify($direction->getDateTimeDirection() . ' ' . $modifiedDay); |
|
| 74 | - $cal->setTime($day->getTimestamp() * 1000); |
|
| 75 | - $cal2 = $this->day->getCalendar(); |
|
| 76 | - $cal2->setTime($dateTime->getTimestamp() * 1000); |
|
| 77 | - |
|
| 78 | - if ($this->day->hasYearSet() && $cal->get(IntlCalendar::FIELD_YEAR) !== $cal2->get(IntlCalendar::FIELD_YEAR)) { |
|
| 79 | - return false; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - if ($cal->get(IntlCalendar::FIELD_MONTH) !== $cal2->get(IntlCalendar::FIELD_MONTH)) { |
|
| 83 | - return false; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - return $cal->get(IntlCalendar::FIELD_DAY_OF_MONTH) === $cal2->get(IntlCalendar::FIELD_DAY_OF_MONTH); |
|
| 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 | + public function getName(): string |
|
| 54 | + { |
|
| 55 | + return $this->rule->getName(); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + public function isHoliday(): bool |
|
| 59 | + { |
|
| 60 | + return $this->rule->isHoliday(); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + private function ruleMatches(SwapRule $rule, GregorianWeekday $weekday): bool |
|
| 64 | + { |
|
| 65 | + return in_array($weekday, $rule->getSwapWhenDays(), true); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + private function isModifiedDate(DateTimeInterface $dateTime, GregorianWeekday $modifiedDay, SwapDirection $direction): bool |
|
| 69 | + { |
|
| 70 | + $cal = $this->day->getCalendar(); |
|
| 71 | + $cal = CalendarDay::setGregorianYearForDate((int) $dateTime->format('Y'), $cal); |
|
| 72 | + $day = $cal->toDateTime(); |
|
| 73 | + $day->modify($direction->getDateTimeDirection() . ' ' . $modifiedDay); |
|
| 74 | + $cal->setTime($day->getTimestamp() * 1000); |
|
| 75 | + $cal2 = $this->day->getCalendar(); |
|
| 76 | + $cal2->setTime($dateTime->getTimestamp() * 1000); |
|
| 77 | + |
|
| 78 | + if ($this->day->hasYearSet() && $cal->get(IntlCalendar::FIELD_YEAR) !== $cal2->get(IntlCalendar::FIELD_YEAR)) { |
|
| 79 | + return false; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + if ($cal->get(IntlCalendar::FIELD_MONTH) !== $cal2->get(IntlCalendar::FIELD_MONTH)) { |
|
| 83 | + return false; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + return $cal->get(IntlCalendar::FIELD_DAY_OF_MONTH) === $cal2->get(IntlCalendar::FIELD_DAY_OF_MONTH); |
|
| 87 | + } |
|
| 88 | 88 | } |