From 24f36c16d0be1e4ddb8364c1b63b36231bd1a80a Mon Sep 17 00:00:00 2001 From: Ondrej Vlach Date: Wed, 7 Aug 2024 12:54:55 +0200 Subject: [PATCH] feat(services): add generator for public holidays in cz --- .../PublicHolidaysCzechGenerator.php | 57 +++++++++++++++++++ .../Generator/PublicHolidaysGenerator.php | 18 ++++++ .../PublicHolidaysGeneratorFactory.php | 22 +++++++ .../PublicHolidaysCzechGeneratorTest.php | 33 +++++++++++ .../PublicHolidaysGeneratorFactoryTest.php | 25 ++++++++ 5 files changed, 155 insertions(+) create mode 100644 app/Services/WorkingDays/PublicHolidays/Generator/PublicHolidaysCzechGenerator.php create mode 100644 app/Services/WorkingDays/PublicHolidays/Generator/PublicHolidaysGenerator.php create mode 100644 app/Services/WorkingDays/PublicHolidays/PublicHolidaysGeneratorFactory.php create mode 100644 tests/Unit/Services/WorkingDays/PublicHolidays/Generator/PublicHolidaysCzechGeneratorTest.php create mode 100644 tests/Unit/Services/WorkingDays/PublicHolidaysGeneratorFactoryTest.php diff --git a/app/Services/WorkingDays/PublicHolidays/Generator/PublicHolidaysCzechGenerator.php b/app/Services/WorkingDays/PublicHolidays/Generator/PublicHolidaysCzechGenerator.php new file mode 100644 index 0000000..a65133c --- /dev/null +++ b/app/Services/WorkingDays/PublicHolidays/Generator/PublicHolidaysCzechGenerator.php @@ -0,0 +1,57 @@ +getEasterDateTime($year); + $result[] = $easters->add(new \DateInterval("P1D")); // easter Monday + $result[] = $easters->sub(new \DateInterval("P2D")); // easter Friday + + array_multisort($result, SORT_ASC); + + return $result; + } + + private function getEasterDateTime(int $year): \DateTimeImmutable + { + $base = new \DateTime("$year-03-21"); + $days = \easter_days($year); + + return \DateTimeImmutable::createFromInterface($base->add(new \DateInterval("P{$days}D"))); + } +} diff --git a/app/Services/WorkingDays/PublicHolidays/Generator/PublicHolidaysGenerator.php b/app/Services/WorkingDays/PublicHolidays/Generator/PublicHolidaysGenerator.php new file mode 100644 index 0000000..34e2b01 --- /dev/null +++ b/app/Services/WorkingDays/PublicHolidays/Generator/PublicHolidaysGenerator.php @@ -0,0 +1,18 @@ + new PublicHolidaysCzechGenerator(), + default => throw new \RuntimeException('Unsupported country code'), + }; + } +} diff --git a/tests/Unit/Services/WorkingDays/PublicHolidays/Generator/PublicHolidaysCzechGeneratorTest.php b/tests/Unit/Services/WorkingDays/PublicHolidays/Generator/PublicHolidaysCzechGeneratorTest.php new file mode 100644 index 0000000..d87903d --- /dev/null +++ b/tests/Unit/Services/WorkingDays/PublicHolidays/Generator/PublicHolidaysCzechGeneratorTest.php @@ -0,0 +1,33 @@ +generatePublicHolidaysForYear(2025); + $this->assertEquals([ + new \DateTimeImmutable("2025-01-01"), + new \DateTimeImmutable("2025-04-18"), + new \DateTimeImmutable("2025-04-21"), + new \DateTimeImmutable("2025-05-01"), + new \DateTimeImmutable("2025-05-08"), + new \DateTimeImmutable("2025-07-05"), + new \DateTimeImmutable("2025-07-06"), + new \DateTimeImmutable("2025-09-28"), + new \DateTimeImmutable("2025-10-28"), + new \DateTimeImmutable("2025-11-17"), + new \DateTimeImmutable("2025-12-24"), + new \DateTimeImmutable("2025-12-25"), + new \DateTimeImmutable("2025-12-26"), + ], $czechNWD); + } +} diff --git a/tests/Unit/Services/WorkingDays/PublicHolidaysGeneratorFactoryTest.php b/tests/Unit/Services/WorkingDays/PublicHolidaysGeneratorFactoryTest.php new file mode 100644 index 0000000..0f624d5 --- /dev/null +++ b/tests/Unit/Services/WorkingDays/PublicHolidaysGeneratorFactoryTest.php @@ -0,0 +1,25 @@ +assertInstanceOf(PublicHolidaysCzechGenerator::class, $factory->createPublicHolidaysGeneratorForCountry('CZ')); + } + + public function testRuntimeExceptionGeneratedForUnknownState(): void + { + $factory = new PublicHolidaysGeneratorFactory(); + $this->expectException(\RuntimeException::class); + $factory->createPublicHolidaysGeneratorForCountry('XZ'); + } +}