@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | |
| 47 | 47 | public static function monday(): self |
| 48 | 48 | { |
| 49 | - if (! isset(self::$instances[self::MONDAY])) { |
|
| 49 | + if (!isset(self::$instances[self::MONDAY])) { |
|
| 50 | 50 | self::$instances[self::MONDAY] = new self(self::MONDAY); |
| 51 | 51 | } |
| 52 | 52 | |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | |
| 56 | 56 | public static function tuesday(): self |
| 57 | 57 | { |
| 58 | - if (! isset(self::$instances[self::TUESDAY])) { |
|
| 58 | + if (!isset(self::$instances[self::TUESDAY])) { |
|
| 59 | 59 | self::$instances[self::TUESDAY] = new self(self::TUESDAY); |
| 60 | 60 | } |
| 61 | 61 | |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | |
| 65 | 65 | public static function wednesday(): self |
| 66 | 66 | { |
| 67 | - if (! isset(self::$instances[self::WEDNESDAY])) { |
|
| 67 | + if (!isset(self::$instances[self::WEDNESDAY])) { |
|
| 68 | 68 | self::$instances[self::WEDNESDAY] = new self(self::WEDNESDAY); |
| 69 | 69 | } |
| 70 | 70 | |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | |
| 74 | 74 | public static function thursday(): self |
| 75 | 75 | { |
| 76 | - if (! isset(self::$instances[self::THURSDAY])) { |
|
| 76 | + if (!isset(self::$instances[self::THURSDAY])) { |
|
| 77 | 77 | self::$instances[self::THURSDAY] = new self(self::THURSDAY); |
| 78 | 78 | } |
| 79 | 79 | |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | |
| 83 | 83 | public static function friday(): self |
| 84 | 84 | { |
| 85 | - if (! isset(self::$instances[self::FRIDAY])) { |
|
| 85 | + if (!isset(self::$instances[self::FRIDAY])) { |
|
| 86 | 86 | self::$instances[self::FRIDAY] = new self(self::FRIDAY); |
| 87 | 87 | } |
| 88 | 88 | |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | |
| 92 | 92 | public static function saturday(): self |
| 93 | 93 | { |
| 94 | - if (! isset(self::$instances[self::SATURDAY])) { |
|
| 94 | + if (!isset(self::$instances[self::SATURDAY])) { |
|
| 95 | 95 | self::$instances[self::SATURDAY] = new self(self::SATURDAY); |
| 96 | 96 | } |
| 97 | 97 | |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | |
| 101 | 101 | public static function sunday(): self |
| 102 | 102 | { |
| 103 | - if (! isset(self::$instances[self::SUNDAY])) { |
|
| 103 | + if (!isset(self::$instances[self::SUNDAY])) { |
|
| 104 | 104 | self::$instances[self::SUNDAY] = new self(self::SUNDAY); |
| 105 | 105 | } |
| 106 | 106 | |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | |
| 110 | 110 | public static function fromString(string $weekday): self |
| 111 | 111 | { |
| 112 | - if (! method_exists(self::class, strtolower($weekday))) { |
|
| 112 | + if (!method_exists(self::class, strtolower($weekday))) { |
|
| 113 | 113 | throw new RuntimeException('Weekday not found'); |
| 114 | 114 | } |
| 115 | 115 | |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | IntlCalendar::DOW_FRIDAY => 'friday', |
| 133 | 133 | IntlCalendar::DOW_SATURDAY => 'saturday', |
| 134 | 134 | ]; |
| 135 | - if (! isset($mapper[$weekday])) { |
|
| 135 | + if (!isset($mapper[$weekday])) { |
|
| 136 | 136 | throw new UnexpectedValueException(sprintf( |
| 137 | 137 | 'IntlCalendar weekday %s could not be resolved', |
| 138 | 138 | $weekday |
@@ -20,133 +20,133 @@ |
||
| 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('Weekday not found'); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** @var GregorianWeekday $gregorianWeekday */ |
|
| 117 | - $gregorianWeekday = [self::class, strtolower($weekday)](); |
|
| 118 | - |
|
| 119 | - return $gregorianWeekday; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - public static function fromDateTimeInterface(DateTimeInterface $date): self |
|
| 123 | - { |
|
| 124 | - return self::fromString($date->format('l')); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - public static function fromIntlWeekday(int $weekday): self |
|
| 128 | - { |
|
| 129 | - $mapper = [ |
|
| 130 | - IntlCalendar::DOW_SUNDAY => 'sunday', |
|
| 131 | - IntlCalendar::DOW_MONDAY => 'monday', |
|
| 132 | - IntlCalendar::DOW_TUESDAY => 'tuesday', |
|
| 133 | - IntlCalendar::DOW_WEDNESDAY => 'wednesday', |
|
| 134 | - IntlCalendar::DOW_THURSDAY => 'thursday', |
|
| 135 | - IntlCalendar::DOW_FRIDAY => 'friday', |
|
| 136 | - IntlCalendar::DOW_SATURDAY => 'saturday', |
|
| 137 | - ]; |
|
| 138 | - if (! isset($mapper[$weekday])) { |
|
| 139 | - throw new UnexpectedValueException(sprintf( |
|
| 140 | - 'IntlCalendar weekday %s could not be resolved', |
|
| 141 | - $weekday |
|
| 142 | - )); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - return self::fromString($mapper[$weekday]); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - public function __toString(): string |
|
| 149 | - { |
|
| 150 | - return $this->getValue(); |
|
| 151 | - } |
|
| 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('Weekday not found'); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** @var GregorianWeekday $gregorianWeekday */ |
|
| 117 | + $gregorianWeekday = [self::class, strtolower($weekday)](); |
|
| 118 | + |
|
| 119 | + return $gregorianWeekday; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + public static function fromDateTimeInterface(DateTimeInterface $date): self |
|
| 123 | + { |
|
| 124 | + return self::fromString($date->format('l')); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + public static function fromIntlWeekday(int $weekday): self |
|
| 128 | + { |
|
| 129 | + $mapper = [ |
|
| 130 | + IntlCalendar::DOW_SUNDAY => 'sunday', |
|
| 131 | + IntlCalendar::DOW_MONDAY => 'monday', |
|
| 132 | + IntlCalendar::DOW_TUESDAY => 'tuesday', |
|
| 133 | + IntlCalendar::DOW_WEDNESDAY => 'wednesday', |
|
| 134 | + IntlCalendar::DOW_THURSDAY => 'thursday', |
|
| 135 | + IntlCalendar::DOW_FRIDAY => 'friday', |
|
| 136 | + IntlCalendar::DOW_SATURDAY => 'saturday', |
|
| 137 | + ]; |
|
| 138 | + if (! isset($mapper[$weekday])) { |
|
| 139 | + throw new UnexpectedValueException(sprintf( |
|
| 140 | + 'IntlCalendar weekday %s could not be resolved', |
|
| 141 | + $weekday |
|
| 142 | + )); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + return self::fromString($mapper[$weekday]); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + public function __toString(): string |
|
| 149 | + { |
|
| 150 | + return $this->getValue(); |
|
| 151 | + } |
|
| 152 | 152 | } |
@@ -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 | } |