Skip to content

Commit

Permalink
Implement setHolidayName method
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Nov 5, 2018
1 parent e39f082 commit 6071799
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 26 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ Carbon::parse('2018-01-15')->getHolidayName() // "Christmas"
Carbon::parse('2018-01-15')->locale('sl')->getHolidayName() // "Božič"
```

#### setHolidayName

Wanna rename a holiday name in a particular language? No problem:

```php
Carbon::parse('2018-12-25')->getHolidayName() // "Christmas"
Carbon::setHolidayName('christmas', 'en', 'Christmas Day');
Carbon::parse('2018-12-25')->getHolidayName() // "Christmas Day"
```

#### isBusinessDay

Returns `true` if the date (Carbon instance) is nor a week-end day neither
Expand Down
86 changes: 60 additions & 26 deletions src/Cmixin/BusinessDay/HolidaysList.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function initializeHolidaysRegion($region = null)
}

/**
* Add holidays to the holidays list.
* Push a holiday to the holidays list of a region.
*
* @return \Closure
*/
Expand All @@ -129,31 +129,50 @@ public function pushHoliday()
}

/**
* Add holidays to the holidays list.
* Set the name(s) of a holiday.
*
* @return \Closure
*/
public function addHoliday()
public function setHolidayName()
{
$mixin = $this;

return function ($region, $holiday, $key = null, $name = null, $observed = null) use ($mixin) {
static $dictionary, $observer;

$mixin->initializeHolidaysRegion($region);
$push = $mixin->pushHoliday();
$push($region, $holiday, $key);
return function ($holidayKey = null, $name = null, $value = null) use ($mixin) {
static $dictionary;

if (isset($name) && $mixin instanceof Holiday) {
if (($name = is_string($name) ? array($name => $value) : $name) && $mixin instanceof Holiday) {
if (!isset($dictionary)) {
$dictionary = $mixin->getHolidayNamesDictionary();
}
foreach ($name as $language => $text) {
$dictionary($language);
$mixin->holidayNames[$language][$key] = $text;
$mixin->holidayNames[$language][$holidayKey] = $text;
}
}

return isset($this) ? $this : null;
};
}

/**
* Add a holiday to the holidays list of a region then init name and observed state.
*
* @return \Closure
*/
public function addHoliday()
{
$mixin = $this;
$dictionary = $this->setHolidayName();

return function ($region, $holiday, $key = null, $name = null, $observed = null) use ($mixin, $dictionary) {
static $observer;

$mixin->initializeHolidaysRegion($region);
$push = $mixin->pushHoliday();
$push($region, $holiday, $key);

$dictionary($key, $name);

if (isset($observed) && $mixin instanceof HolidayObserver) {
if (!isset($observer)) {
$observer = $mixin->setHolidayObserveStatus();
Expand All @@ -166,15 +185,13 @@ public function addHoliday()
}

/**
* Add holidays to the holidays list.
* Unpack a holiday array definition.
*
* @return \Closure
*/
public function unpackHoliday()
{
$mixin = $this;

return function (&$holiday, &$name = null, &$observed = null) use ($mixin) {
return function (&$holiday, &$name = null, &$observed = null) {
if (!isset($holiday['date'])) {
throw new \InvalidArgumentException(
'Holiday array definition should at least contains a "date" entry.'
Expand All @@ -195,6 +212,32 @@ public function unpackHoliday()
};
}

/**
* Check a holiday definition and unpack it if it's an array.
*
* @return \Closure
*/
public function checkHoliday()
{
$mixin = $this;

return function (&$holiday, $key, &$name = null, &$observed = null) use ($mixin) {
$unpack = $mixin->unpackHoliday();

if (is_array($holiday)) {
if (is_int($key)) {
throw new \InvalidArgumentException(
'Holiday array definition need a string identifier as main array key.'
);
}

$unpack($holiday, $name, $observed);
}

return $holiday;
};
}

/**
* Add holidays to the holidays list.
*
Expand All @@ -207,21 +250,12 @@ public function addHolidays()
return function ($region, $holidays) use ($mixin) {
$mixin->initializeHolidaysRegion($region);
$add = $mixin->addHoliday();
$unpack = $mixin->unpackHoliday();
$check = $mixin->checkHoliday();

foreach ($holidays as $key => $holiday) {
$name = null;
$observed = null;
if (is_array($holiday)) {
if (is_int($key)) {
throw new \InvalidArgumentException(
'Holiday array definition need a string identifier as main array key.'
);
}

$unpack($holiday, $name, $observed);
}

$check($holiday, $key, $name, $observed);
$add($region, $holiday, $key, $name, $observed);
}
};
Expand Down
10 changes: 10 additions & 0 deletions tests/Cmixin/BusinessDayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ public function testAddHolidaysArray()
self::assertSame('Unknown', $carbon::parse('2010-11-15 03:30:40')->getHolidayName('nl'));
}

public function testSetHolidayName()
{
$carbon = static::CARBON_CLASS;
$carbon::setHolidaysRegion('fr-national');
self::assertSame('Christmas', $carbon::parse('2018-12-25')->getHolidayName());
$carbon::setHolidayName('christmas', 'en', 'Christmas Day');
self::assertSame('Christmas Day', $carbon::parse('2018-12-25')->getHolidayName());
self::assertSame('Noël', $carbon::parse('2018-12-25')->getHolidayName('fr'));
}

public function testIsHolidayStatic()
{
$carbon = static::CARBON_CLASS;
Expand Down

0 comments on commit 6071799

Please sign in to comment.