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 accessors for shortName and substitutedHoliday #220

Merged
merged 3 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/) and this
- Added American English spelling for Labour Day [\#216](https://github.com/azuyalabs/yasumi/issues/216)
- Added French translation for Second Christmas Day [\#188](https://github.com/azuyalabs/yasumi/pull/188) ([Arkounay](https://github.com/Arkounay))

- Added accessor methods Holiday::getShortName() and SubstituteHoliday::getSubstitutedHoliday() [\#220](https://github.com/azuyalabs/yasumi/pull/220) ([c960657](https://github.com/c960657))
- Added missing return (correct) and parameter types in various methods.

### Changed
Expand Down Expand Up @@ -44,6 +45,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/) and this

- Fixed compound conditions that are always true by simplifying the condition steps.

### Deprecated
- Deprecated direct access to public properties Holiday::$shortName and SubstituteHoliday::$substitutedHoliday in favor of accessor methods [\#220](https://github.com/azuyalabs/yasumi/pull/220) ([c960657](https://github.com/c960657))

### Removed
- PHP 7.1 Support, as it has reached its end of life.
- Removed the assertion of the instance type in some functions as it is already defined by the return type.
Expand Down
4 changes: 2 additions & 2 deletions src/Yasumi/Filters/AbstractFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public function count(): int
{
$names = \array_map(static function ($holiday) {
if ($holiday instanceof SubstituteHoliday) {
return $holiday->substitutedHoliday->shortName;
return $holiday->getSubstitutedHoliday()->getShortName();
}

return $holiday->shortName;
return $holiday->getShortName();
}, \iterator_to_array($this));

return \count(\array_unique($names));
Expand Down
12 changes: 12 additions & 0 deletions src/Yasumi/Holiday.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class Holiday extends DateTime implements JsonSerializable

/**
* @var string short name (internal name) of this holiday
* @deprecated public access to this property is deprecated in favor of getShortName()
* @see getShortName()
*/
public $shortName;

Expand Down Expand Up @@ -138,6 +140,16 @@ public function __construct(
parent::__construct($date->format('Y-m-d'), $date->getTimezone());
}

/**
* Returns the short name for this holiday.
*
* @return string the short name, e.g. "newYearsDay".
*/
public function getShortName(): string
{
return $this->shortName;
}

/**
* Returns what type this holiday is.
*
Expand Down
6 changes: 3 additions & 3 deletions src/Yasumi/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function addHoliday(Holiday $holiday): void
$holiday->mergeGlobalTranslations($this->globalTranslations);
}

$this->holidays[$holiday->shortName] = $holiday;
$this->holidays[$holiday->getShortName()] = $holiday;
\uasort($this->holidays, [__CLASS__, 'compareDates']);
}

Expand Down Expand Up @@ -315,10 +315,10 @@ public function count(): int
{
$names = \array_map(static function ($holiday) {
if ($holiday instanceof SubstituteHoliday) {
return $holiday->substitutedHoliday->shortName;
return $holiday->getSubstitutedHoliday()->getShortName();
}

return $holiday->shortName;
return $holiday->getShortName();
}, $this->getHolidays());

return \count(\array_unique($names));
Expand Down
2 changes: 1 addition & 1 deletion src/Yasumi/Provider/SouthKorea.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ public function calculateSubstituteHolidays(): void
foreach ($holidays as $shortName => $holiday) {
// Get list of holiday dates except this
$holidayDates = \array_map(static function ($holiday) use ($shortName) {
return $holiday->shortName === $shortName ? false : (string)$holiday;
return $holiday->getShortName() === $shortName ? false : (string)$holiday;
}, $holidays);

// Only process accepted holidays and conditions
Expand Down
16 changes: 14 additions & 2 deletions src/Yasumi/SubstituteHoliday.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class SubstituteHoliday extends Holiday
{
/**
* @var Holiday
* @deprecated public access to this property is deprecated in favor of getSubstitutedHoliday()
* @see getSubstitutedHoliday()
*/
public $substitutedHoliday;

Expand Down Expand Up @@ -66,7 +68,7 @@ public function __construct(
) {
$this->substitutedHoliday = $substitutedHoliday;

$shortName = 'substituteHoliday:' . $substitutedHoliday->shortName;
$shortName = 'substituteHoliday:' . $substitutedHoliday->getShortName();

if ($date == $substitutedHoliday) {
throw new \InvalidArgumentException('Date must differ from the substituted holiday');
Expand All @@ -76,6 +78,16 @@ public function __construct(
parent::__construct($shortName, $names, $date, $displayLocale, $type);
}

/**
* Returns the holiday being substituted.
*
* @return Holiday the holiday being substituted.
*/
public function getSubstitutedHoliday(): Holiday
{
return $this->substitutedHoliday;
}

/**
* Returns the localized name of this holiday
*
Expand All @@ -95,7 +107,7 @@ public function getName($locales = null): string
{
$name = parent::getName();

if ($name === $this->shortName) {
if ($name === $this->getShortName()) {
foreach ($this->getLocales($locales) as $locales) {
$pattern = $this->substituteHolidayTranslations[$locales] ?? null;
if ($pattern) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Base/SubstituteHolidayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public function testConstructor(): void
$holiday = new Holiday('testHoliday', [], new DateTime('2019-01-01'), 'en_US', Holiday::TYPE_BANK);
$substitute = new SubstituteHoliday($holiday, [], new DateTime('2019-01-02'), 'en_US', Holiday::TYPE_SEASON);

$this->assertSame($holiday, $substitute->substitutedHoliday);
$this->assertEquals('substituteHoliday:testHoliday', $substitute->shortName);
$this->assertSame($holiday, $substitute->getSubstitutedHoliday());
$this->assertEquals('substituteHoliday:testHoliday', $substitute->getShortName());
$this->assertEquals(Holiday::TYPE_SEASON, $substitute->getType());
$this->assertEquals(new DateTime('2019-01-02'), $substitute);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Base/TypographyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function translationProvider(): array

foreach ($provider->getHolidays() as $holiday) {
foreach ($holiday->translations as $locale => $name) {
$tests[$name] = [$name, $class, $holiday->shortName, $locale];
$tests[$name] = [$name, $class, $holiday->getShortName(), $locale];
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Ukraine/SubstitutedHolidayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function assertHolidayWithSubstitution(
$this->assertTrue($holidays->isHoliday($holidayOfficial));
$this->assertEquals(Holiday::TYPE_OFFICIAL, $holidayOfficial->getType());

$holidaySubstitution = $holidays->getHoliday('substituteHoliday:' . $holidayOfficial->shortName);
$holidaySubstitution = $holidays->getHoliday('substituteHoliday:' . $holidayOfficial->getShortName());
if ($expectedSubstitution === null) {
// without substitution
$this->assertNull($holidaySubstitution);
Expand Down