From 3b1c0bd9384bef35a050e75f7345a2808323934f Mon Sep 17 00:00:00 2001 From: Takeshi Yu Date: Sat, 27 Jan 2024 18:57:08 +0800 Subject: [PATCH] Support for Taiwan holidays (#124) * support Taiwan holidays * Refactor lunarCalendar method to handle parsing errors * Update variableHolidays to correctly return converted dates * Refactor variableHolidays to combine array_map and array_filter * use the Taiwanese language to label the holiday --------- Co-authored-by: Takeshi Yu --- composer.json | 4 +- src/Countries/Taiwan.php | 63 +++++++++++++++++++ .../it_can_calculate_taiwan_holidays.snap | 38 +++++++++++ tests/Countries/TaiwanTest.php | 19 ++++++ 4 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 src/Countries/Taiwan.php create mode 100644 tests/.pest/snapshots/Countries/TaiwanTest/it_can_calculate_taiwan_holidays.snap create mode 100644 tests/Countries/TaiwanTest.php diff --git a/composer.json b/composer.json index 8e0f585dc..d51ff2f1e 100644 --- a/composer.json +++ b/composer.json @@ -21,8 +21,8 @@ ], "require": { "php": "^8.1", - "nesbot/carbon": "^2.72.1", - "ext-calendar": "*" + "ext-intl": "*", + "nesbot/carbon": "^2.72.1" }, "require-dev": { "laravel/pint": "^1.0", diff --git a/src/Countries/Taiwan.php b/src/Countries/Taiwan.php new file mode 100644 index 000000000..804c06424 --- /dev/null +++ b/src/Countries/Taiwan.php @@ -0,0 +1,63 @@ + '01-01', + '228和平紀念日' => '02-28', + '兒童節' => '04-04', + '雙十國慶' => '10-10', + ], $this->variableHolidays($year)); + } + + /** @return array */ + protected function variableHolidays(int $year): array + { + return array_filter(array_map(fn ($date) => $this->lunarCalendar($date, $year), [ + '農曆春節-正月初一' => '01-01', + '農曆春節-正月初二' => '01-02', + '農曆春節-正月初三' => '01-03', + '端午節' => '05-05', + '中秋節' => '08-15', + ])); + } + + protected function lunarCalendar(string $input, int $year): ?string + { + $formatter = new IntlDateFormatter( + locale: 'zh-TW@calendar=chinese', + dateType: IntlDateFormatter::SHORT, + timeType: IntlDateFormatter::NONE, + timezone: $this->timezone, + calendar: IntlDateFormatter::TRADITIONAL + ); + + $lunarDateStr = $year . '-' . $input; + $parsedTimestamp = $formatter->parse($lunarDateStr); + + if ($parsedTimestamp === false) { + return null; + } + + $dateTime = new DateTime; + $dateTime->setTimestamp($parsedTimestamp); + $dateTime->setTimezone(new DateTimeZone($this->timezone)); + + return $dateTime->format('m-d'); + } +} diff --git a/tests/.pest/snapshots/Countries/TaiwanTest/it_can_calculate_taiwan_holidays.snap b/tests/.pest/snapshots/Countries/TaiwanTest/it_can_calculate_taiwan_holidays.snap new file mode 100644 index 000000000..637abf2c6 --- /dev/null +++ b/tests/.pest/snapshots/Countries/TaiwanTest/it_can_calculate_taiwan_holidays.snap @@ -0,0 +1,38 @@ +[ + { + "name": "\u5143\u65e6", + "date": "2024-01-01" + }, + { + "name": "\u8fb2\u66c6\u6625\u7bc0-\u6b63\u6708\u521d\u4e00", + "date": "2024-02-10" + }, + { + "name": "\u8fb2\u66c6\u6625\u7bc0-\u6b63\u6708\u521d\u4e8c", + "date": "2024-02-11" + }, + { + "name": "\u8fb2\u66c6\u6625\u7bc0-\u6b63\u6708\u521d\u4e09", + "date": "2024-02-12" + }, + { + "name": "228\u548c\u5e73\u7d00\u5ff5\u65e5", + "date": "2024-02-28" + }, + { + "name": "\u5152\u7ae5\u7bc0", + "date": "2024-04-04" + }, + { + "name": "\u7aef\u5348\u7bc0", + "date": "2024-06-10" + }, + { + "name": "\u4e2d\u79cb\u7bc0", + "date": "2024-09-17" + }, + { + "name": "\u96d9\u5341\u570b\u6176", + "date": "2024-10-10" + } +] \ No newline at end of file diff --git a/tests/Countries/TaiwanTest.php b/tests/Countries/TaiwanTest.php new file mode 100644 index 000000000..75c7b6906 --- /dev/null +++ b/tests/Countries/TaiwanTest.php @@ -0,0 +1,19 @@ +get(); + + expect($holidays) + ->toBeArray() + ->not()->toBeEmpty(); + + expect(formatDates($holidays))->toMatchSnapshot(); + +});