generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from davsaniuv/main
Adding Full Mexican Holidays
- Loading branch information
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
.php_cs | ||
.php_cs.cache | ||
.phpunit.cache | ||
.vscode | ||
build | ||
composer.lock | ||
coverage | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Spatie\Holidays\Countries; | ||
|
||
use Carbon\Carbon; | ||
use Carbon\CarbonImmutable; | ||
use Carbon\CarbonPeriod; | ||
|
||
class Netherlands extends Country | ||
Check failure on line 9 in src/Countries/Mexico.php GitHub Actions / P8.3 - prefer-stable - ubuntu-latest
Check failure on line 9 in src/Countries/Mexico.php GitHub Actions / P8.1 - prefer-lowest - ubuntu-latest
|
||
{ | ||
public function countryCode(): string | ||
{ | ||
return 'mx'; | ||
} | ||
|
||
/** @return array<string, string|CarbonImmutable> */ | ||
protected function allHolidays(int $year): array | ||
{ | ||
return array_merge([ | ||
'Año nuevo' => '01-01', | ||
'Día de la Candelaria' => '02-02', | ||
'Día de la Bandera' => '02-24', | ||
'Día del Niño' => '04-30', | ||
'Día de la Madre' => '04-30', | ||
'Día del Trabajo' => '05-01', | ||
'Día de la Independencia' => '09-15', | ||
'Día de la Raza' => '10-12', | ||
'Día de Muertos' => '11-02', | ||
'Virgen de Guadalupe' => '12-12', | ||
'Navidad' => '12-25', | ||
], $this->variableHolidays($year)); | ||
} | ||
|
||
/** @return array<string, CarbonImmutable> */ | ||
protected function variableHolidays(int $year): array | ||
{ | ||
|
||
$natalicioBenitoJuarez = new CarbonImmutable(sprintf("third monday of march %s", $year)); | ||
$promulgacionConstitucion = new CarbonImmutable(sprintf("first monday of february %s", $year)); | ||
$revolucionMexicana = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-11-20")->setTimezone('America/Mexico_City'); | ||
|
||
if ($revolucionMexicana->isSunday()) { | ||
$revolucionMexicana = $revolucionMexicana->next('monday'); | ||
} | ||
|
||
$fathersDay = new CarbonImmutable(sprintf("third sunday of june %s", $year)); | ||
|
||
|
||
$days = [ | ||
'Aniversario de la promulgación de la Constitución de 1917' => $promulgacionConstitucion->format('m-d'), | ||
'Natalicio de Benito Juárez' => $natalicioBenitoJuarez->format('m-d'), | ||
'Revolución Mexicana' => $revolucionMexicana->format('m-d'), | ||
'Día del Padre' => $fathersDay->format('m-d'), | ||
|
||
]; | ||
|
||
if ($this->transmisionPoderEjecutivoFederal($year)) { | ||
$days[ | ||
"Transmisión del Poder Ejecutivo Federal" | ||
] = $this->transmisionPoderEjecutivoFederal($year); | ||
} | ||
|
||
return $days; | ||
} | ||
|
||
|
||
protected function transmisionPoderEjecutivoFederal($year): bool|string | ||
{ | ||
$period = new CarbonPeriod(); | ||
$period->setDateClass(CarbonImmutable::class); | ||
$period | ||
->every("6 years") | ||
->since(sprintf("%s-10-01", 2024)) | ||
->until(sprintf("%s-10-01 00:00:00", Carbon::now()->addYears(6)->year)); | ||
|
||
$period->addFilter(function ($date) use ($year) { | ||
return $date->year === $year; | ||
}); | ||
|
||
$availableDates = $period->toArray(); | ||
|
||
if (count($availableDates)) { | ||
return $availableDates[0]->format("m-d"); | ||
} | ||
return false; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Spatie\Holidays\Tests\Countries; | ||
|
||
use Carbon\CarbonImmutable; | ||
use Spatie\Holidays\Holidays; | ||
|
||
it('can calculate mexico holidays', function () { | ||
CarbonImmutable::setTestNowAndTimezone('2024-01-01'); | ||
|
||
$holidays = Holidays::for(country: 'mx')->get(); | ||
|
||
expect($holidays) | ||
->toBeArray() | ||
->not()->toBeEmpty(); | ||
|
||
expect(formatDates($holidays))->toMatchSnapshot(); | ||
|
||
}); |