From ee5ce46b2dd092b262c3fa62cbf15b0bb67fefff Mon Sep 17 00:00:00 2001 From: Andreas Heigl Date: Sat, 5 Aug 2023 16:10:22 +0200 Subject: [PATCH] Code-refactoring --- src/Calendar.php | 64 +++---- src/CalendarDay.php | 184 ++++++++++----------- src/CalendarDayFactory.php | 22 +-- src/Factory/DateFactory.php | 2 +- src/Factory/EasterOrthodoxFactory.php | 2 +- src/Factory/ObservanceDecoratorFactory.php | 2 +- src/Factory/SwapDecoratorFactory.php | 22 +-- src/GregorianWeekday.php | 62 ++++--- src/Holiday.php | 48 +++--- src/HolidayIterator.php | 25 +-- src/HolidayIteratorFactory.php | 44 ++--- src/HolidayIteratorItemInterface.php | 6 +- src/Holidaychecker.php | 34 ++-- src/IteratorItem/Date.php | 48 +++--- src/IteratorItem/DateFollowUp.php | 144 ++++++++-------- src/IteratorItem/Easter.php | 62 +++---- src/IteratorItem/EasterOrthodox.php | 68 ++++---- src/IteratorItem/Relative.php | 66 ++++---- src/IteratorItem/SwapDecorator.php | 26 +-- src/ObservanceDecorator.php | 36 ++-- src/SwapDirection.php | 48 +++--- 21 files changed, 506 insertions(+), 509 deletions(-) diff --git a/src/Calendar.php b/src/Calendar.php index 579680b..0e2840b 100644 --- a/src/Calendar.php +++ b/src/Calendar.php @@ -36,38 +36,38 @@ class Calendar { - public const BUDDHIST = 'buddhist'; - public const CHINESE = 'chinese'; - public const COPTIC = 'coptic'; - public const ETHIOPIAN = 'ethiopian'; - public const GREGORIAN = 'gregorian'; - public const HEBREW = 'hebrew'; - public const INDIAN = 'indian'; - public const ISLAMIC = 'islamic'; - public const ISLAMIC_CIVIL = 'islamic-civil'; - public const ISLAMIC_UMALQURA = 'islamic-umalqura'; - public const ISLAMIC_RGSA = 'islamic-rgsa'; - public const ISLAMIC_TBLA = 'islamic-tbla'; - public const JAPANESE = 'japanese'; - public const PERSIAN = 'persian'; + public const BUDDHIST = 'buddhist'; + public const CHINESE = 'chinese'; + public const COPTIC = 'coptic'; + public const ETHIOPIAN = 'ethiopian'; + public const GREGORIAN = 'gregorian'; + public const HEBREW = 'hebrew'; + public const INDIAN = 'indian'; + public const ISLAMIC = 'islamic'; + public const ISLAMIC_CIVIL = 'islamic-civil'; + public const ISLAMIC_UMALQURA = 'islamic-umalqura'; + public const ISLAMIC_RGSA = 'islamic-rgsa'; + public const ISLAMIC_TBLA = 'islamic-tbla'; + public const JAPANESE = 'japanese'; + public const PERSIAN = 'persian'; - public static function isValidCalendarName(string $calendarname): bool - { - return in_array($calendarname, [ - self::BUDDHIST, - self::CHINESE, - self::COPTIC, - self::ETHIOPIAN, - self::GREGORIAN, - self::HEBREW, - self::INDIAN, - self::ISLAMIC, - self::ISLAMIC_CIVIL, - self::ISLAMIC_RGSA, - self::ISLAMIC_TBLA, - self::ISLAMIC_UMALQURA, - self::JAPANESE, - self::PERSIAN, - ]); + public static function isValidCalendarName(string $calendarname): bool + { + return in_array($calendarname, [ + self::BUDDHIST, + self::CHINESE, + self::COPTIC, + self::ETHIOPIAN, + self::GREGORIAN, + self::HEBREW, + self::INDIAN, + self::ISLAMIC, + self::ISLAMIC_CIVIL, + self::ISLAMIC_RGSA, + self::ISLAMIC_TBLA, + self::ISLAMIC_UMALQURA, + self::JAPANESE, + self::PERSIAN, + ]); } } diff --git a/src/CalendarDay.php b/src/CalendarDay.php index 5a5ee64..7f9c5df 100644 --- a/src/CalendarDay.php +++ b/src/CalendarDay.php @@ -15,81 +15,65 @@ class CalendarDay { - /** @var int */ - private $day; - - /** @var int */ - private $month; - - /** @var int|null */ - private $year = null; - - /** @var IntlCalendar */ - private $calendar; - - public function __construct(int $day, int $month, IntlCalendar $calendar) - { - $this->day = $day; - $this->month = $month; - $this->calendar = $calendar; - $this->calendar->set(IntlCalendar::FIELD_DAY_OF_MONTH, $day); - $this->calendar->set(IntlCalendar::FIELD_MONTH, $month - 1); - $this->calendar->set(IntlCalendar::FIELD_HOUR_OF_DAY, 12); - $this->calendar->set(IntlCalendar::FIELD_MINUTE, 0); - $this->calendar->set(IntlCalendar::FIELD_SECOND, 0); - $this->calendar->set(IntlCalendar::FIELD_MILLISECOND, 0); - } - - public function setYear(int $year): void - { - $this->year = $year; - $this->calendar->set(IntlCalendar::FIELD_YEAR, $year); - } - - public function setGregorianYear(int $year): void - { - $calendarYear = (int) $this->calendar->toDateTime()->format('Y'); - - $diff = $year - $calendarYear; - $realYear = $this->calendar->get(IntlCalendar::FIELD_YEAR); - - $this->year = $realYear + $diff; - $this->calendar->add(IntlCalendar::FIELD_YEAR, $diff); - } - - public static function setGregorianYearForDate(int $year, IntlCalendar $calendar): IntlCalendar - { - $datetime = $calendar->toDateTime(); - $yearDiff = $year - (int) $datetime->format('Y'); - - $calendar->set(IntlCalendar::FIELD_YEAR, $calendar->get(IntlCalendar::FIELD_YEAR) + $yearDiff); - if ($calendar->toDateTime()->format('Y') < $year) { - $calendar->set(IntlCalendar::FIELD_YEAR, $calendar->get(IntlCalendar::FIELD_YEAR) + 1); - } - if ($calendar->toDateTime()->format('Y') > $year) { - $calendar->set(IntlCalendar::FIELD_YEAR, $calendar->get(IntlCalendar::FIELD_YEAR) - 1); - } - - return $calendar; - } - - public function isSameDay(DateTimeInterface $dateTime): bool - { - $cal = clone $this->calendar; - $cal->setTime($dateTime->getTimestamp() * 1000); - - if (null !== $this->year && - $cal->get(IntlCalendar::FIELD_YEAR) !== $this->calendar->get(IntlCalendar::FIELD_YEAR) - ) { - return false; - } - - if ($cal->get(IntlCalendar::FIELD_MONTH) !== $this->calendar->get(IntlCalendar::FIELD_MONTH)) { - return false; - } - - return $cal->get(IntlCalendar::FIELD_DAY_OF_MONTH) === $this->calendar->get(IntlCalendar::FIELD_DAY_OF_MONTH); - } + /** @var int */ + private $day; + + /** @var int */ + private $month; + + /** @var int|null */ + private $year = null; + + /** @var IntlCalendar */ + private $calendar; + + public function __construct(int $day, int $month, IntlCalendar $calendar) + { + $this->day = $day; + $this->month = $month; + $this->calendar = $calendar; + $this->calendar->set(IntlCalendar::FIELD_DAY_OF_MONTH, $day); + $this->calendar->set(IntlCalendar::FIELD_MONTH, $month - 1); + $this->calendar->set(IntlCalendar::FIELD_HOUR_OF_DAY, 12); + $this->calendar->set(IntlCalendar::FIELD_MINUTE, 0); + $this->calendar->set(IntlCalendar::FIELD_SECOND, 0); + $this->calendar->set(IntlCalendar::FIELD_MILLISECOND, 0); + } + + public function setYear(int $year): void + { + $this->year = $year; + $this->calendar->set(IntlCalendar::FIELD_YEAR, $year); + } + + public function setGregorianYear(int $year): void + { + $calendarYear = (int) $this->calendar->toDateTime()->format('Y'); + + $diff = $year - $calendarYear; + $realYear = $this->calendar->get(IntlCalendar::FIELD_YEAR); + + $this->year = $realYear + $diff; + $this->calendar->add(IntlCalendar::FIELD_YEAR, $diff); + } + + public function isSameDay(DateTimeInterface $dateTime): bool + { + $cal = clone $this->calendar; + $cal->setTime($dateTime->getTimestamp() * 1000); + + if (null !== $this->year && + $cal->get(IntlCalendar::FIELD_YEAR) !== $this->calendar->get(IntlCalendar::FIELD_YEAR) + ) { + return false; + } + + if ($cal->get(IntlCalendar::FIELD_MONTH) !== $this->calendar->get(IntlCalendar::FIELD_MONTH)) { + return false; + } + + return $cal->get(IntlCalendar::FIELD_DAY_OF_MONTH) === $this->calendar->get(IntlCalendar::FIELD_DAY_OF_MONTH); + } public function getCalendar(): IntlCalendar { @@ -101,10 +85,10 @@ public function hasYearSet(): bool return null !== $this->year; } - public function isFollowUpDay(DateTimeInterface $dateTime, string $followUpDay): bool - { - return $this->isModifiedDate($dateTime, $followUpDay, 'next'); - } + public function isFollowUpDay(DateTimeInterface $dateTime, string $followUpDay): bool + { + return $this->isModifiedDate($dateTime, $followUpDay, 'next'); + } private function isModifiedDate(DateTimeInterface $dateTime, string $modifiedDay, string $direction): bool { @@ -113,7 +97,7 @@ private function isModifiedDate(DateTimeInterface $dateTime, string $modifiedDay $day = $cal->toDateTime(); $day->modify($direction . ' ' . $modifiedDay); $cal->setTime($day->getTimestamp() * 1000); - $cal2 = clone $this->calendar; + $cal2 = clone $this->calendar; $cal2->setTime($dateTime->getTimestamp() * 1000); if (null !== $this->year && $cal->get(IntlCalendar::FIELD_YEAR) !== $cal2->get(IntlCalendar::FIELD_YEAR)) { @@ -127,19 +111,35 @@ private function isModifiedDate(DateTimeInterface $dateTime, string $modifiedDay return $cal->get(IntlCalendar::FIELD_DAY_OF_MONTH) === $cal2->get(IntlCalendar::FIELD_DAY_OF_MONTH); } - public function getWeekdayForGregorianYear(int $year): int - { - $cal = $this->getDayForGregorianYear($year); + public static function setGregorianYearForDate(int $year, IntlCalendar $calendar): IntlCalendar + { + $datetime = $calendar->toDateTime(); + $yearDiff = $year - (int) $datetime->format('Y'); - return $cal->get(IntlCalendar::FIELD_DAY_OF_WEEK); - } + $calendar->set(IntlCalendar::FIELD_YEAR, $calendar->get(IntlCalendar::FIELD_YEAR) + $yearDiff); + if ($calendar->toDateTime()->format('Y') < $year) { + $calendar->set(IntlCalendar::FIELD_YEAR, $calendar->get(IntlCalendar::FIELD_YEAR) + 1); + } + if ($calendar->toDateTime()->format('Y') > $year) { + $calendar->set(IntlCalendar::FIELD_YEAR, $calendar->get(IntlCalendar::FIELD_YEAR) - 1); + } - private function getDayForGregorianYear(int $gregorianYear): IntlCalendar - { - $cal = clone $this->calendar; - $cal->set(IntlCalendar::FIELD_MONTH, $this->month - 1); - $cal->set(IntlCalendar::FIELD_DAY_OF_MONTH, $this->day); + return $calendar; + } + + public function getWeekdayForGregorianYear(int $year): int + { + $cal = $this->getDayForGregorianYear($year); + + return $cal->get(IntlCalendar::FIELD_DAY_OF_WEEK); + } - return self::setGregorianYearForDate($gregorianYear, $cal); - } + private function getDayForGregorianYear(int $gregorianYear): IntlCalendar + { + $cal = clone $this->calendar; + $cal->set(IntlCalendar::FIELD_MONTH, $this->month - 1); + $cal->set(IntlCalendar::FIELD_DAY_OF_MONTH, $this->day); + + return self::setGregorianYearForDate($gregorianYear, $cal); + } } diff --git a/src/CalendarDayFactory.php b/src/CalendarDayFactory.php index 12dff39..6282248 100644 --- a/src/CalendarDayFactory.php +++ b/src/CalendarDayFactory.php @@ -39,19 +39,19 @@ class CalendarDayFactory { - public static function createCalendarDay(int $day, int $month, string $calendar): CalendarDay - { - if (! Calendar::isValidCalendarName($calendar)) { - throw new UnknownCalendar(sprintf( - 'The calendar %s is not known to the ICU', - $calendar - )); - } - $calendar = IntlCalendar::createInstance('UTC', '@calendar=' . $calendar); + public static function createCalendarDay(int $day, int $month, string $calendar): CalendarDay + { + if (!Calendar::isValidCalendarName($calendar)) { + throw new UnknownCalendar(sprintf( + 'The calendar %s is not known to the ICU', + $calendar + )); + } + $calendar = IntlCalendar::createInstance('UTC', '@calendar=' . $calendar); if (null === $calendar) { throw new UnexpectedValueException('Expected instance of IntlCalendar, got null'); } - return new CalendarDay($day, $month, $calendar); - } + return new CalendarDay($day, $month, $calendar); + } } diff --git a/src/Factory/DateFactory.php b/src/Factory/DateFactory.php index 8438d6c..ab76c44 100644 --- a/src/Factory/DateFactory.php +++ b/src/Factory/DateFactory.php @@ -35,7 +35,7 @@ public function itemFromDomElement(DOMElement $element): ?HolidayIteratorItemInt $date = new Date( $element->textContent, - $element->getAttribute('free') === "true", + $element->getAttribute('free') === "true", $day, ); diff --git a/src/Factory/EasterOrthodoxFactory.php b/src/Factory/EasterOrthodoxFactory.php index b45d1df..fcef727 100644 --- a/src/Factory/EasterOrthodoxFactory.php +++ b/src/Factory/EasterOrthodoxFactory.php @@ -24,7 +24,7 @@ public function itemFromDomElement(DOMElement $element): ?HolidayIteratorItemInt return new EasterOrthodox( $element->textContent, - $element->getAttribute('free') === "true", + $element->getAttribute('free') === "true", (int) $element->getAttribute('offset') ); } diff --git a/src/Factory/ObservanceDecoratorFactory.php b/src/Factory/ObservanceDecoratorFactory.php index 6d6e431..dd8260e 100644 --- a/src/Factory/ObservanceDecoratorFactory.php +++ b/src/Factory/ObservanceDecoratorFactory.php @@ -18,7 +18,7 @@ class ObservanceDecoratorFactory implements DecorateFromDomElement { public function decorate(HolidayIteratorItemInterface $element, DOMElement $domElement): HolidayIteratorItemInterface { - if (! $domElement->hasAttribute('firstobservance') && ! $domElement->hasAttribute('lastobservance')) { + if (!$domElement->hasAttribute('firstobservance') && !$domElement->hasAttribute('lastobservance')) { return $element; } diff --git a/src/Factory/SwapDecoratorFactory.php b/src/Factory/SwapDecoratorFactory.php index 05dfc86..1e3efe6 100644 --- a/src/Factory/SwapDecoratorFactory.php +++ b/src/Factory/SwapDecoratorFactory.php @@ -43,17 +43,6 @@ public function decorate(HolidayIteratorItemInterface $element, DOMElement $domE return new SwapDecorator($element, $day, ...$rules); } - private function createRuleFrom(string $to, string $when, SwapDirection $direction): SwapRule - { - return new SwapRule( - $direction, - GregorianWeekday::fromString($to), - ...array_map(function ($item) { - return GregorianWeekday::fromString($item); - }, explode(' ', $when)) - ); - } - /** * @return SwapRule[] */ @@ -75,4 +64,15 @@ private function getRulesFromDomElement(DOMElement $domElement): array return $rules; } + + private function createRuleFrom(string $to, string $when, SwapDirection $direction): SwapRule + { + return new SwapRule( + $direction, + GregorianWeekday::fromString($to), + ...array_map(function ($item) { + return GregorianWeekday::fromString($item); + }, explode(' ', $when)) + ); + } } diff --git a/src/GregorianWeekday.php b/src/GregorianWeekday.php index a2c0365..25bcab4 100644 --- a/src/GregorianWeekday.php +++ b/src/GregorianWeekday.php @@ -20,12 +20,6 @@ final class GregorianWeekday { - /** @var string */ - private $value; - - /** @var array */ - private static $instances = []; - private const MONDAY = 'monday'; private const TUESDAY = 'tuesday'; private const WEDNESDAY = 'wednesday'; @@ -33,20 +27,19 @@ final class GregorianWeekday private const FRIDAY = 'friday'; private const SATURDAY = 'saturday'; private const SUNDAY = 'sunday'; + /** @var array */ + private static $instances = []; + /** @var string */ + private $value; private function __construct(string $value) - { - $this->value = $value; - } - - public function getValue(): string { - return $this->value; + $this->value = $value; } public static function monday(): self { - if (! isset(self::$instances[self::MONDAY])) { + if (!isset(self::$instances[self::MONDAY])) { self::$instances[self::MONDAY] = new self(self::MONDAY); } @@ -55,7 +48,7 @@ public static function monday(): self public static function tuesday(): self { - if (! isset(self::$instances[self::TUESDAY])) { + if (!isset(self::$instances[self::TUESDAY])) { self::$instances[self::TUESDAY] = new self(self::TUESDAY); } @@ -64,7 +57,7 @@ public static function tuesday(): self public static function wednesday(): self { - if (! isset(self::$instances[self::WEDNESDAY])) { + if (!isset(self::$instances[self::WEDNESDAY])) { self::$instances[self::WEDNESDAY] = new self(self::WEDNESDAY); } @@ -73,7 +66,7 @@ public static function wednesday(): self public static function thursday(): self { - if (! isset(self::$instances[self::THURSDAY])) { + if (!isset(self::$instances[self::THURSDAY])) { self::$instances[self::THURSDAY] = new self(self::THURSDAY); } @@ -82,7 +75,7 @@ public static function thursday(): self public static function friday(): self { - if (! isset(self::$instances[self::FRIDAY])) { + if (!isset(self::$instances[self::FRIDAY])) { self::$instances[self::FRIDAY] = new self(self::FRIDAY); } @@ -91,7 +84,7 @@ public static function friday(): self public static function saturday(): self { - if (! isset(self::$instances[self::SATURDAY])) { + if (!isset(self::$instances[self::SATURDAY])) { self::$instances[self::SATURDAY] = new self(self::SATURDAY); } @@ -100,16 +93,21 @@ public static function saturday(): self public static function sunday(): self { - if (! isset(self::$instances[self::SUNDAY])) { + if (!isset(self::$instances[self::SUNDAY])) { self::$instances[self::SUNDAY] = new self(self::SUNDAY); } return self::$instances[self::SUNDAY]; } + public static function fromDateTimeInterface(DateTimeInterface $date): self + { + return self::fromString($date->format('l')); + } + public static function fromString(string $weekday): self { - if (! method_exists(self::class, strtolower($weekday))) { + if (!method_exists(self::class, strtolower($weekday))) { throw new RuntimeException(sprintf( 'Weekday "%s" is not known', $weekday @@ -122,23 +120,18 @@ public static function fromString(string $weekday): self return $gregorianWeekday; } - public static function fromDateTimeInterface(DateTimeInterface $date): self - { - return self::fromString($date->format('l')); - } - public static function fromIntlWeekday(int $weekday): self { $mapper = [ - IntlCalendar::DOW_SUNDAY => 'sunday', - IntlCalendar::DOW_MONDAY => 'monday', - IntlCalendar::DOW_TUESDAY => 'tuesday', + IntlCalendar::DOW_SUNDAY => 'sunday', + IntlCalendar::DOW_MONDAY => 'monday', + IntlCalendar::DOW_TUESDAY => 'tuesday', IntlCalendar::DOW_WEDNESDAY => 'wednesday', - IntlCalendar::DOW_THURSDAY => 'thursday', - IntlCalendar::DOW_FRIDAY => 'friday', - IntlCalendar::DOW_SATURDAY => 'saturday', + IntlCalendar::DOW_THURSDAY => 'thursday', + IntlCalendar::DOW_FRIDAY => 'friday', + IntlCalendar::DOW_SATURDAY => 'saturday', ]; - if (! isset($mapper[$weekday])) { + if (!isset($mapper[$weekday])) { throw new UnexpectedValueException(sprintf( 'IntlCalendar weekday %s could not be resolved', $weekday @@ -152,4 +145,9 @@ public function __toString(): string { return $this->getValue(); } + + public function getValue(): string + { + return $this->value; + } } diff --git a/src/Holiday.php b/src/Holiday.php index 40dd506..c722e41 100644 --- a/src/Holiday.php +++ b/src/Holiday.php @@ -34,28 +34,28 @@ class Holiday { - private $holiday; - - private $name; - - public function __construct(bool $holiday, string $name = '') - { - $this->holiday = $holiday; - $this->name = $name; - } - - public function isHoliday(): bool - { - return $this->holiday; - } - - public function isNamed(): bool - { - return $this->name !== ''; - } - - public function getName(): string - { - return $this->name; - } + private $holiday; + + private $name; + + public function __construct(bool $holiday, string $name = '') + { + $this->holiday = $holiday; + $this->name = $name; + } + + public function isHoliday(): bool + { + return $this->holiday; + } + + public function isNamed(): bool + { + return $this->name !== ''; + } + + public function getName(): string + { + return $this->name; + } } diff --git a/src/HolidayIterator.php b/src/HolidayIterator.php index 630cc0e..f2968a4 100644 --- a/src/HolidayIterator.php +++ b/src/HolidayIterator.php @@ -33,23 +33,24 @@ namespace Org_Heigl\Holidaychecker; use ArrayObject; +use ReturnTypeWillChange; /** * @template-extends ArrayObject */ class HolidayIterator extends ArrayObject { - /** - * @param mixed $value - * @return void - */ - #[\ReturnTypeWillChange] - public function append($value) - { - if (! $value instanceof HolidayIteratorItemInterface) { - return; - } + /** + * @param mixed $value + * @return void + */ + #[ReturnTypeWillChange] + public function append($value) + { + if (!$value instanceof HolidayIteratorItemInterface) { + return; + } - parent::append($value); - } + parent::append($value); + } } diff --git a/src/HolidayIteratorFactory.php b/src/HolidayIteratorFactory.php index 31e1afa..b7777ed 100644 --- a/src/HolidayIteratorFactory.php +++ b/src/HolidayIteratorFactory.php @@ -74,6 +74,28 @@ public function __construct() ]; } + /** + * Create a HolidayIterator from an ISO 3166-code. + * + * @param string $isoCode + * + * @return HolidayIterator + */ + public function createIteratorFromISO3166(string $isoCode): HolidayIterator + { + $file = __DIR__ . '/../share/%s.xml'; + $file1 = sprintf($file, $isoCode); + + if (!is_readable($file1)) { + throw new UnexpectedValueException(sprintf( + 'There is no holiday-file for %s', + $isoCode + )); + } + + return $this->createIteratorFromXmlFile($file1); + } + /** * Create a HolidayIterator from an XML-File * @@ -115,28 +137,6 @@ public function createIteratorFromXmlFile(string $file): HolidayIterator return $iterator; } - /** - * Create a HolidayIterator from an ISO 3166-code. - * - * @param string $isoCode - * - * @return HolidayIterator - */ - public function createIteratorFromISO3166(string $isoCode): HolidayIterator - { - $file = __DIR__ . '/../share/%s.xml'; - $file1 = sprintf($file, $isoCode); - - if (!is_readable($file1)) { - throw new UnexpectedValueException(sprintf( - 'There is no holiday-file for %s', - $isoCode - )); - } - - return $this->createIteratorFromXmlFile($file1); - } - private function getElement(DOMElement $child): HolidayIteratorItemInterface { foreach ($this->factories as $factory) { diff --git a/src/HolidayIteratorItemInterface.php b/src/HolidayIteratorItemInterface.php index de39a9b..268d5cc 100644 --- a/src/HolidayIteratorItemInterface.php +++ b/src/HolidayIteratorItemInterface.php @@ -36,9 +36,9 @@ interface HolidayIteratorItemInterface { - public function dateMatches(DateTimeInterface $date): bool; + public function dateMatches(DateTimeInterface $date): bool; - public function getName(): string; + public function getName(): string; - public function isHoliday(): bool; + public function isHoliday(): bool; } diff --git a/src/Holidaychecker.php b/src/Holidaychecker.php index 9bcd145..a0409aa 100644 --- a/src/Holidaychecker.php +++ b/src/Holidaychecker.php @@ -36,21 +36,21 @@ class Holidaychecker { - private $iterator; - - public function __construct(HolidayIterator $iterator) - { - $this->iterator = $iterator; - } - - public function check(DateTimeInterface $date): Holiday - { - foreach ($this->iterator as $entry) { - if ($entry->dateMatches($date)) { - return new Holiday($entry->isHoliday(), $entry->getName()); - } - } - - return new Holiday(false); - } + private $iterator; + + public function __construct(HolidayIterator $iterator) + { + $this->iterator = $iterator; + } + + public function check(DateTimeInterface $date): Holiday + { + foreach ($this->iterator as $entry) { + if ($entry->dateMatches($date)) { + return new Holiday($entry->isHoliday(), $entry->getName()); + } + } + + return new Holiday(false); + } } diff --git a/src/IteratorItem/Date.php b/src/IteratorItem/Date.php index da41826..3b8a443 100644 --- a/src/IteratorItem/Date.php +++ b/src/IteratorItem/Date.php @@ -38,34 +38,34 @@ class Date implements HolidayIteratorItemInterface { - /** @var CalendarDay */ - private $calendarDay; + /** @var CalendarDay */ + private $calendarDay; - /** @var bool */ - private $holiday; + /** @var bool */ + private $holiday; - /** @var string */ - private $name; + /** @var string */ + private $name; - public function __construct(string $name, bool $holiday, CalendarDay $day) - { - $this->calendarDay = $day; - $this->holiday = $holiday; - $this->name = $name; - } + public function __construct(string $name, bool $holiday, CalendarDay $day) + { + $this->calendarDay = $day; + $this->holiday = $holiday; + $this->name = $name; + } - public function dateMatches(DateTimeInterface $date): bool - { - return $this->calendarDay->isSameDay($date); - } + public function dateMatches(DateTimeInterface $date): bool + { + return $this->calendarDay->isSameDay($date); + } - public function getName(): string - { - return $this->name; - } + public function getName(): string + { + return $this->name; + } - public function isHoliday(): bool - { - return $this->holiday; - } + public function isHoliday(): bool + { + return $this->holiday; + } } diff --git a/src/IteratorItem/DateFollowUp.php b/src/IteratorItem/DateFollowUp.php index 23cfe01..2cef0bc 100644 --- a/src/IteratorItem/DateFollowUp.php +++ b/src/IteratorItem/DateFollowUp.php @@ -41,76 +41,76 @@ class DateFollowUp implements HolidayIteratorItemInterface { - /** @var CalendarDay */ - private $day; - - /** @var bool */ - private $holiday; - - /** @var string */ - private $name; - - /** @var string */ - private $followup; - - /** @var array */ - private $replaced; - - public function __construct(string $name, bool $holiday, CalendarDay $day, string $followup, array $replaced = []) - { - $this->day = $day; - $this->followup = $followup; - $this->holiday = $holiday; - $this->name = $name; - $this->replaced = $this->replacedDays($replaced); - } - - public function dateMatches(DateTimeInterface $date): bool - { - $gregorianYear = (int) $date->format('Y'); - $weekday = $this->day->getWeekdayForGregorianYear($gregorianYear); - - if (in_array($weekday, $this->replaced)) { - return $this->day->isFollowUpDay($date, $this->followup); - } - - return $this->day->isSameDay($date); - } - - public function getName(): string - { - return $this->name; - } - - public function isHoliday(): bool - { - return $this->holiday; - } - - private static function replacedDays(array $replaced): array - { - $daymap = [ - 'sunday' => IntlCalendar::DOW_SUNDAY, - 'monday' => IntlCalendar::DOW_MONDAY, - 'tuesday' => IntlCalendar::DOW_TUESDAY, - 'wednesday' => IntlCalendar::DOW_WEDNESDAY, - 'thursday' => IntlCalendar::DOW_THURSDAY, - 'friday' => IntlCalendar::DOW_FRIDAY, - 'saturday' => IntlCalendar::DOW_SATURDAY, - ]; - - if ([] === $replaced) { - return [ - IntlCalendar::DOW_SATURDAY, - IntlCalendar::DOW_SUNDAY, - ]; - } - - return array_map(function (string $day) use ($daymap) { - if (! isset($daymap[$day])) { - return null; - } - return $daymap[$day]; - }, $replaced); - } + /** @var CalendarDay */ + private $day; + + /** @var bool */ + private $holiday; + + /** @var string */ + private $name; + + /** @var string */ + private $followup; + + /** @var array */ + private $replaced; + + public function __construct(string $name, bool $holiday, CalendarDay $day, string $followup, array $replaced = []) + { + $this->day = $day; + $this->followup = $followup; + $this->holiday = $holiday; + $this->name = $name; + $this->replaced = $this->replacedDays($replaced); + } + + private static function replacedDays(array $replaced): array + { + $daymap = [ + 'sunday' => IntlCalendar::DOW_SUNDAY, + 'monday' => IntlCalendar::DOW_MONDAY, + 'tuesday' => IntlCalendar::DOW_TUESDAY, + 'wednesday' => IntlCalendar::DOW_WEDNESDAY, + 'thursday' => IntlCalendar::DOW_THURSDAY, + 'friday' => IntlCalendar::DOW_FRIDAY, + 'saturday' => IntlCalendar::DOW_SATURDAY, + ]; + + if ([] === $replaced) { + return [ + IntlCalendar::DOW_SATURDAY, + IntlCalendar::DOW_SUNDAY, + ]; + } + + return array_map(function (string $day) use ($daymap) { + if (!isset($daymap[$day])) { + return null; + } + return $daymap[$day]; + }, $replaced); + } + + public function dateMatches(DateTimeInterface $date): bool + { + $gregorianYear = (int) $date->format('Y'); + $weekday = $this->day->getWeekdayForGregorianYear($gregorianYear); + + if (in_array($weekday, $this->replaced)) { + return $this->day->isFollowUpDay($date, $this->followup); + } + + return $this->day->isSameDay($date); + } + + public function getName(): string + { + return $this->name; + } + + public function isHoliday(): bool + { + return $this->holiday; + } } diff --git a/src/IteratorItem/Easter.php b/src/IteratorItem/Easter.php index 7bee436..df38c36 100644 --- a/src/IteratorItem/Easter.php +++ b/src/IteratorItem/Easter.php @@ -43,47 +43,37 @@ class Easter implements HolidayIteratorItemInterface { - private $offset; + private $offset; - private $holiday; + private $holiday; - private $name; + private $name; - public function __construct(string $name, bool $holiday, int $offset) - { - $this->offset = $offset; - $this->holiday = $holiday; - $this->name = $name; - } + public function __construct(string $name, bool $holiday, int $offset) + { + $this->offset = $offset; + $this->holiday = $holiday; + $this->name = $name; + } - public function dateMatches(DateTimeInterface $date): bool - { - $year = (int) $date->format('Y'); + public function dateMatches(DateTimeInterface $date): bool + { + $year = (int) $date->format('Y'); $easter = $this->getEaster($year); $day = $this->getOffsetDay($easter, $this->offset); - $comparator = new DateIntervalComparator(); - return 0 > $comparator->compare($day->diff($date), new DateInterval('P1D')); - } - - public function getName(): string - { - return $this->name; - } - - public function isHoliday(): bool - { - return $this->holiday; - } + $comparator = new DateIntervalComparator(); + return 0 > $comparator->compare($day->diff($date), new DateInterval('P1D')); + } - protected function getEaster(int $year): DateTimeImmutable - { - $base = new DateTimeImmutable($year . "-03-21", new DateTimeZone('UTC')); - $days = easter_days($year); + protected function getEaster(int $year): DateTimeImmutable + { + $base = new DateTimeImmutable($year . "-03-21", new DateTimeZone('UTC')); + $days = easter_days($year); - return $base->add(new DateInterval("P{$days}D")); - } + return $base->add(new DateInterval("P{$days}D")); + } /** * @param DateTime|DateTimeImmutable $date @@ -97,4 +87,14 @@ private function getOffsetDay($date, int $offset) return $date->add(new DateInterval('P' . $offset . 'D')); } + + public function getName(): string + { + return $this->name; + } + + public function isHoliday(): bool + { + return $this->holiday; + } } diff --git a/src/IteratorItem/EasterOrthodox.php b/src/IteratorItem/EasterOrthodox.php index dd65103..0f68f40 100644 --- a/src/IteratorItem/EasterOrthodox.php +++ b/src/IteratorItem/EasterOrthodox.php @@ -40,41 +40,41 @@ class EasterOrthodox extends Easter { - /** - * @param int $year - * - * @see http://www.smart.net/~mmontes/ortheast.html - * @return DateTimeImmutable - */ - private function getOrthodoxEaster(int $year): DateTimeImmutable - { - $r1 = $year % 19; - $r2 = $year % 4; - $r3 = $year % 7; - $rA = 19 * $r1 + 16; - $r4 = $rA % 30; - $rB = 2 * $r2 + 4 * $r3 + 6 * $r4; - $r5 = $rB % 7; - $rC = $r4 + $r5; + protected function getEaster(int $year): DateTimeImmutable + { + $jewishYear = 3760 + $year; + $endOfPessach = new DateTimeImmutable( + '@' . jdtounix(jewishtojd(1, 20, $jewishYear)) + ); + $orthodoxEaster = $this->getOrthodoxEaster($year); + if ($endOfPessach > $orthodoxEaster) { + $weekday = (int) $endOfPessach->format('w'); + return $endOfPessach->add(new DateInterval('P' . (7 - $weekday) . 'D')); + } - // Don't touch this. It just seems to work… - // And doing the "same" in DateTime (adding a period of $rC days doesn't - // yield the same result… - return new DateTimeImmutable('@' . jdtounix(juliantojd(3, 21, $year) + $rC)); - } + return $orthodoxEaster; + } - protected function getEaster(int $year): DateTimeImmutable - { - $jewishYear = 3760 + $year; - $endOfPessach = new DateTimeImmutable( - '@' . jdtounix(jewishtojd(1, 20, $jewishYear)) - ); - $orthodoxEaster = $this->getOrthodoxEaster($year); - if ($endOfPessach > $orthodoxEaster) { - $weekday = (int) $endOfPessach->format('w'); - return $endOfPessach->add(new DateInterval('P' . (7-$weekday) . 'D')); - } + /** + * @param int $year + * + * @return DateTimeImmutable + * @see http://www.smart.net/~mmontes/ortheast.html + */ + private function getOrthodoxEaster(int $year): DateTimeImmutable + { + $r1 = $year % 19; + $r2 = $year % 4; + $r3 = $year % 7; + $rA = 19 * $r1 + 16; + $r4 = $rA % 30; + $rB = 2 * $r2 + 4 * $r3 + 6 * $r4; + $r5 = $rB % 7; + $rC = $r4 + $r5; - return $orthodoxEaster; - } + // Don't touch this. It just seems to work… + // And doing the "same" in DateTime (adding a period of $rC days doesn't + // yield the same result… + return new DateTimeImmutable('@' . jdtounix(juliantojd(3, 21, $year) + $rC)); + } } diff --git a/src/IteratorItem/Relative.php b/src/IteratorItem/Relative.php index 9912e2e..6366ad5 100644 --- a/src/IteratorItem/Relative.php +++ b/src/IteratorItem/Relative.php @@ -39,53 +39,53 @@ class Relative implements HolidayIteratorItemInterface { - private $day; + private $day; - private $month; + private $month; - private $relation; + private $relation; - private $holiday; + private $holiday; - private $name; + private $name; - public function __construct(string $name, bool $holiday, int $day, int $month, string $relation) - { - $this->day = $day; - $this->month = $month; - $this->relation = $relation; - $this->holiday = $holiday; - $this->name = $name; - } + public function __construct(string $name, bool $holiday, int $day, int $month, string $relation) + { + $this->day = $day; + $this->month = $month; + $this->relation = $relation; + $this->holiday = $holiday; + $this->name = $name; + } - public function dateMatches(DateTimeInterface $date): bool - { - $year = (int) $date->format('Y'); + public function dateMatches(DateTimeInterface $date): bool + { + $year = (int) $date->format('Y'); - $day = new DateTimeImmutable(sprintf( - '%s-%s-%s', - $year, - $this->month, - $this->day - )); + $day = new DateTimeImmutable(sprintf( + '%s-%s-%s', + $year, + $this->month, + $this->day + )); /** @var DateTimeImmutable|false $day */ - $day = $day->modify($this->relation); + $day = $day->modify($this->relation); if ($day === false) { return false; } - return $date->format('Y-m-d') === $day->format('Y-m-d'); - } + return $date->format('Y-m-d') === $day->format('Y-m-d'); + } - public function getName(): string - { - return $this->name; - } + public function getName(): string + { + return $this->name; + } - public function isHoliday(): bool - { - return $this->holiday; - } + public function isHoliday(): bool + { + return $this->holiday; + } } diff --git a/src/IteratorItem/SwapDecorator.php b/src/IteratorItem/SwapDecorator.php index ca5428a..7e0ec80 100644 --- a/src/IteratorItem/SwapDecorator.php +++ b/src/IteratorItem/SwapDecorator.php @@ -21,13 +21,13 @@ class SwapDecorator implements HolidayIteratorItemInterface { - /** @var HolidayIteratorItemInterface */ + /** @var HolidayIteratorItemInterface */ private $rule; - /** @var CalendarDay */ + /** @var CalendarDay */ private $day; - /** @var SwapRule[] */ + /** @var SwapRule[] */ private $swapRules; public function __construct(HolidayIteratorItemInterface $rule, CalendarDay $day, SwapRule ...$swapRules) @@ -50,16 +50,6 @@ public function dateMatches(DateTimeInterface $date): bool return $this->rule->dateMatches($date); } - public function getName(): string - { - return $this->rule->getName(); - } - - public function isHoliday(): bool - { - return $this->rule->isHoliday(); - } - private function ruleMatches(SwapRule $rule, GregorianWeekday $weekday): bool { return in_array($weekday, $rule->getSwapWhenDays(), true); @@ -85,4 +75,14 @@ private function isModifiedDate(DateTimeInterface $dateTime, GregorianWeekday $m return $cal->get(IntlCalendar::FIELD_DAY_OF_MONTH) === $cal2->get(IntlCalendar::FIELD_DAY_OF_MONTH); } + + public function getName(): string + { + return $this->rule->getName(); + } + + public function isHoliday(): bool + { + return $this->rule->isHoliday(); + } } diff --git a/src/ObservanceDecorator.php b/src/ObservanceDecorator.php index 77f8332..4bca75d 100644 --- a/src/ObservanceDecorator.php +++ b/src/ObservanceDecorator.php @@ -14,40 +14,40 @@ final class ObservanceDecorator implements HolidayIteratorItemInterface { - /** @var int|null */ - private $firstObservance; + /** @var int|null */ + private $firstObservance; - /** @var int|null */ - private $lastObservance; + /** @var int|null */ + private $lastObservance; /** @var HolidayIteratorItemInterface */ private $wrapped; public function __construct(HolidayIteratorItemInterface $wrapped, ?int $firstObservance, ?int $lastObservance) - { + { $this->wrapped = $wrapped; - $this->firstObservance = $firstObservance; - $this->lastObservance = $lastObservance; - } - - private function isWithinObservance(int $gregorianYear): bool - { - if (null !== $this->firstObservance && $this->firstObservance > $gregorianYear) { - return false; - } - - return null === $this->lastObservance || $this->lastObservance >= $gregorianYear; - } + $this->firstObservance = $firstObservance; + $this->lastObservance = $lastObservance; + } public function dateMatches(DateTimeInterface $date): bool { - if (! $this->isWithinObservance((int) $date->format('Y'))) { + if (!$this->isWithinObservance((int) $date->format('Y'))) { return false; } return $this->wrapped->dateMatches($date); } + private function isWithinObservance(int $gregorianYear): bool + { + if (null !== $this->firstObservance && $this->firstObservance > $gregorianYear) { + return false; + } + + return null === $this->lastObservance || $this->lastObservance >= $gregorianYear; + } + public function getName(): string { return $this->wrapped->getName(); diff --git a/src/SwapDirection.php b/src/SwapDirection.php index 69a0fba..7f95b41 100644 --- a/src/SwapDirection.php +++ b/src/SwapDirection.php @@ -12,20 +12,36 @@ final class SwapDirection { - /** @var string */ - private $value; - - /** @var array */ - private static $instances = []; - private const FORWARD = 'forward'; private const REWIND = 'rewind'; + /** @var array */ + private static $instances = []; + /** @var string */ + private $value; private function __construct(string $value) - { + { $this->value = $value; } + public static function forward(): self + { + if (!isset(self::$instances[self::FORWARD])) { + self::$instances[self::FORWARD] = new self(self::FORWARD); + } + + return self::$instances[self::FORWARD]; + } + + public static function rewind(): self + { + if (!isset(self::$instances[self::REWIND])) { + self::$instances[self::REWIND] = new self(self::REWIND); + } + + return self::$instances[self::REWIND]; + } + public function getValue(): string { return $this->value; @@ -42,22 +58,4 @@ public function getDateTimeDirection(): string return ''; } - - public static function forward(): self - { - if (! isset(self::$instances[self::FORWARD])) { - self::$instances[self::FORWARD] = new self(self::FORWARD); - } - - return self::$instances[self::FORWARD]; - } - - public static function rewind(): self - { - if (! isset(self::$instances[self::REWIND])) { - self::$instances[self::REWIND] = new self(self::REWIND); - } - - return self::$instances[self::REWIND]; - } }