@@ -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 | } |
@@ -12,35 +12,35 @@ |
||
| 12 | 12 | |
| 13 | 13 | class SwapRule |
| 14 | 14 | { |
| 15 | - private $swapDirection; |
|
| 16 | - |
|
| 17 | - private $swapToDay; |
|
| 18 | - |
|
| 19 | - /** @var GregorianWeekday[] */ |
|
| 20 | - private $swapWhenDay; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @param GregorianWeekday[] $swapWhenGregorianDay |
|
| 24 | - */ |
|
| 25 | - public function __construct(SwapDirection $direction, GregorianWeekday $swapToGregorianDay, GregorianWeekday ...$swapWhenGregorianDay) |
|
| 26 | - { |
|
| 27 | - $this->swapDirection = $direction; |
|
| 28 | - $this->swapToDay = $swapToGregorianDay; |
|
| 29 | - $this->swapWhenDay = $swapWhenGregorianDay; |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - public function getDirection(): SwapDirection |
|
| 33 | - { |
|
| 34 | - return $this->swapDirection; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - public function getSwapToDay(): GregorianWeekday |
|
| 38 | - { |
|
| 39 | - return $this->swapToDay; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - public function getSwapWhenDays(): array |
|
| 43 | - { |
|
| 44 | - return $this->swapWhenDay; |
|
| 45 | - } |
|
| 15 | + private $swapDirection; |
|
| 16 | + |
|
| 17 | + private $swapToDay; |
|
| 18 | + |
|
| 19 | + /** @var GregorianWeekday[] */ |
|
| 20 | + private $swapWhenDay; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @param GregorianWeekday[] $swapWhenGregorianDay |
|
| 24 | + */ |
|
| 25 | + public function __construct(SwapDirection $direction, GregorianWeekday $swapToGregorianDay, GregorianWeekday ...$swapWhenGregorianDay) |
|
| 26 | + { |
|
| 27 | + $this->swapDirection = $direction; |
|
| 28 | + $this->swapToDay = $swapToGregorianDay; |
|
| 29 | + $this->swapWhenDay = $swapWhenGregorianDay; |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + public function getDirection(): SwapDirection |
|
| 33 | + { |
|
| 34 | + return $this->swapDirection; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + public function getSwapToDay(): GregorianWeekday |
|
| 38 | + { |
|
| 39 | + return $this->swapToDay; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + public function getSwapWhenDays(): array |
|
| 43 | + { |
|
| 44 | + return $this->swapWhenDay; |
|
| 45 | + } |
|
| 46 | 46 | } |
@@ -69,12 +69,12 @@ |
||
| 69 | 69 | $this->day |
| 70 | 70 | )); |
| 71 | 71 | |
| 72 | - /** @var DateTimeImmutable|false $day */ |
|
| 72 | + /** @var DateTimeImmutable|false $day */ |
|
| 73 | 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 | 79 | return $date->format('Y-m-d') === $day->format('Y-m-d'); |
| 80 | 80 | } |
@@ -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 | } |
@@ -20,136 +20,136 @@ |
||
| 20 | 20 | |
| 21 | 21 | final class GregorianWeekday |
| 22 | 22 | { |
| 23 | - /** @var string */ |
|
| 24 | - private $value; |
|
| 23 | + /** @var string */ |
|
| 24 | + private $value; |
|
| 25 | 25 | |
| 26 | - /** @var array<string, GregorianWeekday> */ |
|
| 27 | - private static $instances = []; |
|
| 26 | + /** @var array<string, GregorianWeekday> */ |
|
| 27 | + private static $instances = []; |
|
| 28 | 28 | |
| 29 | - private const MONDAY = 'monday'; |
|
| 30 | - private const TUESDAY = 'tuesday'; |
|
| 31 | - private const WEDNESDAY = 'wednesday'; |
|
| 32 | - private const THURSDAY = 'thursday'; |
|
| 33 | - private const FRIDAY = 'friday'; |
|
| 34 | - private const SATURDAY = 'saturday'; |
|
| 35 | - private const SUNDAY = 'sunday'; |
|
| 29 | + private const MONDAY = 'monday'; |
|
| 30 | + private const TUESDAY = 'tuesday'; |
|
| 31 | + private const WEDNESDAY = 'wednesday'; |
|
| 32 | + private const THURSDAY = 'thursday'; |
|
| 33 | + private const FRIDAY = 'friday'; |
|
| 34 | + private const SATURDAY = 'saturday'; |
|
| 35 | + private const SUNDAY = 'sunday'; |
|
| 36 | 36 | |
| 37 | - private function __construct(string $value) |
|
| 37 | + private function __construct(string $value) |
|
| 38 | 38 | { |
| 39 | - $this->value = $value; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - public function getValue(): string |
|
| 43 | - { |
|
| 44 | - return $this->value; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - public static function monday(): self |
|
| 48 | - { |
|
| 49 | - if (! isset(self::$instances[self::MONDAY])) { |
|
| 50 | - self::$instances[self::MONDAY] = new self(self::MONDAY); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - return self::$instances[self::MONDAY]; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - public static function tuesday(): self |
|
| 57 | - { |
|
| 58 | - if (! isset(self::$instances[self::TUESDAY])) { |
|
| 59 | - self::$instances[self::TUESDAY] = new self(self::TUESDAY); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - return self::$instances[self::TUESDAY]; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - public static function wednesday(): self |
|
| 66 | - { |
|
| 67 | - if (! isset(self::$instances[self::WEDNESDAY])) { |
|
| 68 | - self::$instances[self::WEDNESDAY] = new self(self::WEDNESDAY); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - return self::$instances[self::WEDNESDAY]; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - public static function thursday(): self |
|
| 75 | - { |
|
| 76 | - if (! isset(self::$instances[self::THURSDAY])) { |
|
| 77 | - self::$instances[self::THURSDAY] = new self(self::THURSDAY); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - return self::$instances[self::THURSDAY]; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - public static function friday(): self |
|
| 84 | - { |
|
| 85 | - if (! isset(self::$instances[self::FRIDAY])) { |
|
| 86 | - self::$instances[self::FRIDAY] = new self(self::FRIDAY); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - return self::$instances[self::FRIDAY]; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - public static function saturday(): self |
|
| 93 | - { |
|
| 94 | - if (! isset(self::$instances[self::SATURDAY])) { |
|
| 95 | - self::$instances[self::SATURDAY] = new self(self::SATURDAY); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - return self::$instances[self::SATURDAY]; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - public static function sunday(): self |
|
| 102 | - { |
|
| 103 | - if (! isset(self::$instances[self::SUNDAY])) { |
|
| 104 | - self::$instances[self::SUNDAY] = new self(self::SUNDAY); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return self::$instances[self::SUNDAY]; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - public static function fromString(string $weekday): self |
|
| 111 | - { |
|
| 112 | - if (! method_exists(self::class, strtolower($weekday))) { |
|
| 113 | - throw new RuntimeException(sprintf( |
|
| 114 | - 'Weekday "%s" is not known', |
|
| 115 | - $weekday |
|
| 116 | - )); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** @var GregorianWeekday $gregorianWeekday */ |
|
| 120 | - $gregorianWeekday = [self::class, strtolower($weekday)](); |
|
| 121 | - |
|
| 122 | - return $gregorianWeekday; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - public static function fromDateTimeInterface(DateTimeInterface $date): self |
|
| 126 | - { |
|
| 127 | - return self::fromString($date->format('l')); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - public static function fromIntlWeekday(int $weekday): self |
|
| 131 | - { |
|
| 132 | - $mapper = [ |
|
| 133 | - IntlCalendar::DOW_SUNDAY => 'sunday', |
|
| 134 | - IntlCalendar::DOW_MONDAY => 'monday', |
|
| 135 | - IntlCalendar::DOW_TUESDAY => 'tuesday', |
|
| 136 | - IntlCalendar::DOW_WEDNESDAY => 'wednesday', |
|
| 137 | - IntlCalendar::DOW_THURSDAY => 'thursday', |
|
| 138 | - IntlCalendar::DOW_FRIDAY => 'friday', |
|
| 139 | - IntlCalendar::DOW_SATURDAY => 'saturday', |
|
| 140 | - ]; |
|
| 141 | - if (! isset($mapper[$weekday])) { |
|
| 142 | - throw new UnexpectedValueException(sprintf( |
|
| 143 | - 'IntlCalendar weekday %s could not be resolved', |
|
| 144 | - $weekday |
|
| 145 | - )); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - return self::fromString($mapper[$weekday]); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - public function __toString(): string |
|
| 152 | - { |
|
| 153 | - return $this->getValue(); |
|
| 154 | - } |
|
| 39 | + $this->value = $value; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + public function getValue(): string |
|
| 43 | + { |
|
| 44 | + return $this->value; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + public static function monday(): self |
|
| 48 | + { |
|
| 49 | + if (! isset(self::$instances[self::MONDAY])) { |
|
| 50 | + self::$instances[self::MONDAY] = new self(self::MONDAY); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + return self::$instances[self::MONDAY]; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + public static function tuesday(): self |
|
| 57 | + { |
|
| 58 | + if (! isset(self::$instances[self::TUESDAY])) { |
|
| 59 | + self::$instances[self::TUESDAY] = new self(self::TUESDAY); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + return self::$instances[self::TUESDAY]; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + public static function wednesday(): self |
|
| 66 | + { |
|
| 67 | + if (! isset(self::$instances[self::WEDNESDAY])) { |
|
| 68 | + self::$instances[self::WEDNESDAY] = new self(self::WEDNESDAY); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + return self::$instances[self::WEDNESDAY]; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + public static function thursday(): self |
|
| 75 | + { |
|
| 76 | + if (! isset(self::$instances[self::THURSDAY])) { |
|
| 77 | + self::$instances[self::THURSDAY] = new self(self::THURSDAY); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + return self::$instances[self::THURSDAY]; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + public static function friday(): self |
|
| 84 | + { |
|
| 85 | + if (! isset(self::$instances[self::FRIDAY])) { |
|
| 86 | + self::$instances[self::FRIDAY] = new self(self::FRIDAY); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + return self::$instances[self::FRIDAY]; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + public static function saturday(): self |
|
| 93 | + { |
|
| 94 | + if (! isset(self::$instances[self::SATURDAY])) { |
|
| 95 | + self::$instances[self::SATURDAY] = new self(self::SATURDAY); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + return self::$instances[self::SATURDAY]; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + public static function sunday(): self |
|
| 102 | + { |
|
| 103 | + if (! isset(self::$instances[self::SUNDAY])) { |
|
| 104 | + self::$instances[self::SUNDAY] = new self(self::SUNDAY); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + return self::$instances[self::SUNDAY]; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + public static function fromString(string $weekday): self |
|
| 111 | + { |
|
| 112 | + if (! method_exists(self::class, strtolower($weekday))) { |
|
| 113 | + throw new RuntimeException(sprintf( |
|
| 114 | + 'Weekday "%s" is not known', |
|
| 115 | + $weekday |
|
| 116 | + )); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** @var GregorianWeekday $gregorianWeekday */ |
|
| 120 | + $gregorianWeekday = [self::class, strtolower($weekday)](); |
|
| 121 | + |
|
| 122 | + return $gregorianWeekday; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + public static function fromDateTimeInterface(DateTimeInterface $date): self |
|
| 126 | + { |
|
| 127 | + return self::fromString($date->format('l')); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + public static function fromIntlWeekday(int $weekday): self |
|
| 131 | + { |
|
| 132 | + $mapper = [ |
|
| 133 | + IntlCalendar::DOW_SUNDAY => 'sunday', |
|
| 134 | + IntlCalendar::DOW_MONDAY => 'monday', |
|
| 135 | + IntlCalendar::DOW_TUESDAY => 'tuesday', |
|
| 136 | + IntlCalendar::DOW_WEDNESDAY => 'wednesday', |
|
| 137 | + IntlCalendar::DOW_THURSDAY => 'thursday', |
|
| 138 | + IntlCalendar::DOW_FRIDAY => 'friday', |
|
| 139 | + IntlCalendar::DOW_SATURDAY => 'saturday', |
|
| 140 | + ]; |
|
| 141 | + if (! isset($mapper[$weekday])) { |
|
| 142 | + throw new UnexpectedValueException(sprintf( |
|
| 143 | + 'IntlCalendar weekday %s could not be resolved', |
|
| 144 | + $weekday |
|
| 145 | + )); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + return self::fromString($mapper[$weekday]); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + public function __toString(): string |
|
| 152 | + { |
|
| 153 | + return $this->getValue(); |
|
| 154 | + } |
|
| 155 | 155 | } |
@@ -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 | { |
@@ -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 | } |
@@ -60,12 +60,12 @@ discard block |
||
| 60 | 60 | { |
| 61 | 61 | $year = (int) $date->format('Y'); |
| 62 | 62 | |
| 63 | - $easter = $this->getEaster($year); |
|
| 64 | - $day = $this->getOffsetDay($easter, $this->offset); |
|
| 63 | + $easter = $this->getEaster($year); |
|
| 64 | + $day = $this->getOffsetDay($easter, $this->offset); |
|
| 65 | 65 | |
| 66 | - if (false === $day) { |
|
| 67 | - return false; |
|
| 68 | - } |
|
| 66 | + if (false === $day) { |
|
| 67 | + return false; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | 70 | $comparator = new DateIntervalComparator(); |
| 71 | 71 | return 0 > $comparator->compare($day->diff($date), new DateInterval('P1D')); |
@@ -89,16 +89,16 @@ discard block |
||
| 89 | 89 | return $base->add(new DateInterval("P{$days}D")); |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * @param DateTime|DateTimeImmutable $date |
|
| 94 | - * @return DateTime|DateTimeImmutable|false |
|
| 95 | - */ |
|
| 96 | - private function getOffsetDay($date, int $offset) |
|
| 97 | - { |
|
| 98 | - if ($offset < 0) { |
|
| 99 | - return $date->sub(new DateInterval('P' . $offset * -1 . 'D')); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - return $date->add(new DateInterval('P' . $offset . 'D')); |
|
| 103 | - } |
|
| 92 | + /** |
|
| 93 | + * @param DateTime|DateTimeImmutable $date |
|
| 94 | + * @return DateTime|DateTimeImmutable|false |
|
| 95 | + */ |
|
| 96 | + private function getOffsetDay($date, int $offset) |
|
| 97 | + { |
|
| 98 | + if ($offset < 0) { |
|
| 99 | + return $date->sub(new DateInterval('P' . $offset * -1 . 'D')); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + return $date->add(new DateInterval('P' . $offset . 'D')); |
|
| 103 | + } |
|
| 104 | 104 | } |