Created
November 15, 2021 15:13
-
-
Save marcusedu/79dba1c9b190df65d45c213211c4254e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function pegarValidadeDiaUtil($dias = 30, $finaisDeSemana = [0, 6]): Carbon | |
{ | |
$agora = now(); | |
$anoInicial = $agora->year; | |
$numFinaisDeSemanaParaPular = 0; | |
for ($i = 0; $i < $dias; $i++) { | |
$agora->addDays(); | |
if (in_array($agora->weekday(), $finaisDeSemana)) { | |
$numFinaisDeSemanaParaPular++; | |
} | |
} | |
$validadeDiaUtilSemFeriado = now()->addDays($numFinaisDeSemanaParaPular + $dias) | |
->setHour(23) | |
->setMinute(59) | |
->setSeconds(59); | |
$agora = now()->setHour(0) | |
->setMinute(0) | |
->setSeconds(0) | |
->setMillisecond(0); | |
$holidayClient = new HolidayClient('https://api.calendario.com.br', env('LARAVEL_HOLIDAY_CALENDAR_TOKEN')); | |
$holidayClient->setJsonResponse(); | |
if ($validadeDiaUtilSemFeriado->year > $anoInicial) { | |
$feriadosProximoAno = $holidayClient->getHolidaysByCity($validadeDiaUtilSemFeriado->year, 'Belo Horizonte', 'MG'); | |
} | |
$feriados = array_merge($feriadosProximoAno ?? [], | |
$feriadosProximoAno = $holidayClient->getHolidaysByCity($anoInicial, 'Belo Horizonte', 'MG')); | |
$numFeriadosParaPular = 0; | |
foreach ($feriados as $feriado) { | |
$day = new Carbon($feriado->getDate()); | |
if ($day | |
->isAfter($agora) | |
&& $day | |
->isBefore($validadeDiaUtilSemFeriado) | |
&& !in_array($day | |
->weekday(), $finaisDeSemana)) { | |
$numFeriadosParaPular++; | |
} | |
} | |
$validade = now()->addDays($numFinaisDeSemanaParaPular + $numFeriadosParaPular + $dias); | |
if (in_array($validade->weekday(), $finaisDeSemana)) $validade->next('monday'); | |
return $validade; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment