Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support conditions with alternative calendar #85

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Cmixin/BusinessDay/Calculator/HolidayCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,23 @@ protected function calculateDynamicHoliday(string $holiday, &$dateTime = null, $

[$before, $after, $holiday] = $this->extractModifiers($holiday);

['start' => $start, 'end' => $end] = preg_match('/^(?<start>.*?)(?<end> (?:if|on|not on).*)$/', $holiday, $match)
? $match
: ['start' => $holiday, 'end' => null];

foreach ($this->calendars as $calendar) {
if (preg_match('/^\s*(\d+)\s+('.$calendar->getRegex().')\s*$/i', $holiday, $match)) {
if (preg_match('/^\s*(\d+)\s+('.$calendar->getRegex().')\s*$/i', $start, $match)) {
[$result, $nextHolidays] = $calendar->getHolidays($this->year, $match[1], $match[2], $key);

$this->nextHolidays = $nextHolidays;

return $result;
if (!$end) {
return $result;
}

$holiday = $result.$end;

break;
}
}

Expand Down
16 changes: 16 additions & 0 deletions tests/Cmixin/BusinessDayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,22 @@ public function testMultipleIfRule()
self::assertFalse($carbon::parse('2025-06-23')->isHoliday());
}

public function testIfRuleWithHijriCalendar()
{
$carbon = static::CARBON_CLASS;
$carbon::resetHolidays();
$carbon::setHolidays('custom', [
'hari-raya-puasa' => '= 1 Shawwal if sunday then next monday',
]);
$carbon::setHolidaysRegion('custom');
$year = static function (int $year) use ($carbon) {
return array_map('strval', $carbon::getYearHolidays($year));
};

self::assertSame(['hari-raya-puasa' => '2020-05-25 00:00:00'], $year(2020));
self::assertSame(['hari-raya-puasa' => '2021-05-13 00:00:00'], $year(2021));
}

public function testCaseInsensitivity()
{
$carbon = static::CARBON_CLASS;
Expand Down