Skip to content

Commit

Permalink
Extend PoC with test
Browse files Browse the repository at this point in the history
  • Loading branch information
herpaderpaldent committed Jan 24, 2024
1 parent b6db086 commit 92ab6ef
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 87 deletions.
150 changes: 63 additions & 87 deletions src/Countries/Switzerland.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,21 @@

class Switzerland extends Country
{
const HOLIDAYS_DE = [
'01-01' => 'Neujahrstag',
'01-02' => 'Berchtoldstag',
'03-19' => 'Josefstag',
'= easter -2' => 'Karfreitag',
'= easter' => 'Ostermontag',
'= easter 39' => 'Auffahrt',
'= easter 50' => 'Pfingstmontag',
'= easter 60' => 'Fronleichnam',
'08-01' => 'Bundesfeier',
'08-15' => 'Maria Himmelfahrt',
'11-01' => 'Allerheiligen',
'12-08' => 'Maria Empfängnis',
'12-25' => 'Weihnachtstag',
'12-26' => 'Stephanstag',
];

const HOLIDAYS_FR = [
'01-01' => 'Nouvel an',
'01-02' => 'Saint-Etienne',
'03-19' => 'Saint-Joseph',
'= easter -2' => 'Vendredi Saint',
'= easter' => 'Lundi de Pâques',
'= easter 39' => 'Ascension',
'= easter 50' => 'Lundi de Pentecôte',
'= easter 60' => 'Fête-Dieu',
'08-01' => 'Fête nationale',
'08-15' => 'Assomption',
'11-01' => 'Toussaint',
'12-25' => 'Noël',
'12-26' => 'Saint Etienne',
];

const HOLIDAYS_IT = [
'01-01' => 'Capodanno',
'01-02' => 'San Berchtoldo',
'= easter -2' => 'Venerdì Santo', // This does not seem to be a holiday in Ticino
'= easter' => 'Lunedì di Pasqua',
'= easter 39' => 'Ascensione',
'= easter 50' => 'Lunedì di Pentecoste',
'08-01' => 'Festa nazionale',
'12-25' => 'Natale',
'12-26' => 'Santo Stefano',
const HOLIDAYS = [
'New Year\'s Day' => ['de' => 'Neujahrstag', 'fr' => 'Nouvel an', 'it' => 'Capodanno', 'rm' => 'Bun di bun onn'],
'Berchtold Day' => ['de' => 'Berchtoldstag', 'fr' => '2 janvier', 'it' => 'Giorno di Berchtold', 'rm' => 'Di da Berchtold'],
'St. Joseph\'s Day' => ['de' => 'Josefstag', 'fr' => 'Saint-Joseph', 'it' => 'Giorno di San Giuseppe', 'rm' => 'Di da San Giusep'],
'Good Friday' => ['de' => 'Karfreitag', 'fr' => 'Vendredi saint', 'it' => 'Venerdì santo', 'rm' => 'Venderdi santo'],
'Easter Monday' => ['de' => 'Ostermontag', 'fr' => 'Lundi de Pâques', 'it' => 'Lunedì di Pasqua', 'rm' => 'Glindesdi da Pasca'],
'Ascension Day' => ['de' => 'Auffahrt', 'fr' => 'Ascension', 'it' => 'Ascensione', 'rm' => 'Ascensiun'],
'Whit Monday' => ['de' => 'Pfingstmontag', 'fr' => 'Lundi de Pentecôte', 'it' => 'Lunedì di Pentecoste', 'rm' => 'Glindesdi da Pentecosta'],
'Corpus Christi' => ['de' => 'Fronleichnam', 'fr' => 'Fête-Dieu', 'it' => 'Corpus Domini', 'rm' => 'Corpus Christi'],
'Swiss National Day' => ['de' => 'Bundesfeier', 'fr' => 'Fête nationale', 'it' => 'Festa nazionale', 'rm' => 'Fiasta naziunala'],
'Assumption Day' => ['de' => 'Maria Himmelfahrt', 'fr' => 'Assomption', 'it' => 'Assunzione', 'rm' => 'Assunta'],
'All Saints\' Day' => ['de' => 'Allerheiligen', 'fr' => 'Toussaint', 'it' => 'Ognissanti', 'rm' => 'Ognissants'],
'Immaculate Conception' => ['de' => 'Maria Empfängnis', 'fr' => 'Immaculée Conception', 'it' => 'Immacolata Concezione', 'rm' => 'Concepziun da Maria'],
'Christmas Day' => ['de' => 'Weihnachtstag', 'fr' => 'Noël', 'it' => 'Natale', 'rm' => 'Nadal'],
'St. Stephen\'s Day' => ['de' => 'Stephanstag', 'fr' => 'Saint-Étienne', 'it' => 'Santo Stefano', 'rm' => 'San Steffan'],
];

protected function __construct(
Expand All @@ -64,67 +36,67 @@ public function countryCode(): string

protected function allHolidays(int $year): array
{

return array_merge([
$this->getLocaleHoliday('08-01')=> '08-01',
$this->trans('Swiss National Day')=> '08-01',
],
$this->region ? $this->getSwissHolidays($year) : $this->getRegionHolidays($year),
$this->region ? $this->getRegionHolidays($year) : $this->getSwissHolidays($year),
);
}

// This defaults to SBB/CFF/FFS holidays or postal holidays
// https://github.com/spatie/holidays/pull/49/files#r1462344840
/**
* This defaults to SBB/CFF/FFS holidays or postal holidays
* https://github.com/spatie/holidays/pull/49/files#r1462344840
* @return array<string, CarbonImmutable>
*/
private function getSwissHolidays(int $year): array
{

$easter = $this->easter($year);

return array_merge([
$this->getLocaleHoliday('01-01') => '01-01',
$this->getLocaleHoliday('01-02') => '01-02',
$this->getLocaleHoliday('25-12') => '12-25',
$this->getLocaleHoliday('26-12') => '12-26',
$this->trans('New Year\'s Day') => '01-01',
$this->trans('Berchtold Day') => '01-02',
$this->trans('Christmas Day') => '12-25',
$this->trans('St. Stephen\'s Day') => '12-26',
], $this->getEasterHolidays($year));
}

private function getEasterHolidays(int $year, bool $with_easter_minus_two = true, bool $add_corpus_christi = false): array
/** @return array<string, CarbonImmutable> */
private function getEasterHolidays(int $year, bool $with_good_friday = true, bool $with_corpus_christi = false): array
{

$easter = $this->easter($year);

$easter_holidays = [
$this->getLocaleHoliday('= easter') => $easter->addDay(),
$this->getLocaleHoliday('= easter 39') => $easter->addDays(39),
$this->getLocaleHoliday('= easter 50') => $easter->addDays(50),
$this->trans('Easter Monday') => $easter->addDay(),
$this->trans('Ascension Day') => $easter->addDays(39),
$this->trans('Whit Monday') => $easter->addDays(50),
];

if ($with_easter_minus_two) {
$easter_holidays[$this->getLocaleHoliday('= easter -2')] = $easter->subDays(2);
if ($with_good_friday) {
$easter_holidays[$this->trans('Good Friday')] = $easter->subDays(2);
}

if ($add_corpus_christi) {
$easter_holidays[$this->getLocaleHoliday('= easter 60')] = $easter->addDays(60);
if ($with_corpus_christi) {
$easter_holidays[$this->trans('Corpus Christi')] = $easter->addDays(60);
}

return $easter_holidays;
}

/** @return array<string, CarbonImmutable> */
private function getRegionHolidays(int $year): array
{

if (!$this->region) {
return [];
}

// split region into canton and region
$region = explode('-', $this->region);
$canton = $region[0];
$region = $region[1];
$canton = $region[1];

// get holidays for canton
$holidays = $this->getCantonHolidays($canton, $year);

// if region is set, get holidays for region
if ($region) {
$holidays = array_merge($holidays, $this->getRegionHolidays($region, $year));
}

return $holidays;
return $this->getCantonHolidays($canton, $year);
}

private function getCantonHolidays(string $canton, int $year): array
Expand All @@ -135,28 +107,32 @@ private function getCantonHolidays(string $canton, int $year): array
};
}

private function getLocaleHoliday(string $string): string
private function trans(string $key): string
{
return match ($this->locale) {
'de' => self::HOLIDAYS_DE[$string],
'fr' => self::HOLIDAYS_FR[$string],
'it' => self::HOLIDAYS_IT[$string],
};

// return translation if available
if (isset(self::HOLIDAYS[$key][$this->locale])) {
return self::HOLIDAYS[$key][$this->locale];
}

// return key if no translation is available
return $key;
}

private function getVSHolidays(int $year)
/** @return array<string, string> */
private function getVSHolidays(int $year): array
{
$eastern_holidays = $this->getEasterHolidays($year, with_easter_minus_two: false, add_corpus_christi: true);
$eastern_holidays = $this->getEasterHolidays($year, with_good_friday: false, with_corpus_christi: true);

return array_merge([
$this->getLocaleHoliday('01-01') => '01-01',
$this->getLocaleHoliday('01-02') => '01-02',
$this->getLocaleHoliday('03-19') => '03-19', // St. Joseph
$this->getLocaleHoliday('08-15') => '08-15', // Assumption
$this->getLocaleHoliday('11-01') => '11-01', // All Saints
$this->getLocaleHoliday('12-08') => '12-08', // Immaculate Conception
$this->getLocaleHoliday('25-12') => '12-25',
$this->getLocaleHoliday('26-12') => '12-26',
$this->trans('New Year\'s Day') => '01-01',
$this->trans('Berchtold Day') => '01-02',
$this->trans('St. Joseph\'s Day') => '03-19', // St. Joseph
$this->trans('Assumption Day') => '08-15', // Assumption
$this->trans('All Saints\' Day') => '11-01', // All Saints
$this->trans('Immaculate Conception') => '12-08', // Immaculate Conception
$this->trans('Christmas Day') => '12-25',
$this->trans('St. Stephen\'s Day') => '12-26',
], $eastern_holidays);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[
{
"name": "Neujahrstag",
"date": "2024-01-01"
},
{
"name": "Berchtoldstag",
"date": "2024-01-02"
},
{
"name": "Josefstag",
"date": "2024-03-19"
},
{
"name": "Ostermontag",
"date": "2024-04-01"
},
{
"name": "Auffahrt",
"date": "2024-05-09"
},
{
"name": "Pfingstmontag",
"date": "2024-05-20"
},
{
"name": "Fronleichnam",
"date": "2024-05-30"
},
{
"name": "Bundesfeier",
"date": "2024-08-01"
},
{
"name": "Maria Himmelfahrt",
"date": "2024-08-15"
},
{
"name": "Allerheiligen",
"date": "2024-11-01"
},
{
"name": "Maria Empf\u00e4ngnis",
"date": "2024-12-08"
},
{
"name": "Weihnachtstag",
"date": "2024-12-25"
},
{
"name": "Stephanstag",
"date": "2024-12-26"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[
{
"name": "Nouvel an",
"date": "2024-01-01"
},
{
"name": "2 janvier",
"date": "2024-01-02"
},
{
"name": "Saint-Joseph",
"date": "2024-03-19"
},
{
"name": "Lundi de P\u00e2ques",
"date": "2024-04-01"
},
{
"name": "Ascension",
"date": "2024-05-09"
},
{
"name": "Lundi de Pentec\u00f4te",
"date": "2024-05-20"
},
{
"name": "F\u00eate-Dieu",
"date": "2024-05-30"
},
{
"name": "F\u00eate nationale",
"date": "2024-08-01"
},
{
"name": "Assomption",
"date": "2024-08-15"
},
{
"name": "Toussaint",
"date": "2024-11-01"
},
{
"name": "Immacul\u00e9e Conception",
"date": "2024-12-08"
},
{
"name": "No\u00ebl",
"date": "2024-12-25"
},
{
"name": "Saint-\u00c9tienne",
"date": "2024-12-26"
}
]
18 changes: 18 additions & 0 deletions tests/Countries/SwitzerlandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\Holidays\Tests\Countries;

use Carbon\CarbonImmutable;
use Spatie\Holidays\Countries\Switzerland;
use Spatie\Holidays\Holidays;

it('can calculate swiss holidays', function () {
Expand All @@ -16,3 +17,20 @@

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

describe('cantons', function () {
it('can calculate holidays for the canton valais', function (string $language) {

CarbonImmutable::setTestNowAndTimezone('2024-01-01');

$holidays = Holidays::for(Switzerland::make('ch-vs', $language))->get();

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

expect(formatDates($holidays))->toMatchSnapshot();
})->with(['de', 'fr'])->only();


});

0 comments on commit 92ab6ef

Please sign in to comment.