Skip to content

Commit 8517d8c

Browse files
committed
simplify GET / POST request parameter initialization
1 parent ce2e800 commit 8517d8c

File tree

1 file changed

+66
-92
lines changed

1 file changed

+66
-92
lines changed

src/Paths/Missals.php

+66-92
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ private static function initPayloadFromRequestBody(): ?object
6363
default:
6464
if (in_array(self::$Core->getRequestMethod(), [RequestMethod::PUT, RequestMethod::PATCH])) {
6565
// the payload MUST be in the body of the request, either JSON encoded or YAML encoded
66-
self::produceErrorResponse(StatusCode::BAD_REQUEST, "Expected payload in body of request, either JSON encoded or YAML encoded");
66+
self::produceErrorResponse(StatusCode::BAD_REQUEST, "Expected payload in body of request, either JSON or YAML encoded");
6767
}
6868
}
6969
return $payload;
7070
}
7171

7272
/**
73-
* Handles the POST request payload for the Missals endpoint.
73+
* Handles the GET and POST request payload for the Missals endpoint.
7474
*
7575
* If the request body is a JSON or YAML encoded object, it will attempt to
7676
* retrieve the locale, region, and year from the object.
@@ -89,54 +89,28 @@ private static function initPayloadFromRequestBody(): ?object
8989
* request body was not a JSON or YAML encoded object
9090
* @return array the initialized request parameters
9191
*/
92-
private static function handlePostPayload(?object $payload): array
92+
private static function initPayload(?object $payload): array
9393
{
9494
$data = [];
9595
if ($payload !== null && property_exists($payload, 'locale')) {
96-
$data["LOCALE"] = $payload->locale;
96+
$data["locale"] = $payload->locale;
9797
} elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
9898
$locale = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
9999
if ($locale && LitLocale::isValid($locale)) {
100-
$data["LOCALE"] = $locale;
100+
$data["locale"] = $locale;
101101
} else {
102-
$data["LOCALE"] = LitLocale::LATIN;
102+
$data["locale"] = LitLocale::LATIN;
103103
}
104104
}
105105
if (property_exists($payload, 'region')) {
106-
$data["REGION"] = $payload->region;
106+
$data["region"] = $payload->region;
107107
}
108108
if (property_exists($payload, 'year')) {
109-
$data["YEAR"] = $payload->year;
109+
$data["year"] = $payload->year;
110110
}
111111
return $data;
112112
}
113113

114-
/**
115-
* Handles the GET request payload for the Missals endpoint.
116-
*
117-
* @return array the initialized request parameters
118-
*/
119-
private static function handleGetPayload(): array
120-
{
121-
$data = [];
122-
if (isset($_GET['locale'])) {
123-
$data["LOCALE"] = $_GET['locale'];
124-
} elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
125-
$locale = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
126-
if ($locale && LitLocale::isValid($locale)) {
127-
$data["LOCALE"] = $locale;
128-
} else {
129-
$data["LOCALE"] = LitLocale::LATIN;
130-
}
131-
}
132-
if (isset($_GET['region'])) {
133-
$data["REGION"] = $_GET["region"];
134-
}
135-
if (isset($_GET['year'])) {
136-
$data["YEAR"] = $_GET['year'];
137-
}
138-
return $data;
139-
}
140114

141115
/**
142116
* Initialize the request parameters for the Missals endpoint.
@@ -158,12 +132,12 @@ private static function initRequestParams(): array
158132
if (in_array(self::$Core->getRequestMethod(), [RequestMethod::POST, RequestMethod::PUT, RequestMethod::PATCH])) {
159133
$payload = self::initPayloadFromRequestBody();
160134
if (self::$Core->getRequestMethod() === RequestMethod::POST) {
161-
$data = self::handlePostPayload($payload);
135+
$data = self::initPayload($payload);
162136
} else {
163137
$data["PAYLOAD"] = $payload;
164138
}
165139
} elseif (self::$Core->getRequestMethod() === RequestMethod::GET) {
166-
$data = self::handleGetPayload();
140+
$data = self::initPayload((object)$_GET);
167141
}
168142
return $data;
169143
}
@@ -313,6 +287,62 @@ private static function produceResponse(string $jsonEncodedResponse): void
313287
die();
314288
}
315289

290+
/**
291+
* Handles the request for the /missals endpoint.
292+
*
293+
* If the request method is GET, it will validate the Accept header and set the
294+
* response content type header.
295+
* If the request method is POST, PUT, or PATCH, it will validate the request body
296+
* and set the response content type header.
297+
* If there are no path parameters, it will return all the Missal metadata.
298+
* If there is one path parameter, it will attempt to retrieve the Missal with the
299+
* given ID, and if found:
300+
* - if the Missal has localized data, it will attempt to retrieve the localized
301+
* data for the base locale, and if found, it will return the localized data.
302+
* - if the Missal does not have localized data, or if the localized data for the
303+
* base locale was not found, it will return the Missal data.
304+
* If the Missal was not found, it will produce an error response with a status code
305+
* of 404, listing the available Missal IDs.
306+
*/
307+
public static function handleRequest()
308+
{
309+
self::$Core->init();
310+
if (self::$Core->getRequestMethod() === RequestMethod::GET) {
311+
self::$Core->validateAcceptHeader(true);
312+
} else {
313+
self::$Core->validateAcceptHeader(false);
314+
}
315+
self::$Core->setResponseContentTypeHeader();
316+
if (count(self::$requestPathParts) === 0) {
317+
if (null !== self::$params->Locale) {
318+
header("X-Litcal-Missals-Locale: " . self::$params->Locale, false);
319+
} else {
320+
header("X-Litcal-Missals-Locale: none", false);
321+
}
322+
if (null === self::$params->Region && null === self::$params->Year) {
323+
self::produceResponse(json_encode(self::$missalsIndex));
324+
} else {
325+
$filteredResults = self::$missalsIndex;
326+
if (null !== self::$params->Region) {
327+
$filteredResults->litcal_missals = array_values(array_filter(
328+
$filteredResults->litcal_missals,
329+
fn ($missal) => $missal->region === self::$params->Region
330+
));
331+
header("X-Litcal-Missals-Region: " . self::$params->Region, false);
332+
}
333+
if (null !== self::$params->Year) {
334+
$filteredResults->litcal_missals = array_values(array_filter(
335+
$filteredResults->litcal_missals,
336+
fn ($missal) => $missal->year_published === self::$params->Year
337+
));
338+
header("X-Litcal-Missals-Year: " . self::$params->Year, false);
339+
}
340+
self::produceResponse(json_encode($filteredResults));
341+
}
342+
}
343+
self::handlePathParams();
344+
}
345+
316346
/**
317347
* Initializes the Missals class.
318348
*
@@ -389,60 +419,4 @@ public static function init(array $requestPathParts = [])
389419
// we only set the request parameters after we have collected the MissalRegions and MissalYears
390420
self::$params->setData(self::initRequestParams());
391421
}
392-
393-
/**
394-
* Handles the request for the /missals endpoint.
395-
*
396-
* If the request method is GET, it will validate the Accept header and set the
397-
* response content type header.
398-
* If the request method is POST, PUT, or PATCH, it will validate the request body
399-
* and set the response content type header.
400-
* If there are no path parameters, it will return all the Missal metadata.
401-
* If there is one path parameter, it will attempt to retrieve the Missal with the
402-
* given ID, and if found:
403-
* - if the Missal has localized data, it will attempt to retrieve the localized
404-
* data for the base locale, and if found, it will return the localized data.
405-
* - if the Missal does not have localized data, or if the localized data for the
406-
* base locale was not found, it will return the Missal data.
407-
* If the Missal was not found, it will produce an error response with a status code
408-
* of 404, listing the available Missal IDs.
409-
*/
410-
public static function handleRequest()
411-
{
412-
self::$Core->init();
413-
if (self::$Core->getRequestMethod() === RequestMethod::GET) {
414-
self::$Core->validateAcceptHeader(true);
415-
} else {
416-
self::$Core->validateAcceptHeader(false);
417-
}
418-
self::$Core->setResponseContentTypeHeader();
419-
if (count(self::$requestPathParts) === 0) {
420-
if (null !== self::$params->Locale) {
421-
header("X-Litcal-Missals-Locale: " . self::$params->Locale, false);
422-
} else {
423-
header("X-Litcal-Missals-Locale: none", false);
424-
}
425-
if (null === self::$params->Region && null === self::$params->Year) {
426-
self::produceResponse(json_encode(self::$missalsIndex));
427-
} else {
428-
$filteredResults = self::$missalsIndex;
429-
if (null !== self::$params->Region) {
430-
$filteredResults->litcal_missals = array_values(array_filter(
431-
$filteredResults->litcal_missals,
432-
fn ($missal) => $missal->region === self::$params->Region
433-
));
434-
header("X-Litcal-Missals-Region: " . self::$params->Region, false);
435-
}
436-
if (null !== self::$params->Year) {
437-
$filteredResults->litcal_missals = array_values(array_filter(
438-
$filteredResults->litcal_missals,
439-
fn ($missal) => $missal->year_published === self::$params->Year
440-
));
441-
header("X-Litcal-Missals-Year: " . self::$params->Year, false);
442-
}
443-
self::produceResponse(json_encode($filteredResults));
444-
}
445-
}
446-
self::handlePathParams();
447-
}
448422
}

0 commit comments

Comments
 (0)