diff --git a/README.md b/README.md index 9d1cbce..48056a1 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Cmixin/BusinessDay/HolidaysList.php b/src/Cmixin/BusinessDay/HolidaysList.php index d517148..bf5da14 100644 --- a/src/Cmixin/BusinessDay/HolidaysList.php +++ b/src/Cmixin/BusinessDay/HolidaysList.php @@ -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 */ @@ -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(); @@ -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.' @@ -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. * @@ -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); } }; diff --git a/tests/Cmixin/BusinessDayTest.php b/tests/Cmixin/BusinessDayTest.php index 1b29d47..f8ff058 100644 --- a/tests/Cmixin/BusinessDayTest.php +++ b/tests/Cmixin/BusinessDayTest.php @@ -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;