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

Locale fallback #176

Merged
merged 4 commits into from
Sep 29, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- Upgraded to PHPUnit 8.
- Replaced the standard 'InvalidArgumentException' when an invalid year or holiday provider are given by a new exception for each of these two situations separately ('InvalidYearException' and 'ProviderNotFoundException'). This allows you to better distinguish which exception may occur when instantiating the Yasumi class. [\#95](https://github.com/azuyalabs/yasumi/pull/95) ([qneyrat](https://github.com/qneyrat))
- Refactored the AbstractProvider::count method to use the newly added SubstituteHoliday class.
- Fallback support added to getName() to allow e.g. fallback from 'de_AT' to 'de'. [\#176](https://github.com/azuyalabs/yasumi/pull/176) ([c960657](https://github.com/c960657))

### Fixed
- Late Summer Bank Holiday in 1968 and 1969 in United Kingdom [\#161](https://github.com/azuyalabs/yasumi/pull/161) ([c960657](https://github.com/c960657))
Expand Down
15 changes: 14 additions & 1 deletion src/Yasumi/Holiday.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,20 @@ public function jsonSerialize(): self
*/
public function getName(): string
{
return $this->translations[$this->displayLocale] ?? $this->translations[self::DEFAULT_LOCALE] ?? $this->shortName;
$locales = [$this->displayLocale];
$parts = \explode('_', $this->displayLocale);
while (\array_pop($parts) && $parts) {
$locales[] = \implode('_', $parts);
}
$locales[] = self::DEFAULT_LOCALE;

foreach ($locales as $locale) {
if (isset($this->translations[$locale])) {
return $this->translations[$locale];
}
}

return $this->shortName;
}

/**
Expand Down
24 changes: 12 additions & 12 deletions src/Yasumi/Provider/Australia.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public function initialize(): void
private function calculateNewYearHolidays(): void
{
$newyearsday = new DateTime("$this->year-01-01", new DateTimeZone($this->timezone));
$this->calculateHoliday('newYearsDay', ['en_AU' => 'New Year\'s Day'], $newyearsday, false, false);
$this->calculateHoliday('newYearsDay', [], $newyearsday, false, false);
switch ($newyearsday->format('w')) {
case 0: // sunday
$newyearsday->add(new DateInterval('P1D'));
$this->calculateHoliday('newYearsHoliday', ['en_AU' => 'New Year\'s Holiday'], $newyearsday, false, false);
$this->calculateHoliday('newYearsHoliday', ['en' => 'New Year\'s Holiday'], $newyearsday, false, false);
break;
case 6: // saturday
$newyearsday->add(new DateInterval('P2D'));
$this->calculateHoliday('newYearsHoliday', ['en_AU' => 'New Year\'s Holiday'], $newyearsday, false, false);
$this->calculateHoliday('newYearsHoliday', ['en' => 'New Year\'s Holiday'], $newyearsday, false, false);
break;
}
}
Expand Down Expand Up @@ -137,7 +137,7 @@ private function calculateAustraliaDay(): void
{
$date = new DateTime("$this->year-01-26", new DateTimeZone($this->timezone));

$this->calculateHoliday('australiaDay', ['en_AU' => 'Australia Day'], $date);
$this->calculateHoliday('australiaDay', ['en' => 'Australia Day'], $date);
}

/**
Expand All @@ -163,7 +163,7 @@ private function calculateAnzacDay(): void
}

$date = new DateTime("$this->year-04-25", new DateTimeZone($this->timezone));
$this->calculateHoliday('anzacDay', ['en_AU' => 'ANZAC Day'], $date, false, false);
$this->calculateHoliday('anzacDay', [], $date, false, false);
$easter = $this->calculateEaster($this->year, $this->timezone);

$easterMonday = $this->calculateEaster($this->year, $this->timezone);
Expand All @@ -172,7 +172,7 @@ private function calculateAnzacDay(): void
$fDate = $date->format('Y-m-d');
if ($fDate === $easter->format('Y-m-d') || $fDate === $easterMonday->format('Y-m-d')) {
$easterMonday->add(new DateInterval('P1D'));
$this->calculateHoliday('easterTuesday', ['en_AU' => 'Easter Tuesday'], $easterMonday, false, false);
$this->calculateHoliday('easterTuesday', ['en' => 'Easter Tuesday'], $easterMonday, false, false);
}
unset($fDate);
}
Expand All @@ -193,23 +193,23 @@ private function calculateChristmasDay(): void
{
$christmasDay = new DateTime("$this->year-12-25", new DateTimeZone($this->timezone));
$boxingDay = new DateTime("$this->year-12-26", new DateTimeZone($this->timezone));
$this->calculateHoliday('christmasDay', ['en_AU' => 'Christmas Day'], $christmasDay, false, false);
$this->calculateHoliday('secondChristmasDay', ['en_AU' => 'Boxing Day'], $boxingDay, false, false);
$this->calculateHoliday('christmasDay', [], $christmasDay, false, false);
$this->calculateHoliday('secondChristmasDay', [], $boxingDay, false, false);

switch ($christmasDay->format('w')) {
case 0: // sunday
$christmasDay->add(new DateInterval('P2D'));
$this->calculateHoliday('christmasHoliday', ['en_AU' => 'Christmas Holiday'], $christmasDay, false, false);
$this->calculateHoliday('christmasHoliday', ['en' => 'Christmas Holiday'], $christmasDay, false, false);
break;
case 5: // friday
$boxingDay->add(new DateInterval('P2D'));
$this->calculateHoliday('secondChristmasHoliday', ['en_AU' => 'Boxing Day Holiday'], $boxingDay, false, false);
$this->calculateHoliday('secondChristmasHoliday', ['en' => 'Boxing Day Holiday'], $boxingDay, false, false);
break;
case 6: // saturday
$christmasDay->add(new DateInterval('P2D'));
$boxingDay->add(new DateInterval('P2D'));
$this->calculateHoliday('christmasHoliday', ['en_AU' => 'Christmas Holiday'], $christmasDay, false, false);
$this->calculateHoliday('secondChristmasHoliday', ['en_AU' => 'Boxing Day Holiday'], $boxingDay, false, false);
$this->calculateHoliday('christmasHoliday', ['en' => 'Christmas Holiday'], $christmasDay, false, false);
$this->calculateHoliday('secondChristmasHoliday', ['en' => 'Boxing Day Holiday'], $boxingDay, false, false);
break;
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/Yasumi/Provider/Australia/ACT.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private function easterSunday($year, $timezone, $locale, $type = null): Holiday
{
return new Holiday(
'easter',
['en_AU' => 'Easter Sunday'],
['en' => 'Easter Sunday'],
$this->calculateEaster($year, $timezone),
$locale,
$type ?? Holiday::TYPE_OFFICIAL
Expand Down Expand Up @@ -109,7 +109,7 @@ private function easterSaturday($year, $timezone, $locale, $type = null): Holida
{
return new Holiday(
'easterSaturday',
['en_AU' => 'Easter Saturday'],
['en' => 'Easter Saturday'],
$this->calculateEaster($year, $timezone)->sub(new DateInterval('P1D')),
$locale,
$type ?? Holiday::TYPE_OFFICIAL
Expand All @@ -135,7 +135,7 @@ private function calculateQueensBirthday(): void
{
$this->calculateHoliday(
'queensBirthday',
['en_AU' => "Queen's Birthday"],
['en' => "Queen's Birthday"],
new DateTime('second monday of june ' . $this->year, new DateTimeZone($this->timezone)),
false,
false
Expand All @@ -151,7 +151,7 @@ private function calculateLabourDay(): void
{
$date = new DateTime("first monday of october $this->year", new DateTimeZone($this->timezone));

$this->addHoliday(new Holiday('labourDay', ['en_AU' => 'Labour Day'], $date, $this->locale));
$this->addHoliday(new Holiday('labourDay', [], $date, $this->locale));
}

/**
Expand All @@ -166,7 +166,7 @@ private function calculateCanberraDay(): void
} else {
$date = new DateTime("second monday of march $this->year", new DateTimeZone($this->timezone));
}
$this->addHoliday(new Holiday('canberraDay', ['en_AU' => 'Canberra Day'], $date, $this->locale));
$this->addHoliday(new Holiday('canberraDay', ['en' => 'Canberra Day'], $date, $this->locale));
}

/**
Expand All @@ -185,6 +185,6 @@ private function calculateReconciliationDay(): void
if (1 !== $day) {
$date = $date->add(0 === $day ? new DateInterval('P1D') : new DateInterval('P' . (8 - $day) . 'D'));
}
$this->addHoliday(new Holiday('reconciliationDay', ['en_AU' => 'Reconciliation Day'], $date, $this->locale));
$this->addHoliday(new Holiday('reconciliationDay', ['en' => 'Reconciliation Day'], $date, $this->locale));
}
}
8 changes: 4 additions & 4 deletions src/Yasumi/Provider/Australia/NSW.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function initialize(): void
{
parent::initialize();

$this->addHoliday(new Holiday('easter', ['en_AU' => 'Easter Sunday'], $this->calculateEaster($this->year, $this->timezone), $this->locale));
$this->addHoliday(new Holiday('easter', [], $this->calculateEaster($this->year, $this->timezone), $this->locale));
$this->addHoliday($this->easterSaturday($this->year, $this->timezone, $this->locale));
$this->calculateQueensBirthday();
$this->calculateLabourDay();
Expand Down Expand Up @@ -76,7 +76,7 @@ private function easterSaturday($year, $timezone, $locale, $type = null): Holida
{
return new Holiday(
'easterSaturday',
['en_AU' => 'Easter Saturday'],
['en' => 'Easter Saturday'],
$this->calculateEaster($year, $timezone)->sub(new DateInterval('P1D')),
$locale,
$type ?? Holiday::TYPE_OFFICIAL
Expand All @@ -102,7 +102,7 @@ private function calculateQueensBirthday(): void
{
$this->calculateHoliday(
'queensBirthday',
['en_AU' => "Queen's Birthday"],
['en' => "Queen's Birthday"],
new DateTime('second monday of june ' . $this->year, new DateTimeZone($this->timezone)),
false,
false
Expand Down Expand Up @@ -131,7 +131,7 @@ private function calculateBankHoliday(): void
{
$this->calculateHoliday(
'bankHoliday',
['en_AU' => 'Bank Holiday'],
['en' => 'Bank Holiday'],
new DateTime('first monday of august ' . $this->year, new DateTimeZone($this->timezone)),
false,
false,
Expand Down
8 changes: 4 additions & 4 deletions src/Yasumi/Provider/Australia/NT.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private function easterSaturday($year, $timezone, $locale, $type = null): Holida
{
return new Holiday(
'easterSaturday',
['en_AU' => 'Easter Saturday'],
['en' => 'Easter Saturday'],
$this->calculateEaster($year, $timezone)->sub(new DateInterval('P1D')),
$locale,
$type ?? Holiday::TYPE_OFFICIAL
Expand All @@ -101,7 +101,7 @@ private function calculateQueensBirthday(): void
{
$this->calculateHoliday(
'queensBirthday',
['en_AU' => "Queen's Birthday"],
['en' => "Queen's Birthday"],
new DateTime('second monday of june ' . $this->year, new DateTimeZone($this->timezone)),
false,
false
Expand All @@ -117,7 +117,7 @@ private function calculateMayDay(): void
{
$date = new DateTime("first monday of may $this->year", new DateTimeZone($this->timezone));

$this->addHoliday(new Holiday('mayDay', ['en_AU' => 'May Day'], $date, $this->locale));
$this->addHoliday(new Holiday('mayDay', ['en' => 'May Day'], $date, $this->locale));
}

/**
Expand All @@ -132,7 +132,7 @@ private function calculatePicnicDay(): void
{
$this->calculateHoliday(
'picnicDay',
['en_AU' => 'Picnic Day'],
['en' => 'Picnic Day'],
new DateTime('first monday of august ' . $this->year, new DateTimeZone($this->timezone)),
false,
false
Expand Down
4 changes: 2 additions & 2 deletions src/Yasumi/Provider/Australia/Queensland.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ private function calculateQueensBirthday(): void
if ($this->year < 2012 || 2013 === $this->year || 2014 === $this->year || 2015 === $this->year) {
$this->calculateHoliday(
'queensBirthday',
['en_AU' => "Queen's Birthday"],
['en' => "Queen's Birthday"],
new DateTime('second monday of june ' . $this->year, new DateTimeZone($this->timezone)),
false,
false
);
} else {
$this->calculateHoliday(
'queensBirthday',
['en_AU' => "Queen's Birthday"],
['en' => "Queen's Birthday"],
new DateTime('first monday of october ' . $this->year, new DateTimeZone($this->timezone)),
false,
false
Expand Down
2 changes: 1 addition & 1 deletion src/Yasumi/Provider/Australia/Queensland/Brisbane.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ private function calculatePeoplesDay(): void
$date = $date->add(new DateInterval('P7D'));
}
$date = $date->add(new DateInterval('P5D'));
$this->addHoliday(new Holiday('peoplesDay', ['en_AU' => 'Ekka People\'s Day'], $date, $this->locale));
$this->addHoliday(new Holiday('peoplesDay', ['en' => 'Ekka People\'s Day'], $date, $this->locale));
}
}
24 changes: 12 additions & 12 deletions src/Yasumi/Provider/Australia/SA.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private function easterSaturday($year, $timezone, $locale, $type = null): Holida
{
return new Holiday(
'easterSaturday',
['en_AU' => 'Easter Saturday'],
['en' => 'Easter Saturday'],
$this->calculateEaster($year, $timezone)->sub(new DateInterval('P1D')),
$locale,
$type ?? Holiday::TYPE_OFFICIAL
Expand All @@ -109,7 +109,7 @@ private function calculateQueensBirthday(): void
{
$this->calculateHoliday(
'queensBirthday',
['en_AU' => "Queen's Birthday"],
['en' => "Queen's Birthday"],
new DateTime('second monday of june ' . $this->year, new DateTimeZone($this->timezone)),
false,
false
Expand All @@ -125,7 +125,7 @@ private function calculateLabourDay(): void
{
$date = new DateTime("first monday of october $this->year", new DateTimeZone($this->timezone));

$this->addHoliday(new Holiday('labourDay', ['en_AU' => 'Labour Day'], $date, $this->locale));
$this->addHoliday(new Holiday('labourDay', ['en' => 'Labour Day'], $date, $this->locale));
}

/**
Expand All @@ -142,15 +142,15 @@ private function calculateAdelaideCupDay(): void
if ($this->year < 2006) {
$this->calculateHoliday(
'adelaideCup',
['en_AU' => 'Adelaide Cup'],
['en' => 'Adelaide Cup'],
new DateTime('third monday of may ' . $this->year, new DateTimeZone($this->timezone)),
false,
false
);
} else {
$this->calculateHoliday(
'adelaideCup',
['en_AU' => 'Adelaide Cup'],
['en' => 'Adelaide Cup'],
new DateTime('second monday of march ' . $this->year, new DateTimeZone($this->timezone)),
false,
false
Expand All @@ -167,27 +167,27 @@ private function calculateAdelaideCupDay(): void
private function calculateProclamationDay(): void
{
$christmasDay = new DateTime("$this->year-12-25", new DateTimeZone($this->timezone));
$this->calculateHoliday('christmasDay', ['en_AU' => 'Christmas Day'], $christmasDay, false, false);
$this->calculateHoliday('christmasDay', [], $christmasDay, false, false);
switch ($christmasDay->format('w')) {
case 0: // sunday
$christmasDay->add(new DateInterval('P1D'));
$this->calculateHoliday('christmasHoliday', ['en_AU' => 'Christmas Holiday'], $christmasDay, false, false);
$this->calculateHoliday('christmasHoliday', ['en' => 'Christmas Holiday'], $christmasDay, false, false);
$proclamationDay = $christmasDay->add(new DateInterval('P1D'));
$this->calculateHoliday('proclamationDay', ['en_AU' => 'Proclamation Day'], $proclamationDay, false, false);
$this->calculateHoliday('proclamationDay', ['en' => 'Proclamation Day'], $proclamationDay, false, false);
break;
case 5: // friday
$proclamationDay = $christmasDay->add(new DateInterval('P3D'));
$this->calculateHoliday('proclamationDay', ['en_AU' => 'Proclamation Day'], $proclamationDay, false, false);
$this->calculateHoliday('proclamationDay', ['en' => 'Proclamation Day'], $proclamationDay, false, false);
break;
case 6: // saturday
$christmasDay->add(new DateInterval('P2D'));
$this->calculateHoliday('christmasHoliday', ['en_AU' => 'Christmas Holiday'], $christmasDay, false, false);
$this->calculateHoliday('christmasHoliday', ['en' => 'Christmas Holiday'], $christmasDay, false, false);
$proclamationDay = $christmasDay->add(new DateInterval('P1D'));
$this->calculateHoliday('proclamationDay', ['en_AU' => 'Proclamation Day'], $proclamationDay, false, false);
$this->calculateHoliday('proclamationDay', ['en' => 'Proclamation Day'], $proclamationDay, false, false);
break;
default: // monday-thursday
$proclamationDay = $christmasDay->add(new DateInterval('P1D'));
$this->calculateHoliday('proclamationDay', ['en_AU' => 'Proclamation Day'], $proclamationDay, false, false);
$this->calculateHoliday('proclamationDay', ['en' => 'Proclamation Day'], $proclamationDay, false, false);
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Yasumi/Provider/Australia/Tasmania.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private function calculateEightHoursDay(): void
{
$date = new DateTime("second monday of march $this->year", new DateTimeZone($this->timezone));

$this->addHoliday(new Holiday('eightHourDay', ['en_AU' => 'Eight Hour Day'], $date, $this->locale));
$this->addHoliday(new Holiday('eightHourDay', ['en' => 'Eight Hour Day'], $date, $this->locale));
}

/**
Expand All @@ -79,7 +79,7 @@ private function calculateQueensBirthday(): void
{
$this->calculateHoliday(
'queensBirthday',
['en_AU' => 'Queen\'s Birthday'],
['en' => 'Queen\'s Birthday'],
new DateTime('second monday of june ' . $this->year, new DateTimeZone($this->timezone)),
false,
false
Expand All @@ -98,7 +98,7 @@ private function calculateRecreationDay(): void
{
$this->calculateHoliday(
'recreationDay',
['en_AU' => 'Recreation Day'],
['en' => 'Recreation Day'],
new DateTime('first monday of november ' . $this->year, new DateTimeZone($this->timezone)),
false,
false
Expand Down
2 changes: 1 addition & 1 deletion src/Yasumi/Provider/Australia/Tasmania/CentralNorth.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ private function calculateDevonportShow(): void
{
$date = new DateTime($this->year . '-12-02', new DateTimeZone($this->timezone));
$date = $date->modify('previous friday');
$this->addHoliday(new Holiday('devonportShow', ['en_AU' => 'Devonport Show'], $date, $this->locale));
$this->addHoliday(new Holiday('devonportShow', ['en' => 'Devonport Show'], $date, $this->locale));
}
}
2 changes: 1 addition & 1 deletion src/Yasumi/Provider/Australia/Tasmania/FlindersIsland.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ private function calculateFlindersIslandShow(): void
{
$date = new DateTime('third saturday of october ' . $this->year, new DateTimeZone($this->timezone));
$date = $date->sub(new DateInterval('P1D'));
$this->addHoliday(new Holiday('flindersIslandShow', ['en_AU' => 'Flinders Island Show'], $date, $this->locale));
$this->addHoliday(new Holiday('flindersIslandShow', ['en' => 'Flinders Island Show'], $date, $this->locale));
}
}
2 changes: 1 addition & 1 deletion src/Yasumi/Provider/Australia/Tasmania/KingIsland.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private function calculateKingIslandShow(): void
{
$this->calculateHoliday(
'kingIslandShow',
['en_AU' => 'King Island Show'],
['en' => 'King Island Show'],
new DateTime('first tuesday of march ' . $this->year, new DateTimeZone($this->timezone)),
false,
false
Expand Down
Loading