Skip to content

Commit

Permalink
Merge pull request #23 from davsaniuv/main
Browse files Browse the repository at this point in the history
Adding Full Mexican Holidays
  • Loading branch information
freekmurze authored Jan 19, 2024
2 parents 15ae9e9 + e3a14de commit 4468719
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.php_cs
.php_cs.cache
.phpunit.cache
.vscode
build
composer.lock
coverage
Expand Down
87 changes: 87 additions & 0 deletions src/Countries/Mexico.php
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

View workflow job for this annotation

GitHub Actions / P8.3 - prefer-stable - ubuntu-latest

Cannot declare class Spatie\Holidays\Countries\Netherlands, because the name is already in use

Check failure on line 9 in src/Countries/Mexico.php

View workflow job for this annotation

GitHub Actions / P8.1 - prefer-lowest - ubuntu-latest

Cannot declare class Spatie\Holidays\Countries\Netherlands, because the name is already in use

Check failure on line 9 in src/Countries/Mexico.php

View workflow job for this annotation

GitHub Actions / P8.1 - prefer-stable - ubuntu-latest

Cannot declare class Spatie\Holidays\Countries\Netherlands, because the name is already in use
{
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;
}
}
19 changes: 19 additions & 0 deletions tests/Countries/MexicoTest.php
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();

});

0 comments on commit 4468719

Please sign in to comment.