|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace MLL\Utils; |
| 4 | + |
| 5 | +use Carbon\Carbon; |
| 6 | + |
| 7 | +/** |
| 8 | + * Some definitions: |
| 9 | + * Holiday: special occasions on a mostly fixed date where there is no work |
| 10 | + * Weekend Day: Saturday and Sunday |
| 11 | + * Business Day: any day that is neither a Holiday nor a Weekend Day. |
| 12 | + */ |
| 13 | +class BavarianHolidays |
| 14 | +{ |
| 15 | + public const HOLIDAYS_STATIC = [ |
| 16 | + '01.01' => 'Neujahrstag', |
| 17 | + '06.01' => 'Heilige Drei Könige', |
| 18 | + '01.05' => 'Tag der Arbeit', |
| 19 | + '15.08' => 'Maria Himmelfahrt', |
| 20 | + '03.10' => 'Tag der Deutschen Einheit', |
| 21 | + '01.11' => 'Allerheiligen', |
| 22 | + '24.12' => 'Heilig Abend', |
| 23 | + '25.12' => 'Erster Weihnachtstag', |
| 24 | + '26.12' => 'Zweiter Weihnachtstag', |
| 25 | + '31.12' => 'Sylvester', |
| 26 | + ]; |
| 27 | + |
| 28 | + public const SAMSTAG = 'Samstag'; |
| 29 | + public const SONNTAG = 'Sonntag'; |
| 30 | + public const KARFREITAG = 'Karfreitag'; |
| 31 | + public const OSTERSONNTAG = 'Ostersonntag'; |
| 32 | + public const OSTERMONTAG = 'Ostermontag'; |
| 33 | + public const CHRISTI_HIMMELFAHRT = 'Christi Himmelfahrt'; |
| 34 | + public const PFINGSTSONNTAG = 'Pfingstsonntag'; |
| 35 | + public const PFINGSTMONTAG = 'Pfingstmontag'; |
| 36 | + public const FRONLEICHNAM = 'Fronleichnam'; |
| 37 | + public const REFORMATIONSTAG_500_JAHRE_REFORMATION = 'Reformationstag (500 Jahre Reformation)'; |
| 38 | + |
| 39 | + /** |
| 40 | + * Optionally allows users to define extra holidays for a given year. |
| 41 | + * |
| 42 | + * The returned array is expected to be a map from the day of the year |
| 43 | + * (format with @see self::dayOfTheYear()) to holiday names. |
| 44 | + * |
| 45 | + * @example ['23.02' => 'Day of the Tentacle'] |
| 46 | + * |
| 47 | + * @var (callable(int): array<string, string>)|null |
| 48 | + */ |
| 49 | + public static $loadUserDefinedHolidays; |
| 50 | + |
| 51 | + /** Checks if given date is a business day. */ |
| 52 | + public static function isBusinessDay(Carbon $date): bool |
| 53 | + { |
| 54 | + return ! self::isHoliday($date) |
| 55 | + && ! $date->isWeekend(); |
| 56 | + } |
| 57 | + |
| 58 | + /** Checks if given date is a holiday. */ |
| 59 | + public static function isHoliday(Carbon $date): bool |
| 60 | + { |
| 61 | + return is_string(self::nameHoliday($date)); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns the name of the holiday if the date happens to land on one. |
| 66 | + * Saturday and Sunday are not evaluated as holiday. |
| 67 | + */ |
| 68 | + public static function nameHoliday(Carbon $date): ?string |
| 69 | + { |
| 70 | + $holidayMap = self::buildHolidayMap($date); |
| 71 | + |
| 72 | + return $holidayMap[self::dayOfTheYear($date)] ?? null; |
| 73 | + } |
| 74 | + |
| 75 | + /** Returns a new carbon instance with the given number of business days added. */ |
| 76 | + public static function addBusinessDays(Carbon $date, int $days): Carbon |
| 77 | + { |
| 78 | + return DateModification::addDays( |
| 79 | + $date, |
| 80 | + $days, |
| 81 | + fn (Carbon $date): bool => self::isBusinessDay($date) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + /** Returns a new carbon instance with the given number of business days subtracted. */ |
| 86 | + public static function subBusinessDays(Carbon $date, int $days): Carbon |
| 87 | + { |
| 88 | + return DateModification::subDays( |
| 89 | + $date, |
| 90 | + $days, |
| 91 | + fn (Carbon $date): bool => self::isBusinessDay($date) |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Returns a map from day/month to named holidays. |
| 97 | + * |
| 98 | + * @return array<string, string> |
| 99 | + */ |
| 100 | + protected static function buildHolidayMap(Carbon $date): array |
| 101 | + { |
| 102 | + $holidays = self::HOLIDAYS_STATIC; |
| 103 | + |
| 104 | + $year = $date->year; |
| 105 | + |
| 106 | + // dynamic holidays |
| 107 | + // easter_days avoids issues with timezones and is not limited to UNIX timestamps, see https://github.com/briannesbitt/Carbon/pull/1052#issuecomment-381178494 |
| 108 | + $easter = Carbon::createMidnightDate($year, 3, 21) |
| 109 | + ->addDays(easter_days($year)); |
| 110 | + $holidays[self::dateFromEaster($easter, -2)] = self::KARFREITAG; |
| 111 | + $holidays[self::dateFromEaster($easter, 0)] = self::OSTERSONNTAG; |
| 112 | + $holidays[self::dateFromEaster($easter, 1)] = self::OSTERMONTAG; |
| 113 | + $holidays[self::dateFromEaster($easter, 39)] = self::CHRISTI_HIMMELFAHRT; |
| 114 | + $holidays[self::dateFromEaster($easter, 49)] = self::PFINGSTSONNTAG; |
| 115 | + $holidays[self::dateFromEaster($easter, 50)] = self::PFINGSTMONTAG; |
| 116 | + $holidays[self::dateFromEaster($easter, 60)] = self::FRONLEICHNAM; |
| 117 | + |
| 118 | + // exceptional holidays |
| 119 | + if ($year === 2017) { |
| 120 | + $holidays['31.10'] = self::REFORMATIONSTAG_500_JAHRE_REFORMATION; |
| 121 | + } |
| 122 | + |
| 123 | + // user-defined holidays |
| 124 | + if (isset(self::$loadUserDefinedHolidays)) { |
| 125 | + $holidays = array_merge( |
| 126 | + $holidays, |
| 127 | + (self::$loadUserDefinedHolidays)($year) |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + return $holidays; |
| 132 | + } |
| 133 | + |
| 134 | + protected static function dateFromEaster(Carbon $easter, int $daysAway): string |
| 135 | + { |
| 136 | + $date = $easter->clone()->addDays($daysAway); |
| 137 | + |
| 138 | + return self::dayOfTheYear($date); |
| 139 | + } |
| 140 | + |
| 141 | + public static function dayOfTheYear(Carbon $date): string |
| 142 | + { |
| 143 | + return $date->format('d.m'); |
| 144 | + } |
| 145 | +} |
0 commit comments