From 8f80ba24df0a1c6dd83775fe1361c9a233ffd5cd Mon Sep 17 00:00:00 2001 From: "John R. D'Orazio" Date: Fri, 3 Jan 2025 01:08:56 +0100 Subject: [PATCH] only allow to request a supported locale for a calendar --- php/index.php | 2 +- php/src/Utilities.php | 27 ++++++++++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/php/index.php b/php/index.php index d7fb32c..490b53e 100644 --- a/php/index.php +++ b/php/index.php @@ -114,7 +114,7 @@ if ($litSettings->Year >= 1970 && $litSettings->Year <= 9999) { $queryData = Utilities::prepareQueryData($litSettings); - $response = Utilities::sendAPIRequest($queryData); + $response = Utilities::sendAPIRequest($queryData, $metadata); $LitCalData = json_decode($response); if (json_last_error() !== JSON_ERROR_NONE) { diff --git a/php/src/Utilities.php b/php/src/Utilities.php index 15bb37d..0e85482 100644 --- a/php/src/Utilities.php +++ b/php/src/Utilities.php @@ -46,10 +46,31 @@ public static function retrieveMetadata() * @return string The response from the API request. * @throws Exception If the request fails or the HTTP response status is not 200. */ - public static function sendAPIRequest(array $queryData) + public static function sendAPIRequest(array $queryData, array $metadata) { $url = LITCAL_API_URL; $headers = ['Accept: application/json']; + if (isset($queryData["locale"])) { + if (isset($queryData["diocesan_calendar"])) { + $diocesanCalendarMetadata = array_values(array_filter($metadata['diocesan_calendars'], fn($calendar) => $queryData["diocesan_calendar"] === $calendar["calendar_id"]))[0]; + if (!in_array($queryData["locale"], $diocesanCalendarMetadata["locales"])) { + $queryData["locale"] = $diocesanCalendarMetadata["locales"][0]; + } + } + elseif (isset($queryData["national_calendar"])) { + $nationalCalendarMetadata = array_values(array_filter($metadata['national_calendars'], fn($calendar) => $queryData["national_calendar"] === $calendar["calendar_id"]))[0]; + if (!in_array($queryData["locale"], $nationalCalendarMetadata["locales"])) { + $queryData["locale"] = $nationalCalendarMetadata["locales"][0]; + } + } + else { + if (!in_array($queryData["locale"], $metadata['locales'])) { + $queryData["locale"] = $metadata['locales'][0]; + } + } + $headers[] = 'Accept-Language: ' . $queryData["locale"]; + unset($queryData["locale"]); + } if (isset($queryData["diocesan_calendar"])) { $url .= "/diocese/" . $queryData["diocesan_calendar"]; unset($queryData["diocesan_calendar"]); @@ -58,10 +79,6 @@ public static function sendAPIRequest(array $queryData) $url .= "/nation/" . $queryData["national_calendar"]; unset($queryData["national_calendar"]); } - if (isset($queryData["locale"])) { - $headers[] = 'Accept-Language: ' . $queryData["locale"]; - unset($queryData["locale"]); - } if (isset($queryData["year"])) { $url .= "/" . $queryData["year"]; unset($queryData["year"]);