Skip to content

Commit

Permalink
Optimize calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Sep 21, 2021
1 parent 7f6eddf commit 818e6ec
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions src/Cmixin/BusinessDay/Calculator/HolidayCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,8 @@ protected function filterConditions(array $onConditions): iterable
}
}

protected function parseHolidaysString(string $holiday): ?array
protected function parseHolidaysString(string $holiday, $key = null): ?array
{
$outputClass = $this->outputClass;
$year = $this->year;

$holiday = $this->parseKeywords($holiday);
Expand All @@ -227,8 +226,7 @@ protected function parseHolidaysString(string $holiday): ?array
$holiday .= " $year";
}

/** @var DateTime $dateTime */
$dateTime = new $outputClass($holiday);
$dateTime = $this->createDate($holiday, $key);

foreach ($this->filterConditions($onConditions) as $days => $expected) {
$days = array_map('trim', explode(',', $days));
Expand All @@ -241,40 +239,40 @@ protected function parseHolidaysString(string $holiday): ?array
return [$dateTime, $condition];
}

protected function calculateDynamicHoliday(string $holiday, &$dateTime = null, $key = null)
/**
* @return DateTime
*/
protected function createDate(string $dateString, $key = null)
{
$holiday = str_replace(' in ', ' of ', trim(substr($holiday, 1)));
$substitute = $this->consumeHolidayString('/\ssubstitute$/i', $holiday);

if ($this->isIgnoredYear($holiday)) {
return false;
}

[$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];
$outputClass = $this->outputClass;

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

$this->nextHolidays = $nextHolidays;

if (!$end) {
return $result;
}
return new $outputClass($this->year.'-'.$result);
}
}

$holiday = $result.$end;
return new $outputClass($dateString);
}

break;
}
protected function calculateDynamicHoliday(string $holiday, &$dateTime = null, $key = null)
{
$holiday = str_replace(' in ', ' of ', trim(substr($holiday, 1)));
$substitute = $this->consumeHolidayString('/\ssubstitute$/i', $holiday);

if ($this->isIgnoredYear($holiday)) {
return false;
}

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

return $this->formatHolidayDefinition(
$dateTime,
$this->parseHolidaysString($holiday),
$this->parseHolidaysString($holiday, $key),
$substitute,
$after,
$before
Expand Down

0 comments on commit 818e6ec

Please sign in to comment.