Skip to content

Commit

Permalink
Code-refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
heiglandreas committed Aug 5, 2023
1 parent ed93827 commit ee5ce46
Show file tree
Hide file tree
Showing 21 changed files with 506 additions and 509 deletions.
64 changes: 32 additions & 32 deletions src/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
}
184 changes: 92 additions & 92 deletions src/CalendarDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
{
Expand All @@ -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)) {
Expand All @@ -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);

Check warning on line 121 in src/CalendarDay.php

View check run for this annotation

Codecov / codecov/patch

src/CalendarDay.php#L121

Added line #L121 was not covered by tests
}
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);
}
}
22 changes: 11 additions & 11 deletions src/CalendarDayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Factory/DateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function itemFromDomElement(DOMElement $element): ?HolidayIteratorItemInt

$date = new Date(
$element->textContent,
$element->getAttribute('free') === "true",
$element->getAttribute('free') === "true",
$day,
);

Expand Down
2 changes: 1 addition & 1 deletion src/Factory/EasterOrthodoxFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Factory/ObservanceDecoratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
22 changes: 11 additions & 11 deletions src/Factory/SwapDecoratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
*/
Expand All @@ -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))
);
}
}
Loading

0 comments on commit ee5ce46

Please sign in to comment.