diff --git a/src/Holidays.php b/src/Holidays.php index 39d9b80a5..25f96ce4a 100755 --- a/src/Holidays.php +++ b/src/Holidays.php @@ -76,9 +76,23 @@ public function isHoliday(CarbonImmutable|string $date, string $countryCode): bo return false; } - public function getName(CarbonImmutable $date, Country $country): ?string + public function getName(CarbonImmutable|string $date, string $countryCode): ?string { - return null; // @todo implement + if (! $date instanceof CarbonImmutable) { + $date = CarbonImmutable::parse($date); + } + + $this + ->country($countryCode) + ->year($date->year); + + $holidays = $this->get(); + + if (in_array($date->format('d-m-Y'), array_column($holidays, 'date'), true)) { + return $holidays[array_search($date->format('d-m-Y'), array_column($holidays, 'date'), true)]['name']; + } + + return null; } protected function calculate(): self diff --git a/tests/HolidaysTest.php b/tests/HolidaysTest.php index 9ce328cff..5f54548eb 100644 --- a/tests/HolidaysTest.php +++ b/tests/HolidaysTest.php @@ -65,3 +65,19 @@ $result = Holidays::new()->isHoliday(CarbonImmutable::parse('2024-01-02'), 'be'); expect($result)->toBeFalse(); }); + +it('can see if a name is a holiday', function () { + $result = Holidays::new()->getName('2024-01-01', 'be'); + expect($result)->toBeTrue(); + + $result = Holidays::new()->isHoliday('2024-01-02', 'be'); + expect($result)->toBeFalse(); +}); + +it('can get the holiday name of a date', function () { + $result = Holidays::new()->getName(CarbonImmutable::parse('2024-01-01'), 'be'); + expect($result)->toBe('Nieuwjaar'); + + $result = Holidays::new()->getName(CarbonImmutable::parse('2024-01-02'), 'be'); + expect($result)->toBeNull(); +});