Skip to content

Commit

Permalink
only allow to request a supported locale for a calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRDOrazio committed Jan 3, 2025
1 parent 77bcb57 commit 8f80ba2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion php/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 22 additions & 5 deletions php/src/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
Expand All @@ -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"]);
Expand Down

0 comments on commit 8f80ba2

Please sign in to comment.