Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add holidays for Ecuador #218

Merged
merged 14 commits into from
Apr 11, 2024
13 changes: 13 additions & 0 deletions lang/ecuador/es/holidays.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"New Year's Day": "Año Nuevo",
"Holy Friday": "Viernes Santo",
"Carnival Monday": "Lunes de Carnaval",
"Carnival Tuesday": "Martes de Carnaval",
"Labor Day": "Día del Trabajo",
"Battle of Pichincha": "Batalla de Pichincha",
"Independence Day": "Primer Grito de la Independencia",
"Independence Of Guayaquil": "Independencia de Guayaquil",
"All Souls' Day": "Día de Los Difuntos",
"Independence Of Cuenca": "Independencia de Cuenca",
"Christmas": "Navidad"
}
79 changes: 79 additions & 0 deletions src/Countries/Ecuador.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;
use Spatie\Holidays\Concerns\Observable;
use Spatie\Holidays\Concerns\Translatable;
use Spatie\Holidays\Contracts\HasTranslations;

class Ecuador extends Country implements HasTranslations
{
use Observable;
use Translatable;

public function countryCode(): string
{
return 'ec';
}

public function defaultLocale(): string
{
return 'es';
}

protected function allHolidays(int $year): array
{
return array_merge([
'New Year\'s Day' => '01-01',
], $this->variableHolidays($year));
}

public function nearestDay(int $year, int $month, int $day): CarbonImmutable
{
$date = CarbonImmutable::createFromDate($year, $month, $day);

if($date->is('Tuesday') || $date->is('Saturday')) {
return $date->subDay();
}

if($date->is('Sunday')) {
return $date->addDay();
}

if($date->is('Wednesday') || $date->is('Thursday')) {
return $date->next(CarbonImmutable::FRIDAY);

Check failure on line 45 in src/Countries/Ecuador.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Spatie\Holidays\Countries\Ecuador::nearestDay() should return Carbon\CarbonImmutable but returns Carbon\CarbonInterface.
}

return $date;
}

public function getChristmasHoliday(int $year): CarbonImmutable
{
if($year === 2022) {
insoutt marked this conversation as resolved.
Show resolved Hide resolved
Nielsvanpach marked this conversation as resolved.
Show resolved Hide resolved
return $this->sundayToNextMonday('12-25', $year);

Check failure on line 54 in src/Countries/Ecuador.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Spatie\Holidays\Countries\Ecuador::getChristmasHoliday() should return Carbon\CarbonImmutable but returns Carbon\CarbonInterface|null.
}

return CarbonImmutable::createFromDate($year, 12, 25);
}

/** @return array<string, CarbonImmutable> */
protected function variableHolidays(int $year): array
{
$easter = $this->easter($year);
insoutt marked this conversation as resolved.
Show resolved Hide resolved
$ashWednesday = $easter->subDays(46);

return [
'Holy Friday' => $easter->subDays(2),
'Carnival Monday' => $ashWednesday->subDays(2),
'Carnival Tuesday' => $ashWednesday->subDay(),
'Labor Day' => $this->nearestDay($year, 5, 1),
'Battle of Pichincha' => $this->nearestDay($year, 5, 24),
'Independence Day' => $this->nearestDay($year, 8, 10),
'Independence Of Guayaquil' => $this->nearestDay($year, 10, 9),
'All Souls\' Day' => $this->nearestDay($year, 11, 2),
'Independence Of Cuenca' => $this->nearestDay($year, 11, 3),
'Christmas' => $this->getChristmasHoliday($year),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[
{
"name": "A\u00f1o Nuevo",
"date": "2024-01-01"
},
{
"name": "Lunes de Carnaval",
"date": "2024-02-12"
},
{
"name": "Martes de Carnaval",
"date": "2024-02-13"
},
{
"name": "Viernes Santo",
"date": "2024-03-29"
},
{
"name": "D\u00eda del Trabajo",
"date": "2024-05-03"
},
{
"name": "Batalla de Pichincha",
"date": "2024-05-24"
},
{
"name": "Primer Grito de la Independencia",
"date": "2024-08-09"
},
{
"name": "Independencia de Guayaquil",
"date": "2024-10-11"
},
{
"name": "D\u00eda de Los Difuntos",
"date": "2024-11-01"
},
{
"name": "Independencia de Cuenca",
"date": "2024-11-04"
},
{
"name": "Navidad",
"date": "2024-12-25"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[
{
"name": "New Year's Day",
"date": "2024-01-01"
},
{
"name": "Carnival Monday",
"date": "2024-02-12"
},
{
"name": "Carnival Tuesday",
"date": "2024-02-13"
},
{
"name": "Holy Friday",
"date": "2024-03-29"
},
{
"name": "Labor Day",
"date": "2024-05-03"
},
{
"name": "Battle of Pichincha",
"date": "2024-05-24"
},
{
"name": "Independence Day",
"date": "2024-08-09"
},
{
"name": "Independence Of Guayaquil",
"date": "2024-10-11"
},
{
"name": "All Souls' Day",
"date": "2024-11-01"
},
{
"name": "Independence Of Cuenca",
"date": "2024-11-04"
},
{
"name": "Christmas",
"date": "2024-12-25"
}
]
31 changes: 31 additions & 0 deletions tests/Countries/EcuadorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Spatie\Holidays\Tests\Countries;

use Carbon\CarbonImmutable;
use Spatie\Holidays\Holidays;

it('can calculate ecuador holidays', function () {
insoutt marked this conversation as resolved.
Show resolved Hide resolved
CarbonImmutable::setTestNow('2024-01-01');

$holidays = Holidays::for(country: 'ec')->get();

expect($holidays)
->toBeArray()
->not()->toBeEmpty();

expect(formatDates($holidays))->toMatchSnapshot();
});

it('can calculate ecuador holidays with english translation', function () {
CarbonImmutable::setTestNow('2024-01-01');

$holidays = Holidays::for(country: 'ec', locale: 'en')->get();

expect($holidays)
->toBeArray()
->not()->toBeEmpty();

expect(formatDates($holidays))->toMatchSnapshot();
});

Loading