forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RestControllerHelper.php
92 lines (81 loc) · 3.17 KB
/
RestControllerHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
* RestControllerHelper
*
* @package OpenEMR
* @link http://www.open-emr.org
* @author Matthew Vita <[email protected]>
* @copyright Copyright (c) 2018 Matthew Vita <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
namespace OpenEMR\RestControllers;
class RestControllerHelper
{
/**
* Configures the HTTP status code and payload returned within a response.
*/
public static function responseHandler($serviceResult, $customRespPayload, $idealStatusCode)
{
if ($serviceResult) {
http_response_code($idealStatusCode);
if ($customRespPayload) {
return $customRespPayload;
}
return $serviceResult;
}
// if no result is present return a 404 with a null response
http_response_code(404);
return null;
}
public static function validationHandler($validationResult)
{
if (property_exists($validationResult, 'isValid') && !$validationResult->isValid()) {
http_response_code(400);
if (property_exists($validationResult, 'getValidationMessages')) {
$validationMessages = $validationResult->getValidationMessages();
} else {
$validationMesssages = $validationResult->getMessages();
}
return $validationMesssages;
}
}
/**
* Parses a service processing result to determine the appropriate HTTP status code and response format
* for a request.
*
* The response body has a uniform structure with the following top level keys:
* - validationErrors
* - internalErrors
* - data
*
* The response data key conveys the data payload for a response. The payload is either a "top level" array for a
* single result, or an array for multiple results.
*
* @param $processingResult - The service processing result.
* @param $successStatusCode - The HTTP status code to return for a successful operation that completes without error.
* @param $isMultipleResultResponse - Indicates if the response contains multiple results.
*/
public static function handleProcessingResult($processingResult, $successStatusCode, $isMultipleResultResponse = false)
{
$httpResponseBody = [
"validationErrors" => [],
"internalErrors" => [],
"data" => []
];
if (!$processingResult->isValid()) {
http_response_code(400);
$httpResponseBody["validationErrors"] = $processingResult->getValidationMessages();
} else if ($processingResult->hasInternalErrors()) {
http_response_code(500);
$httpResponseBody["internalErrors"] = $processingResult->getInternalErrors();
} else {
http_response_code($successStatusCode);
$dataResult = $processingResult->getData();
if (!$isMultipleResultResponse) {
$dataResult = (count($dataResult) == 0) ? [] : $dataResult[0];
}
$httpResponseBody["data"] = $dataResult;
}
return $httpResponseBody;
}
}