Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
44 / 44 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
| AnswerTrait | |
100.00% |
44 / 44 |
|
100.00% |
10 / 10 |
11 | |
100.00% |
1 / 1 |
| answerSuccess | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| answerFail | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| answerError | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| answerNoContent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| answerInvalidUuid | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| answerRecordNotFound | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| answerEmptyPayload | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| answerRequired | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| answerColumnNotFound | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| jsonResponse | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace DevToolbelt\JsendPayload; |
| 6 | |
| 7 | use DevToolbelt\Enums\Http\HttpStatusCode as Code; |
| 8 | use Nyholm\Psr7\Response; |
| 9 | use Psr\Http\Message\ResponseInterface; |
| 10 | |
| 11 | trait AnswerTrait |
| 12 | { |
| 13 | public function answerSuccess(mixed $data, Code $code = Code::OK, array $meta = []): ResponseInterface |
| 14 | { |
| 15 | return $this->jsonResponse([ |
| 16 | 'status' => JsendStatus::SUCCESS->value, |
| 17 | 'data' => $data, |
| 18 | 'meta' => $meta |
| 19 | ], $code); |
| 20 | } |
| 21 | |
| 22 | public function answerFail(array $data, Code $code = Code::BAD_REQUEST, array $meta = []): ResponseInterface |
| 23 | { |
| 24 | return $this->jsonResponse([ |
| 25 | 'status' => JsendStatus::FAIL->value, |
| 26 | 'data' => $data, |
| 27 | 'meta' => $meta |
| 28 | ], $code); |
| 29 | } |
| 30 | |
| 31 | public function answerError( |
| 32 | string $message, |
| 33 | Code $code = Code::INTERNAL_SERVER_ERROR, |
| 34 | mixed $data = null |
| 35 | ): ResponseInterface { |
| 36 | $response = [ |
| 37 | 'status' => JsendStatus::ERROR->value, |
| 38 | 'message' => $message, |
| 39 | ]; |
| 40 | |
| 41 | if ($data !== null) { |
| 42 | $response['data'] = $data; |
| 43 | } |
| 44 | |
| 45 | return $this->jsonResponse($response, $code); |
| 46 | } |
| 47 | |
| 48 | public function answerNoContent(Code $code = Code::OK): ResponseInterface |
| 49 | { |
| 50 | return $this->jsonResponse(['status' => JsendStatus::SUCCESS->value, 'data' => null], $code); |
| 51 | } |
| 52 | |
| 53 | public function answerInvalidUuid(Code $code = Code::BAD_REQUEST): ResponseInterface |
| 54 | { |
| 55 | return $this->answerFail([[ |
| 56 | 'field' => 'id', |
| 57 | 'error' => 'invalidUuidFormat', |
| 58 | 'message' => 'The provided uuid format is invalid', |
| 59 | ]], $code); |
| 60 | } |
| 61 | |
| 62 | public function answerRecordNotFound(Code $code = Code::NOT_FOUND): ResponseInterface |
| 63 | { |
| 64 | return $this->answerFail([[ |
| 65 | 'field' => 'id', |
| 66 | 'error' => 'recordNotFound', |
| 67 | 'message' => 'The record was not found with the given id', |
| 68 | ]], $code); |
| 69 | } |
| 70 | |
| 71 | public function answerEmptyPayload(Code $code = Code::BAD_REQUEST): ResponseInterface |
| 72 | { |
| 73 | return $this->answerFail([['error' => 'emptyPayload', 'message' => 'It was send a empty payload']], $code); |
| 74 | } |
| 75 | |
| 76 | public function answerRequired(string $fieldName, Code $code = Code::BAD_REQUEST): ResponseInterface |
| 77 | { |
| 78 | return $this->answerFail([[ |
| 79 | 'field' => $fieldName, |
| 80 | 'error' => 'required', |
| 81 | 'message' => "The \"{$fieldName}\" field is required", |
| 82 | ]], $code); |
| 83 | } |
| 84 | |
| 85 | public function answerColumnNotFound(string $columnName, Code $code = Code::BAD_REQUEST): ResponseInterface |
| 86 | { |
| 87 | return $this->answerFail([[ |
| 88 | 'field' => $columnName, |
| 89 | 'error' => 'columnNotFound', |
| 90 | 'message' => "The \"{$columnName}\" column was not found", |
| 91 | ]], $code); |
| 92 | } |
| 93 | |
| 94 | private function jsonResponse(array $payload, Code $code): ResponseInterface |
| 95 | { |
| 96 | return new Response( |
| 97 | $code->value, |
| 98 | ['Content-Type' => 'application/json'], |
| 99 | json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR) |
| 100 | ); |
| 101 | } |
| 102 | } |