From 9106de5cb44e4a4364bca1cfb9eaf14c63b405e1 Mon Sep 17 00:00:00 2001 From: HAMAD ABDULLA Date: Wed, 21 Feb 2024 12:01:40 +0300 Subject: [PATCH] Adding holidays for Bahrain (#206) * Adding holidays for Bahrain * Update Bahrain.php * Update tests/Countries/BahrainTest.php Co-authored-by: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> --------- Co-authored-by: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> --- lang/bahrain/ar/holidays.json | 16 ++ lang/bahrain/en/holidays.json | 16 ++ src/Countries/Bahrain.php | 212 ++++++++++++++++++ .../it_can_calculate_bahrain_holidays.snap | 62 +++++ tests/Countries/BahrainTest.php | 18 ++ 5 files changed, 324 insertions(+) create mode 100644 lang/bahrain/ar/holidays.json create mode 100644 lang/bahrain/en/holidays.json create mode 100644 src/Countries/Bahrain.php create mode 100644 tests/.pest/snapshots/Countries/BahrainTest/it_can_calculate_bahrain_holidays.snap create mode 100644 tests/Countries/BahrainTest.php diff --git a/lang/bahrain/ar/holidays.json b/lang/bahrain/ar/holidays.json new file mode 100644 index 000000000..a1fcc4b34 --- /dev/null +++ b/lang/bahrain/ar/holidays.json @@ -0,0 +1,16 @@ +{ + "New Year\\'s Day": "رأس السنة الميلادية", + "Labour Day": "عيد العمال", + "Eid al-Fitr": "عيد الفطر المبارك", + "Eid al-Fitr Day 2": "عيد الفطر المبارك", + "Eid al-Fitr Day 3": "عيد الفطر المبارك", + "Eid al-Adha": "عيد الأضحى المبارك", + "Eid al-Adha Day 2": "عيد الأضحى المبارك", + "Eid al-Adha Day 3": "عيد الأضحى المبارك", + "Islamic New Year": "رأس السنة الهجرية", + "Birthday of the Prophet Muhammad": "المولد النبوي الشريف", + "Arafat Day": "يوم عرفة", + "National Day": "اليوم الوطني", + "National Day 2": "اليوم الوطني", + "Arafat Day 2": "يوم عرفة", +} \ No newline at end of file diff --git a/lang/bahrain/en/holidays.json b/lang/bahrain/en/holidays.json new file mode 100644 index 000000000..a5be25ff9 --- /dev/null +++ b/lang/bahrain/en/holidays.json @@ -0,0 +1,16 @@ +{ + "New Year\\'s Day": "New Year\\'s Day", + "Labour Day": "Labour Day", + "Eid al-Fitr": "Eid al-Fitr", + "Eid al-Fitr Day 2": "Eid al-Fitr", + "Eid al-Fitr Day 3": "Eid al-Fitr", + "Eid al-Adha": "Eid al-Adha", + "Eid al-Adha Day 2": "Eid al-Adha", + "Eid al-Adha Day 3": "Eid al-Adha", + "Islamic New Year": "Islamic New Year", + "Birthday of the Prophet Muhammad": "Birthday of the Prophet Muhammad", + "Arafat Day": "Arafat Day", + "National Day": "National Day", + "National Day 2": "National Day", + "Arafat Day 2": "Arafat Day", +} diff --git a/src/Countries/Bahrain.php b/src/Countries/Bahrain.php new file mode 100644 index 000000000..71e66bbc8 --- /dev/null +++ b/src/Countries/Bahrain.php @@ -0,0 +1,212 @@ + '05-24', + 2021 => '05-13', + 2022 => '05-02', + 2023 => '04-20', + 2024 => '04-10', + 2025 => '03-31', + 2026 => '03-21', + 2027 => '03-10', + 2028 => '02-27', + 2029 => '02-15', + 2030 => '02-04', + 2031 => '01-25', + 2032 => '01-15', + 2033 => '01-03', + 2034 => '12-13', + 2035 => '12-02', + 2036 => '11-20', + 2037 => '11-09', + ]; + + protected const ARAFAT_DAY_HOLIDAYS = [ + 2020 => '07-30', + 2021 => '07-19', + 2022 => '07-09', + 2023 => '06-27', + 2024 => '06-16', + 2025 => '06-06', + 2026 => '05-26', + 2027 => '05-16', + 2028 => '05-05', + 2029 => '04-24', + 2030 => '04-13', + 2031 => '04-02', + 2032 => '03-21', + 2033 => '03-11', + 2034 => '03-01', + 2035 => '02-18', + 2036 => '02-07', + 2037 => '01-26', + ]; + + protected const EID_AL_ADHA_HOLIDAYS = [ + 2020 => '07-31', + 2021 => '07-20', + 2022 => '07-09', + 2023 => '06-28', + 2024 => '06-17', + 2025 => '06-07', + 2026 => '05-27', + 2027 => '05-17', + 2028 => '05-06', + 2029 => '04-25', + 2030 => '04-14', + 2031 => '04-03', + 2032 => '03-22', + 2033 => '03-12', + 2034 => '03-02', + 2035 => '02-19', + 2036 => '02-08', + 2037 => '01-27', + ]; + + protected const ISLAMIC_NEW_YEAR_HOLIDAYS = [ + 2020 => '08-20', + 2021 => '08-09', + 2022 => '07-30', + 2023 => '07-19', + 2024 => '07-08', + 2025 => '06-06', + 2026 => '06-17', + 2027 => '06-07', + 2028 => '05-26', + 2029 => '05-15', + 2030 => '05-05', + 2031 => '04-24', + 2032 => '04-12', + 2033 => '04-01', + 2034 => '03-22', + 2035 => '03-12', + 2036 => '02-29', + 2037 => '02-17', + ]; + + protected const ASHURA_HOLIDAYS = [ + 2020 => '08-30', + 2021 => '08-19', + 2022 => '08-08', + 2023 => '07-28', + 2024 => '07-17', + 2025 => '07-07', + 2026 => '06-26', + 2027 => '06-15', + 2028 => '06-04', + 2029 => '05-24', + 2030 => '05-13', + 2031 => '05-02', + 2032 => '04-20', + 2033 => '04-10', + 2034 => '03-30', + 2035 => '03-19', + 2036 => '03-08', + 2037 => '02-25', + ]; + + protected const PROPHET_MUHAMMAD_BIRTHDAY_HOLIDAYS = [ + 2020 => '10-29', + 2021 => '10-21', + 2022 => '10-08', + 2023 => '09-28', + 2024 => '09-16', + 2025 => '09-06', + 2026 => '08-26', + 2027 => '08-15', + 2028 => '08-04', + 2029 => '07-25', + 2030 => '07-14', + 2031 => '07-03', + 2032 => '06-21', + 2033 => '06-10', + 2034 => '05-31', + 2035 => '05-21', + 2036 => '05-09', + 2037 => '04-29', + ]; + + public function countryCode(): string + { + return 'bh'; + } + + protected function allHolidays(int $year): array + { + $variableHolidays = $this->variableHolidays($year); + + return array_merge([ + 'New Year\'s Day' => '1-1', + 'Labour Day' => '5-1', + 'National Day' => '12-16', + 'National Day 2' => '12-17', + ], $variableHolidays); + } + + /** + * @return array + */ +protected function variableHolidays(int $year): array +{ + $holidays = [ + ['EID_AL_FITR_HOLIDAYS', 'Eid al-Fitr', 3], + ['EID_AL_ADHA_HOLIDAYS', 'Eid al-Adha', 3], + ['ARAFAT_DAY_HOLIDAYS', 'Arafat Day'], + ['ISLAMIC_NEW_YEAR_HOLIDAYS', 'Islamic New Year'], + ['ASHURA_HOLIDAYS', 'Ashura', 2], + ['PROPHET_MUHAMMAD_BIRTHDAY_HOLIDAYS', 'Birthday of the Prophet Muhammad'] + ]; + + $dates = []; + foreach ($holidays as $holiday) { + $dates = array_merge($dates, $this->getIslamicHolidayDatesForYear(constant('self::' . $holiday[0]), $year, $holiday[1], $holiday[2] ?? 1)); + } + + return $dates; +} + + /** + * Prepare holiday dates for the given year. + * + * @param array $holidayDates Array mapping years to dates. + * @param int $year The year for which to prepare holiday dates. + * @param string $holidayName The name of the holiday. + * @param int $duration The duration of the holiday in days. + * @return array An array of holiday dates. + */ + private function getIslamicHolidayDatesForYear(array $holidayDates, int $year, string $holidayName, int $duration = 1): array + { + $dates = []; + + if ($year < 2020) { + throw InvalidYear::yearTooLow(2020); + } + + if (! isset($holidayDates[$year])) { + return $dates; + } + + $startDay = CarbonImmutable::createFromFormat('Y-m-d', sprintf('%s-%s', $year, $holidayDates[$year])); + + if ($duration === 1) { + // For single-day holidays, use the holiday name without "Day" + $dates[$holidayName] = $startDay; + } else { + // For multi-day holidays, append "Day N" for the second day onwards + for ($i = 0; $i < $duration; $i++) { + $dayLabel = $i === 0 ? $holidayName : sprintf('%s Day %d', $holidayName, $i + 1); + $dates[$dayLabel] = $startDay->addDays($i); + } + } + + return $dates; + } +} diff --git a/tests/.pest/snapshots/Countries/BahrainTest/it_can_calculate_bahrain_holidays.snap b/tests/.pest/snapshots/Countries/BahrainTest/it_can_calculate_bahrain_holidays.snap new file mode 100644 index 000000000..457cb19b2 --- /dev/null +++ b/tests/.pest/snapshots/Countries/BahrainTest/it_can_calculate_bahrain_holidays.snap @@ -0,0 +1,62 @@ +[ + { + "name": "New Year's Day", + "date": "2024-01-01" + }, + { + "name": "Eid al-Fitr", + "date": "2024-04-10" + }, + { + "name": "Eid al-Fitr Day 2", + "date": "2024-04-11" + }, + { + "name": "Eid al-Fitr Day 3", + "date": "2024-04-12" + }, + { + "name": "Labour Day", + "date": "2024-05-01" + }, + { + "name": "Arafat Day", + "date": "2024-06-16" + }, + { + "name": "Eid al-Adha", + "date": "2024-06-17" + }, + { + "name": "Eid al-Adha Day 2", + "date": "2024-06-18" + }, + { + "name": "Eid al-Adha Day 3", + "date": "2024-06-19" + }, + { + "name": "Islamic New Year", + "date": "2024-07-08" + }, + { + "name": "Ashura", + "date": "2024-07-17" + }, + { + "name": "Ashura Day 2", + "date": "2024-07-18" + }, + { + "name": "Birthday of the Prophet Muhammad", + "date": "2024-09-16" + }, + { + "name": "National Day", + "date": "2024-12-16" + }, + { + "name": "National Day 2", + "date": "2024-12-17" + } +] \ No newline at end of file diff --git a/tests/Countries/BahrainTest.php b/tests/Countries/BahrainTest.php new file mode 100644 index 000000000..b8a7c458a --- /dev/null +++ b/tests/Countries/BahrainTest.php @@ -0,0 +1,18 @@ +get(); + + expect($holidays) + ->toBeArray() + ->not()->toBeEmpty(); + + expect(formatDates($holidays))->toMatchSnapshot(); +});