bakaphp /
phalcon-api
| 1 | <?php |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Gewaer\Http; |
||
| 6 | |||
| 7 | use Phalcon\Http\Response as PhResponse; |
||
| 8 | use Phalcon\Mvc\Model\MessageInterface as ModelMessage; |
||
| 9 | use Phalcon\Validation\Message\Group as ValidationMessage; |
||
| 10 | |||
| 11 | class Response extends PhResponse |
||
| 12 | {
|
||
| 13 | const OK = 200; |
||
| 14 | const CREATED = 201; |
||
| 15 | const ACCEPTED = 202; |
||
| 16 | const MOVED_PERMANENTLY = 301; |
||
| 17 | const FOUND = 302; |
||
| 18 | const TEMPORARY_REDIRECT = 307; |
||
| 19 | const PERMANENTLY_REDIRECT = 308; |
||
| 20 | const BAD_REQUEST = 400; |
||
| 21 | const UNAUTHORIZED = 401; |
||
| 22 | const FORBIDDEN = 403; |
||
| 23 | const NOT_FOUND = 404; |
||
| 24 | const INTERNAL_SERVER_ERROR = 500; |
||
| 25 | const NOT_IMPLEMENTED = 501; |
||
| 26 | const BAD_GATEWAY = 502; |
||
| 27 | |||
| 28 | private $codes = [ |
||
| 29 | 200 => 'OK', |
||
| 30 | 301 => 'Moved Permanently', |
||
| 31 | 302 => 'Found', |
||
| 32 | 307 => 'Temporary Redirect', |
||
| 33 | 308 => 'Permanent Redirect', |
||
| 34 | 400 => 'Bad Request', |
||
| 35 | 401 => 'Unauthorized', |
||
| 36 | 403 => 'Forbidden', |
||
| 37 | 404 => 'Not Found', |
||
| 38 | 500 => 'Internal Server Error', |
||
| 39 | 501 => 'Not Implemented', |
||
| 40 | 502 => 'Bad Gateway', |
||
| 41 | ]; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Returns the http code description or if not found the code itself |
||
| 45 | * @param int $code |
||
| 46 | * |
||
| 47 | * @return int|string |
||
| 48 | */ |
||
| 49 | public function getHttpCodeDescription(int $code) |
||
| 50 | {
|
||
| 51 | if (true === isset($this->codes[$code])) {
|
||
| 52 | return sprintf('%d (%s)', $code, $this->codes[$code]);
|
||
| 53 | } |
||
| 54 | |||
| 55 | return $code; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Send the response back |
||
| 60 | * |
||
| 61 | * @return PhResponse |
||
| 62 | */ |
||
| 63 | public function send(): PhResponse |
||
| 64 | {
|
||
| 65 | $content = $this->getContent(); |
||
| 66 | $timestamp = date('c');
|
||
| 67 | $hash = sha1($timestamp . $content); |
||
| 68 | $eTag = sha1($content); |
||
| 69 | |||
| 70 | /** @var array $content */ |
||
| 71 | $content = json_decode($this->getContent(), true); |
||
| 72 | |||
| 73 | $jsonapi = [ |
||
| 74 | 'jsonapi' => [ |
||
| 75 | 'version' => '1.0', |
||
| 76 | ], |
||
| 77 | ]; |
||
| 78 | $meta = [ |
||
| 79 | 'meta' => [ |
||
| 80 | 'timestamp' => $timestamp, |
||
| 81 | 'hash' => $hash, |
||
| 82 | ] |
||
| 83 | ]; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Join the array again |
||
| 87 | */ |
||
| 88 | $data = $jsonapi + $content + $meta; |
||
| 89 | $this |
||
| 90 | ->setHeader('E-Tag', $eTag)
|
||
| 91 | ->setJsonContent($data); |
||
| 92 | |||
| 93 | return parent::send(); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Sets the payload code as Error |
||
| 98 | * |
||
| 99 | * @param string $detail |
||
| 100 | * |
||
| 101 | * @return Response |
||
| 102 | */ |
||
| 103 | public function setPayloadError(string $detail = ''): Response |
||
| 104 | {
|
||
| 105 | $this->setJsonContent(['errors' => [$detail]]); |
||
| 106 | |||
| 107 | return $this; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Traverses the errors collection and sets the errors in the payload |
||
| 112 | * |
||
| 113 | * @param ModelMessage[]|ValidationMessage $errors |
||
| 114 | * |
||
| 115 | * @return Response |
||
| 116 | */ |
||
| 117 | public function setPayloadErrors($errors): Response |
||
| 118 | {
|
||
| 119 | $data = []; |
||
| 120 | foreach ($errors as $error) {
|
||
| 121 | $data[] = $error->getMessage(); |
||
| 122 | } |
||
| 123 | |||
| 124 | $this->setJsonContent(['errors' => $data]); |
||
| 125 | |||
| 126 | return $this; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Sets the payload code as Success |
||
| 131 | * |
||
| 132 | * @param null|string|array $content The content |
||
| 133 | * |
||
| 134 | * @return Response |
||
| 135 | */ |
||
| 136 | public function setPayloadSuccess($content = []): Response |
||
| 137 | {
|
||
| 138 | $data = (true === is_array($content)) ? $content : ['data' => $content]; |
||
| 139 | $data = (true === isset($data['data'])) ? $data : ['data' => $data]; |
||
| 140 | |||
| 141 | $this->setJsonContent($data); |
||
| 142 | |||
| 143 | return $this; |
||
| 144 | } |
||
| 145 | } |
||
| 146 |