@@ -63,14 +63,14 @@ private static function initPayloadFromRequestBody(): ?object
63
63
default :
64
64
if (in_array (self ::$ Core ->getRequestMethod (), [RequestMethod::PUT , RequestMethod::PATCH ])) {
65
65
// 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 " );
67
67
}
68
68
}
69
69
return $ payload ;
70
70
}
71
71
72
72
/**
73
- * Handles the POST request payload for the Missals endpoint.
73
+ * Handles the GET and POST request payload for the Missals endpoint.
74
74
*
75
75
* If the request body is a JSON or YAML encoded object, it will attempt to
76
76
* retrieve the locale, region, and year from the object.
@@ -89,54 +89,28 @@ private static function initPayloadFromRequestBody(): ?object
89
89
* request body was not a JSON or YAML encoded object
90
90
* @return array the initialized request parameters
91
91
*/
92
- private static function handlePostPayload (?object $ payload ): array
92
+ private static function initPayload (?object $ payload ): array
93
93
{
94
94
$ data = [];
95
95
if ($ payload !== null && property_exists ($ payload , 'locale ' )) {
96
- $ data ["LOCALE " ] = $ payload ->locale ;
96
+ $ data ["locale " ] = $ payload ->locale ;
97
97
} elseif (isset ($ _SERVER ['HTTP_ACCEPT_LANGUAGE ' ])) {
98
98
$ locale = \Locale::acceptFromHttp ($ _SERVER ['HTTP_ACCEPT_LANGUAGE ' ]);
99
99
if ($ locale && LitLocale::isValid ($ locale )) {
100
- $ data ["LOCALE " ] = $ locale ;
100
+ $ data ["locale " ] = $ locale ;
101
101
} else {
102
- $ data ["LOCALE " ] = LitLocale::LATIN ;
102
+ $ data ["locale " ] = LitLocale::LATIN ;
103
103
}
104
104
}
105
105
if (property_exists ($ payload , 'region ' )) {
106
- $ data ["REGION " ] = $ payload ->region ;
106
+ $ data ["region " ] = $ payload ->region ;
107
107
}
108
108
if (property_exists ($ payload , 'year ' )) {
109
- $ data ["YEAR " ] = $ payload ->year ;
109
+ $ data ["year " ] = $ payload ->year ;
110
110
}
111
111
return $ data ;
112
112
}
113
113
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
- }
140
114
141
115
/**
142
116
* Initialize the request parameters for the Missals endpoint.
@@ -158,12 +132,12 @@ private static function initRequestParams(): array
158
132
if (in_array (self ::$ Core ->getRequestMethod (), [RequestMethod::POST , RequestMethod::PUT , RequestMethod::PATCH ])) {
159
133
$ payload = self ::initPayloadFromRequestBody ();
160
134
if (self ::$ Core ->getRequestMethod () === RequestMethod::POST ) {
161
- $ data = self ::handlePostPayload ($ payload );
135
+ $ data = self ::initPayload ($ payload );
162
136
} else {
163
137
$ data ["PAYLOAD " ] = $ payload ;
164
138
}
165
139
} elseif (self ::$ Core ->getRequestMethod () === RequestMethod::GET ) {
166
- $ data = self ::handleGetPayload ( );
140
+ $ data = self ::initPayload (( object ) $ _GET );
167
141
}
168
142
return $ data ;
169
143
}
@@ -313,6 +287,62 @@ private static function produceResponse(string $jsonEncodedResponse): void
313
287
die ();
314
288
}
315
289
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
+
316
346
/**
317
347
* Initializes the Missals class.
318
348
*
@@ -389,60 +419,4 @@ public static function init(array $requestPathParts = [])
389
419
// we only set the request parameters after we have collected the MissalRegions and MissalYears
390
420
self ::$ params ->setData (self ::initRequestParams ());
391
421
}
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
- }
448
422
}
0 commit comments