Skip to content

Commit

Permalink
Merge pull request #140 from spatie/fix/mexico
Browse files Browse the repository at this point in the history
mexico fixes
  • Loading branch information
Nielsvanpach authored Jan 19, 2024
2 parents f9ea67d + 8d52336 commit 4527270
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ parameters:
count: 1
path: src/Countries/Mexico.php

-
message: "#^Method Spatie\\\\Holidays\\\\Countries\\\\Mexico\\:\\:transmisionPoderEjecutivoFederal\\(\\) has parameter \\$year with no type specified\\.$#"
count: 1
path: src/Countries/Mexico.php

-
message: "#^Method Spatie\\\\Holidays\\\\Countries\\\\Mexico\\:\\:variableHolidays\\(\\) should return array\\<string, Carbon\\\\CarbonImmutable\\> but returns array\\<string, string\\|true\\>\\.$#"
count: 1
path: src/Countries/Mexico.php

-
message: "#^Cannot call method isSunday\\(\\) on Carbon\\\\CarbonImmutable\\|false\\.$#"
count: 1
Expand Down
34 changes: 16 additions & 18 deletions src/Countries/Mexico.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,43 +25,41 @@ protected function allHolidays(int $year): array
/** @return array<string, CarbonImmutable> */
protected function variableHolidays(int $year): array
{
$constitutionDay = (new CarbonImmutable("first monday of february $year")) // 5 of february
$constitutionDay = (new CarbonImmutable("first monday of february $year"))
->setTimezone('America/Mexico_City');

$benitoJuarezBirth = (new CarbonImmutable("third monday of March $year")) // 21 of march
$benitoJuarezBirth = (new CarbonImmutable("third monday of March $year"))
->setTimezone('America/Mexico_City');

$revolutionDay = (new CarbonImmutable("third monday of november $year")) // 20 of november
$revolutionDay = (new CarbonImmutable("third monday of november $year"))
->setTimezone('America/Mexico_City');

/** @var CarbonImmutable|false $executiveChange */
$executiveChange = $this->governmentChangeDate();

$known_days = [
'Día de la Constitución' => $constitutionDay, // It's the first monday of february
$holidays = [
'Día de la Constitución' => $constitutionDay,
'Natalicio de Benito Juárez' => $benitoJuarezBirth,
'Día de la Revolución' => $revolutionDay,
];

return array_merge(
$known_days,
$executiveChange ? ['Cambio de Gobierno' => $executiveChange] : []
);
$executiveChange = $this->governmentChangeDate($year);

if ($executiveChange) {
$holidays = array_merge($holidays, ['Cambio de Gobierno' => $executiveChange]);
}

return $holidays;
}

protected function governmentChangeDate(): CarbonImmutable|false
protected function governmentChangeDate(int $year): ?CarbonImmutable
{
$baseYear = 1946; // The first occurrence with president Miguel Aleman Valdes
$currentYear = CarbonImmutable::now()->year; // Get the current year

// Check if the current year is a transmission year
if (($currentYear - $baseYear) % 6 == 0) {
/** @phpstan-ignore-next-line */
return CarbonImmutable::create($currentYear, 10, 1) // October 1st of the transmission year
if (($year - $baseYear) % 6 === 0) {
return CarbonImmutable::create($year, 10, 1) // October 1st of the transmission year
->setTimezone('America/Mexico_City');

}

return false;
return null;
}
}

0 comments on commit 4527270

Please sign in to comment.