@@ -38,34 +38,34 @@ |
||
38 | 38 | |
39 | 39 | class Date implements HolidayIteratorItemInterface |
40 | 40 | { |
41 | - /** @var CalendarDay */ |
|
42 | - private $calendarDay; |
|
41 | + /** @var CalendarDay */ |
|
42 | + private $calendarDay; |
|
43 | 43 | |
44 | - /** @var bool */ |
|
45 | - private $holiday; |
|
44 | + /** @var bool */ |
|
45 | + private $holiday; |
|
46 | 46 | |
47 | - /** @var string */ |
|
48 | - private $name; |
|
47 | + /** @var string */ |
|
48 | + private $name; |
|
49 | 49 | |
50 | - public function __construct(string $name, bool $holiday, CalendarDay $day) |
|
51 | - { |
|
52 | - $this->calendarDay = $day; |
|
53 | - $this->holiday = $holiday; |
|
54 | - $this->name = $name; |
|
55 | - } |
|
50 | + public function __construct(string $name, bool $holiday, CalendarDay $day) |
|
51 | + { |
|
52 | + $this->calendarDay = $day; |
|
53 | + $this->holiday = $holiday; |
|
54 | + $this->name = $name; |
|
55 | + } |
|
56 | 56 | |
57 | - public function dateMatches(DateTimeInterface $date): bool |
|
58 | - { |
|
59 | - return $this->calendarDay->isSameDay($date); |
|
60 | - } |
|
57 | + public function dateMatches(DateTimeInterface $date): bool |
|
58 | + { |
|
59 | + return $this->calendarDay->isSameDay($date); |
|
60 | + } |
|
61 | 61 | |
62 | - public function getName(): string |
|
63 | - { |
|
64 | - return $this->name; |
|
65 | - } |
|
62 | + public function getName(): string |
|
63 | + { |
|
64 | + return $this->name; |
|
65 | + } |
|
66 | 66 | |
67 | - public function isHoliday(): bool |
|
68 | - { |
|
69 | - return $this->holiday; |
|
70 | - } |
|
67 | + public function isHoliday(): bool |
|
68 | + { |
|
69 | + return $this->holiday; |
|
70 | + } |
|
71 | 71 | } |
@@ -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 | - private function ruleMatches(SwapRule $rule, GregorianWeekday $weekday): bool |
|
54 | - { |
|
55 | - return in_array($weekday, $rule->getSwapWhenDays(), true); |
|
56 | - } |
|
57 | - |
|
58 | - private function isModifiedDate(DateTimeInterface $dateTime, GregorianWeekday $modifiedDay, SwapDirection $direction): bool |
|
59 | - { |
|
60 | - $cal = $this->day->getCalendar(); |
|
61 | - $cal = CalendarDay::setGregorianYearForDate((int) $dateTime->format('Y'), $cal); |
|
62 | - $day = $cal->toDateTime(); |
|
63 | - $day->modify($direction->getDateTimeDirection() . ' ' . $modifiedDay); |
|
64 | - $cal->setTime($day->getTimestamp() * 1000); |
|
65 | - $cal2 = $this->day->getCalendar(); |
|
66 | - $cal2->setTime($dateTime->getTimestamp() * 1000); |
|
67 | - |
|
68 | - if ($this->day->hasYearSet() && $cal->get(IntlCalendar::FIELD_YEAR) !== $cal2->get(IntlCalendar::FIELD_YEAR)) { |
|
69 | - return false; |
|
70 | - } |
|
71 | - |
|
72 | - if ($cal->get(IntlCalendar::FIELD_MONTH) !== $cal2->get(IntlCalendar::FIELD_MONTH)) { |
|
73 | - return false; |
|
74 | - } |
|
75 | - |
|
76 | - return $cal->get(IntlCalendar::FIELD_DAY_OF_MONTH) === $cal2->get(IntlCalendar::FIELD_DAY_OF_MONTH); |
|
77 | - } |
|
78 | - |
|
79 | - public function getName(): string |
|
80 | - { |
|
81 | - return $this->rule->getName(); |
|
82 | - } |
|
83 | - |
|
84 | - public function isHoliday(): bool |
|
85 | - { |
|
86 | - return $this->rule->isHoliday(); |
|
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 | + private function ruleMatches(SwapRule $rule, GregorianWeekday $weekday): bool |
|
54 | + { |
|
55 | + return in_array($weekday, $rule->getSwapWhenDays(), true); |
|
56 | + } |
|
57 | + |
|
58 | + private function isModifiedDate(DateTimeInterface $dateTime, GregorianWeekday $modifiedDay, SwapDirection $direction): bool |
|
59 | + { |
|
60 | + $cal = $this->day->getCalendar(); |
|
61 | + $cal = CalendarDay::setGregorianYearForDate((int) $dateTime->format('Y'), $cal); |
|
62 | + $day = $cal->toDateTime(); |
|
63 | + $day->modify($direction->getDateTimeDirection() . ' ' . $modifiedDay); |
|
64 | + $cal->setTime($day->getTimestamp() * 1000); |
|
65 | + $cal2 = $this->day->getCalendar(); |
|
66 | + $cal2->setTime($dateTime->getTimestamp() * 1000); |
|
67 | + |
|
68 | + if ($this->day->hasYearSet() && $cal->get(IntlCalendar::FIELD_YEAR) !== $cal2->get(IntlCalendar::FIELD_YEAR)) { |
|
69 | + return false; |
|
70 | + } |
|
71 | + |
|
72 | + if ($cal->get(IntlCalendar::FIELD_MONTH) !== $cal2->get(IntlCalendar::FIELD_MONTH)) { |
|
73 | + return false; |
|
74 | + } |
|
75 | + |
|
76 | + return $cal->get(IntlCalendar::FIELD_DAY_OF_MONTH) === $cal2->get(IntlCalendar::FIELD_DAY_OF_MONTH); |
|
77 | + } |
|
78 | + |
|
79 | + public function getName(): string |
|
80 | + { |
|
81 | + return $this->rule->getName(); |
|
82 | + } |
|
83 | + |
|
84 | + public function isHoliday(): bool |
|
85 | + { |
|
86 | + return $this->rule->isHoliday(); |
|
87 | + } |
|
88 | 88 | } |
@@ -40,17 +40,17 @@ |
||
40 | 40 | */ |
41 | 41 | class HolidayIterator extends ArrayObject |
42 | 42 | { |
43 | - /** |
|
44 | - * @param mixed $value |
|
45 | - * @return void |
|
46 | - */ |
|
47 | - #[ReturnTypeWillChange] |
|
48 | - public function append($value) |
|
49 | - { |
|
50 | - if (!$value instanceof HolidayIteratorItemInterface) { |
|
51 | - return; |
|
52 | - } |
|
43 | + /** |
|
44 | + * @param mixed $value |
|
45 | + * @return void |
|
46 | + */ |
|
47 | + #[ReturnTypeWillChange] |
|
48 | + public function append($value) |
|
49 | + { |
|
50 | + if (!$value instanceof HolidayIteratorItemInterface) { |
|
51 | + return; |
|
52 | + } |
|
53 | 53 | |
54 | - parent::append($value); |
|
55 | - } |
|
54 | + parent::append($value); |
|
55 | + } |
|
56 | 56 | } |
@@ -36,9 +36,9 @@ |
||
36 | 36 | |
37 | 37 | interface HolidayIteratorItemInterface |
38 | 38 | { |
39 | - public function dateMatches(DateTimeInterface $date): bool; |
|
39 | + public function dateMatches(DateTimeInterface $date): bool; |
|
40 | 40 | |
41 | - public function getName(): string; |
|
41 | + public function getName(): string; |
|
42 | 42 | |
43 | - public function isHoliday(): bool; |
|
43 | + public function isHoliday(): bool; |
|
44 | 44 | } |
@@ -36,38 +36,38 @@ |
||
36 | 36 | |
37 | 37 | class Calendar |
38 | 38 | { |
39 | - public const BUDDHIST = 'buddhist'; |
|
40 | - public const CHINESE = 'chinese'; |
|
41 | - public const COPTIC = 'coptic'; |
|
42 | - public const ETHIOPIAN = 'ethiopian'; |
|
43 | - public const GREGORIAN = 'gregorian'; |
|
44 | - public const HEBREW = 'hebrew'; |
|
45 | - public const INDIAN = 'indian'; |
|
46 | - public const ISLAMIC = 'islamic'; |
|
47 | - public const ISLAMIC_CIVIL = 'islamic-civil'; |
|
48 | - public const ISLAMIC_UMALQURA = 'islamic-umalqura'; |
|
49 | - public const ISLAMIC_RGSA = 'islamic-rgsa'; |
|
50 | - public const ISLAMIC_TBLA = 'islamic-tbla'; |
|
51 | - public const JAPANESE = 'japanese'; |
|
52 | - public const PERSIAN = 'persian'; |
|
39 | + public const BUDDHIST = 'buddhist'; |
|
40 | + public const CHINESE = 'chinese'; |
|
41 | + public const COPTIC = 'coptic'; |
|
42 | + public const ETHIOPIAN = 'ethiopian'; |
|
43 | + public const GREGORIAN = 'gregorian'; |
|
44 | + public const HEBREW = 'hebrew'; |
|
45 | + public const INDIAN = 'indian'; |
|
46 | + public const ISLAMIC = 'islamic'; |
|
47 | + public const ISLAMIC_CIVIL = 'islamic-civil'; |
|
48 | + public const ISLAMIC_UMALQURA = 'islamic-umalqura'; |
|
49 | + public const ISLAMIC_RGSA = 'islamic-rgsa'; |
|
50 | + public const ISLAMIC_TBLA = 'islamic-tbla'; |
|
51 | + public const JAPANESE = 'japanese'; |
|
52 | + public const PERSIAN = 'persian'; |
|
53 | 53 | |
54 | - public static function isValidCalendarName(string $calendarname): bool |
|
55 | - { |
|
56 | - return in_array($calendarname, [ |
|
57 | - self::BUDDHIST, |
|
58 | - self::CHINESE, |
|
59 | - self::COPTIC, |
|
60 | - self::ETHIOPIAN, |
|
61 | - self::GREGORIAN, |
|
62 | - self::HEBREW, |
|
63 | - self::INDIAN, |
|
64 | - self::ISLAMIC, |
|
65 | - self::ISLAMIC_CIVIL, |
|
66 | - self::ISLAMIC_RGSA, |
|
67 | - self::ISLAMIC_TBLA, |
|
68 | - self::ISLAMIC_UMALQURA, |
|
69 | - self::JAPANESE, |
|
70 | - self::PERSIAN, |
|
71 | - ]); |
|
72 | - } |
|
54 | + public static function isValidCalendarName(string $calendarname): bool |
|
55 | + { |
|
56 | + return in_array($calendarname, [ |
|
57 | + self::BUDDHIST, |
|
58 | + self::CHINESE, |
|
59 | + self::COPTIC, |
|
60 | + self::ETHIOPIAN, |
|
61 | + self::GREGORIAN, |
|
62 | + self::HEBREW, |
|
63 | + self::INDIAN, |
|
64 | + self::ISLAMIC, |
|
65 | + self::ISLAMIC_CIVIL, |
|
66 | + self::ISLAMIC_RGSA, |
|
67 | + self::ISLAMIC_TBLA, |
|
68 | + self::ISLAMIC_UMALQURA, |
|
69 | + self::JAPANESE, |
|
70 | + self::PERSIAN, |
|
71 | + ]); |
|
72 | + } |
|
73 | 73 | } |
@@ -14,47 +14,47 @@ |
||
14 | 14 | |
15 | 15 | final class ObservanceDecorator implements HolidayIteratorItemInterface |
16 | 16 | { |
17 | - /** @var int|null */ |
|
18 | - private $firstObservance; |
|
19 | - |
|
20 | - /** @var int|null */ |
|
21 | - private $lastObservance; |
|
22 | - |
|
23 | - /** @var HolidayIteratorItemInterface */ |
|
24 | - private $wrapped; |
|
25 | - |
|
26 | - public function __construct(HolidayIteratorItemInterface $wrapped, ?int $firstObservance, ?int $lastObservance) |
|
27 | - { |
|
28 | - $this->wrapped = $wrapped; |
|
29 | - $this->firstObservance = $firstObservance; |
|
30 | - $this->lastObservance = $lastObservance; |
|
31 | - } |
|
32 | - |
|
33 | - public function dateMatches(DateTimeInterface $date): bool |
|
34 | - { |
|
35 | - if (!$this->isWithinObservance((int) $date->format('Y'))) { |
|
36 | - return false; |
|
37 | - } |
|
38 | - |
|
39 | - return $this->wrapped->dateMatches($date); |
|
40 | - } |
|
41 | - |
|
42 | - private function isWithinObservance(int $gregorianYear): bool |
|
43 | - { |
|
44 | - if (null !== $this->firstObservance && $this->firstObservance > $gregorianYear) { |
|
45 | - return false; |
|
46 | - } |
|
47 | - |
|
48 | - return null === $this->lastObservance || $this->lastObservance >= $gregorianYear; |
|
49 | - } |
|
50 | - |
|
51 | - public function getName(): string |
|
52 | - { |
|
53 | - return $this->wrapped->getName(); |
|
54 | - } |
|
55 | - |
|
56 | - public function isHoliday(): bool |
|
57 | - { |
|
58 | - return $this->wrapped->isHoliday(); |
|
59 | - } |
|
17 | + /** @var int|null */ |
|
18 | + private $firstObservance; |
|
19 | + |
|
20 | + /** @var int|null */ |
|
21 | + private $lastObservance; |
|
22 | + |
|
23 | + /** @var HolidayIteratorItemInterface */ |
|
24 | + private $wrapped; |
|
25 | + |
|
26 | + public function __construct(HolidayIteratorItemInterface $wrapped, ?int $firstObservance, ?int $lastObservance) |
|
27 | + { |
|
28 | + $this->wrapped = $wrapped; |
|
29 | + $this->firstObservance = $firstObservance; |
|
30 | + $this->lastObservance = $lastObservance; |
|
31 | + } |
|
32 | + |
|
33 | + public function dateMatches(DateTimeInterface $date): bool |
|
34 | + { |
|
35 | + if (!$this->isWithinObservance((int) $date->format('Y'))) { |
|
36 | + return false; |
|
37 | + } |
|
38 | + |
|
39 | + return $this->wrapped->dateMatches($date); |
|
40 | + } |
|
41 | + |
|
42 | + private function isWithinObservance(int $gregorianYear): bool |
|
43 | + { |
|
44 | + if (null !== $this->firstObservance && $this->firstObservance > $gregorianYear) { |
|
45 | + return false; |
|
46 | + } |
|
47 | + |
|
48 | + return null === $this->lastObservance || $this->lastObservance >= $gregorianYear; |
|
49 | + } |
|
50 | + |
|
51 | + public function getName(): string |
|
52 | + { |
|
53 | + return $this->wrapped->getName(); |
|
54 | + } |
|
55 | + |
|
56 | + public function isHoliday(): bool |
|
57 | + { |
|
58 | + return $this->wrapped->isHoliday(); |
|
59 | + } |
|
60 | 60 | } |
@@ -12,50 +12,50 @@ |
||
12 | 12 | |
13 | 13 | final class SwapDirection |
14 | 14 | { |
15 | - private const FORWARD = 'forward'; |
|
16 | - private const REWIND = 'rewind'; |
|
17 | - /** @var array<string, self> */ |
|
18 | - private static $instances = []; |
|
19 | - /** @var string */ |
|
20 | - private $value; |
|
21 | - |
|
22 | - private function __construct(string $value) |
|
23 | - { |
|
24 | - $this->value = $value; |
|
25 | - } |
|
26 | - |
|
27 | - public static function forward(): self |
|
28 | - { |
|
29 | - if (!isset(self::$instances[self::FORWARD])) { |
|
30 | - self::$instances[self::FORWARD] = new self(self::FORWARD); |
|
31 | - } |
|
32 | - |
|
33 | - return self::$instances[self::FORWARD]; |
|
34 | - } |
|
35 | - |
|
36 | - public static function rewind(): self |
|
37 | - { |
|
38 | - if (!isset(self::$instances[self::REWIND])) { |
|
39 | - self::$instances[self::REWIND] = new self(self::REWIND); |
|
40 | - } |
|
41 | - |
|
42 | - return self::$instances[self::REWIND]; |
|
43 | - } |
|
44 | - |
|
45 | - public function getValue(): string |
|
46 | - { |
|
47 | - return $this->value; |
|
48 | - } |
|
49 | - |
|
50 | - public function getDateTimeDirection(): string |
|
51 | - { |
|
52 | - switch ($this->value) { |
|
53 | - case self::REWIND: |
|
54 | - return 'previous'; |
|
55 | - case self::FORWARD: |
|
56 | - return 'next'; |
|
57 | - } |
|
58 | - |
|
59 | - return ''; |
|
60 | - } |
|
15 | + private const FORWARD = 'forward'; |
|
16 | + private const REWIND = 'rewind'; |
|
17 | + /** @var array<string, self> */ |
|
18 | + private static $instances = []; |
|
19 | + /** @var string */ |
|
20 | + private $value; |
|
21 | + |
|
22 | + private function __construct(string $value) |
|
23 | + { |
|
24 | + $this->value = $value; |
|
25 | + } |
|
26 | + |
|
27 | + public static function forward(): self |
|
28 | + { |
|
29 | + if (!isset(self::$instances[self::FORWARD])) { |
|
30 | + self::$instances[self::FORWARD] = new self(self::FORWARD); |
|
31 | + } |
|
32 | + |
|
33 | + return self::$instances[self::FORWARD]; |
|
34 | + } |
|
35 | + |
|
36 | + public static function rewind(): self |
|
37 | + { |
|
38 | + if (!isset(self::$instances[self::REWIND])) { |
|
39 | + self::$instances[self::REWIND] = new self(self::REWIND); |
|
40 | + } |
|
41 | + |
|
42 | + return self::$instances[self::REWIND]; |
|
43 | + } |
|
44 | + |
|
45 | + public function getValue(): string |
|
46 | + { |
|
47 | + return $this->value; |
|
48 | + } |
|
49 | + |
|
50 | + public function getDateTimeDirection(): string |
|
51 | + { |
|
52 | + switch ($this->value) { |
|
53 | + case self::REWIND: |
|
54 | + return 'previous'; |
|
55 | + case self::FORWARD: |
|
56 | + return 'next'; |
|
57 | + } |
|
58 | + |
|
59 | + return ''; |
|
60 | + } |
|
61 | 61 | } |
@@ -38,22 +38,22 @@ |
||
38 | 38 | |
39 | 39 | class CalendarDayFactory |
40 | 40 | { |
41 | - public static function createCalendarDay(int $day, int $month, string $calendar): CalendarDay |
|
42 | - { |
|
43 | - if (!Calendar::isValidCalendarName($calendar)) { |
|
44 | - throw new UnknownCalendar(sprintf( |
|
45 | - 'The calendar %s is not known to the ICU', |
|
46 | - $calendar |
|
47 | - )); |
|
48 | - } |
|
49 | - $icuCalendar = IntlCalendar::createInstance('UTC', '@calendar=' . $calendar); |
|
41 | + public static function createCalendarDay(int $day, int $month, string $calendar): CalendarDay |
|
42 | + { |
|
43 | + if (!Calendar::isValidCalendarName($calendar)) { |
|
44 | + throw new UnknownCalendar(sprintf( |
|
45 | + 'The calendar %s is not known to the ICU', |
|
46 | + $calendar |
|
47 | + )); |
|
48 | + } |
|
49 | + $icuCalendar = IntlCalendar::createInstance('UTC', '@calendar=' . $calendar); |
|
50 | 50 | |
51 | - if ($icuCalendar === null) { |
|
52 | - throw new UnknownCalendar(sprintf( |
|
53 | - 'The %s calendar could not be created', |
|
54 | - $calendar |
|
55 | - )); |
|
56 | - } |
|
57 | - return new CalendarDay($day, $month, $icuCalendar); |
|
58 | - } |
|
51 | + if ($icuCalendar === null) { |
|
52 | + throw new UnknownCalendar(sprintf( |
|
53 | + 'The %s calendar could not be created', |
|
54 | + $calendar |
|
55 | + )); |
|
56 | + } |
|
57 | + return new CalendarDay($day, $month, $icuCalendar); |
|
58 | + } |
|
59 | 59 | } |
@@ -34,30 +34,30 @@ |
||
34 | 34 | |
35 | 35 | class Holiday |
36 | 36 | { |
37 | - /** @var bool */ |
|
38 | - private $holiday; |
|
39 | - |
|
40 | - /** @var string */ |
|
41 | - private $name; |
|
42 | - |
|
43 | - public function __construct(bool $holiday, string $name = '') |
|
44 | - { |
|
45 | - $this->holiday = $holiday; |
|
46 | - $this->name = $name; |
|
47 | - } |
|
48 | - |
|
49 | - public function isHoliday(): bool |
|
50 | - { |
|
51 | - return $this->holiday; |
|
52 | - } |
|
53 | - |
|
54 | - public function isNamed(): bool |
|
55 | - { |
|
56 | - return $this->name !== ''; |
|
57 | - } |
|
58 | - |
|
59 | - public function getName(): string |
|
60 | - { |
|
61 | - return $this->name; |
|
62 | - } |
|
37 | + /** @var bool */ |
|
38 | + private $holiday; |
|
39 | + |
|
40 | + /** @var string */ |
|
41 | + private $name; |
|
42 | + |
|
43 | + public function __construct(bool $holiday, string $name = '') |
|
44 | + { |
|
45 | + $this->holiday = $holiday; |
|
46 | + $this->name = $name; |
|
47 | + } |
|
48 | + |
|
49 | + public function isHoliday(): bool |
|
50 | + { |
|
51 | + return $this->holiday; |
|
52 | + } |
|
53 | + |
|
54 | + public function isNamed(): bool |
|
55 | + { |
|
56 | + return $this->name !== ''; |
|
57 | + } |
|
58 | + |
|
59 | + public function getName(): string |
|
60 | + { |
|
61 | + return $this->name; |
|
62 | + } |
|
63 | 63 | } |