diff --git a/php/PublicHolidayCalendar.php b/php/PublicHolidayCalendar.php index 431c280..e853f1a 100644 --- a/php/PublicHolidayCalendar.php +++ b/php/PublicHolidayCalendar.php @@ -30,23 +30,32 @@ class PublicHolidayCalendar { * - YearStart: int The start year of the calendar (with century). Default : current year * - YearNumber: int The number of year fo the calendar. Default : 5 * - WeeksNumbers: bool (0/1) Include weeks number ? Default : 0 + * - debug: bool(0/1) * * All times are UTC * * @return void */ #[NoReturn] public function proceed (): void { + $debug = !isset($_GET['debug']) || $_GET['debug'] == '' ? 0 : $_GET['debug']; + if (preg_match('#^\s*(?[01])?\s*$#i', $debug, $match) !== 1) { + $debug = false; + } + else { + $debug = isset($match['bool']) && $match['bool']; + } + try { //region Extract arguments //region YearStart - $yearStart = $_GET['YearStart'] == '' ? (new DateTimeImmutable('now'))->format('Y') : $_GET['YearStart']; + $yearStart = !isset($_GET['YearStart']) || $_GET['YearStart'] == '' ? (new DateTimeImmutable('now'))->format('Y') : $_GET['YearStart']; if (!is_numeric($yearStart)) { throw new UnexpectedValueException('The "YearStart" argument must be an integer'); } $yearStart = intval($yearStart); //endregion //region YearNumber - $yearNumber = $_GET['YearNumber'] == '' ? 5 : $_GET['YearNumber']; + $yearNumber = !isset($_GET['YearNumber']) || $_GET['YearNumber'] == '' ? 5 : $_GET['YearNumber']; if (!is_numeric($yearNumber)) { throw new UnexpectedValueException('The "YearNumber" argument must be a strictly positive integer'); } @@ -56,17 +65,17 @@ class PublicHolidayCalendar { } //endregion //region WeeksNumbers - $weeksNumbers = $_GET['WeeksNumbers'] == '' ? 0 : $_GET['WeeksNumbers']; + $weeksNumbers = !isset($_GET['WeeksNumbers']) || $_GET['WeeksNumbers'] == '' ? 0 : $_GET['WeeksNumbers']; if (preg_match('#^\s*(?[01])?\s*$#i', $weeksNumbers, $match) !== 1) { throw new UnexpectedValueException('The "WeeksNumber" argument must be a boolean (0 or 1)'); } - $weeksNumbers = isset($match['bool']) && $match['false']; + $weeksNumbers = isset($match['bool']) && $match['bool']; //endregion //endregion //region Create the calendar $calendar = new Calendar(); - $calendar->addTimeZone(TimeZone::createFromPhpDateTimeZone(new DateTimeZone(DateTimeZone::UTC))); + $calendar->addTimeZone(TimeZone::createFromPhpDateTimeZone(new DateTimeZone('UTC'))); $yearEnd = $yearStart + $yearNumber; for ($yearCurrent = $yearStart; $yearCurrent <= $yearEnd; $yearCurrent++) { @@ -81,16 +90,25 @@ class PublicHolidayCalendar { $icalFactory = new CalendarFactory(); $icalContent = (string)$icalFactory->createCalendar($calendar); - header('Content-Type: text/calendar; charset=UTF-8', true, 200); - header('Content-Length: ' . mb_strlen($icalContent)); - header('Content-Disposition: attachment; filename=PublicHolidayCalendar.ics'); + if ($debug) { + header('Content-Type: text/plain; charset=UTF-8', true, 200); + } + else { + header('Content-Type: text/calendar; charset=UTF-8', true, 200); + header('Content-Length: ' . strlen($icalContent)); // Don't use mb_strlen because we NEED the number of bytes + header('Content-Disposition: attachment; filename=PublicHolidayCalendar.ics'); + } echo $icalContent; exit(0); //endregion } catch (Throwable $e) { header('Content-Type: text/plain; charset=UTF-8', true, 500); - echo $e->getMessage(); + echo 'Exception [' . get_class($e) . ']: ' . $e->getMessage() . PHP_EOL; + if ($debug) { + echo $e->getTraceAsString() . PHP_EOL; + } + exit(1); } } @@ -182,7 +200,7 @@ class PublicHolidayCalendar { $currentDayOfWeek = (int)$currentDay->format('N'); if ($currentDayOfWeek > 1) { try { - $currentDay = $currentDay->sub(new DateInterval('P' . (8 - $currentDayOfWeek) . 'D')); + $currentDay = $currentDay->add(new DateInterval('P' . (8 - $currentDayOfWeek) . 'D')); } catch (Exception) { } @@ -192,7 +210,7 @@ class PublicHolidayCalendar { while ($currentDay < $lastDay) { $events[] = static::createEvent( 'Semaine ' . (count($events) + 1), - 'Semaine n° ' . (count($events) + 1), + 'Semaine ' . (count($events) + 1), false, $currentDay ); @@ -313,7 +331,7 @@ class PublicHolidayCalendar { private static function createEvent (string $summary, string $description, bool $publicHoliday, DateTimeInterface $date): Event { return (new Event(new UniqueIdentifier(hash('sha256', $date->format('Y-m-d'))))) ->setSummary($summary) - ->setDescription($description . ($publicHoliday ? ' - Férié' : '')) + ->setDescription($description . ' ' . $date->format('Y') . ($publicHoliday ? ' - Férié' : '')) ->setOccurrence(new SingleDay(new Date($date))); }