generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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 <[email protected]>
- Loading branch information
Showing
4 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Spatie\Holidays\Countries; | ||
|
||
use DateTime; | ||
use DateTimeZone; | ||
use IntlDateFormatter; | ||
|
||
class Taiwan extends Country | ||
{ | ||
protected string $timezone = 'Asia/Taipei'; | ||
|
||
public function countryCode(): string | ||
{ | ||
return 'tw'; | ||
} | ||
|
||
protected function allHolidays(int $year): array | ||
{ | ||
return array_merge([ | ||
'元旦' => '01-01', | ||
'228和平紀念日' => '02-28', | ||
'兒童節' => '04-04', | ||
'雙十國慶' => '10-10', | ||
], $this->variableHolidays($year)); | ||
} | ||
|
||
/** @return array<string, string> */ | ||
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'); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
tests/.pest/snapshots/Countries/TaiwanTest/it_can_calculate_taiwan_holidays.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Spatie\Holidays\Tests\Countries; | ||
|
||
use Carbon\CarbonImmutable; | ||
use Spatie\Holidays\Holidays; | ||
|
||
it('can calculate taiwan holidays', function () { | ||
CarbonImmutable::setTestNowAndTimezone('2024-01-01'); | ||
|
||
$holidays = Holidays::for(country: 'tw')->get(); | ||
|
||
expect($holidays) | ||
->toBeArray() | ||
->not()->toBeEmpty(); | ||
|
||
expect(formatDates($holidays))->toMatchSnapshot(); | ||
|
||
}); |