Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
45.45% |
10 / 22 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
RequestBodyParser | |
45.45% |
10 / 22 |
|
20.00% |
1 / 5 |
40.43 | |
0.00% |
0 / 1 |
getRequiredElement | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
getElementWithDefault | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
getElementsFromRequest | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
applyDefaultsIfNotRequired | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
4.07 | |||
getElementsFromArray | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | /** @noinspection PhpUnhandledExceptionInspection */ |
4 | declare(strict_types=1); |
5 | |
6 | // TODO unit test |
7 | |
8 | use Slim\Exception\HttpBadRequestException; |
9 | use Slim\Http\ServerRequest as Request; |
10 | |
11 | class RequestBodyParser { |
12 | static function getRequiredElement(Request $request, string $elementName) { |
13 | $requestBody = JSON::decode($request->getBody()->getContents()); |
14 | |
15 | if (!isset($requestBody->$elementName)) { |
16 | throw new HttpBadRequestException($request, "Required body-parameter is missing: `$elementName`"); |
17 | } |
18 | |
19 | return $requestBody->$elementName; |
20 | } |
21 | |
22 | static function getElementWithDefault(Request $request, string $elementName, $default) { |
23 | $requestBody = JSON::decode($request->getBody()->getContents()); |
24 | |
25 | return isset($requestBody->$elementName) ? $requestBody->$elementName : $default; |
26 | } |
27 | |
28 | static function getElementsFromRequest(Request $request, array $requiredElements2defaults = []): array { |
29 | $requestBody = JSON::decode($request->getBody()->getContents()); |
30 | return self::applyDefaultsIfNotRequired($requestBody, $requiredElements2defaults); |
31 | } |
32 | |
33 | /** |
34 | * @param $elementObject |
35 | * @param array $elements2defaults this array shows which elements are required to be present and which will be mapped with a |
36 | * default value. The structure is: ['element' => 'default', ...]. If the value is the string 'REQUIRED' the element |
37 | * is required and cannot be mapped with a default value. For every other value the element will be mapped with the |
38 | * given default value, if the element is not present in the request body. |
39 | * @return array |
40 | * @throws HttpError |
41 | */ |
42 | static private function applyDefaultsIfNotRequired($elementObject, array $elements2defaults): array { |
43 | $elements = []; |
44 | |
45 | foreach ($elements2defaults as $element => $default) { |
46 | if (!isset($elementObject->$element) and ($default === 'REQUIRED')) { |
47 | throw new HttpError("Required body-parameter is missing: `$element`", 400); |
48 | } |
49 | |
50 | $elements[$element] = $elementObject->$element ?? $default; |
51 | } |
52 | |
53 | return $elements; |
54 | } |
55 | |
56 | // TODO Unit Test |
57 | static function getElementsFromArray(Request $request, array $elements2defaults = [], mixed $getOnlyThisKey = null): array { |
58 | $requestBody = JSON::decode($request->getBody()->getContents()); |
59 | $requestBody = !is_null($getOnlyThisKey) ? $requestBody->$getOnlyThisKey : $requestBody; |
60 | |
61 | if (!is_array($requestBody)) { |
62 | throw new HttpBadRequestException($request, "body has to be an array"); |
63 | } |
64 | |
65 | $result = []; |
66 | |
67 | foreach ($requestBody as $row) { |
68 | $result[] = self::applyDefaultsIfNotRequired($row, $elements2defaults); |
69 | } |
70 | |
71 | return $result; |
72 | } |
73 | } |