1 | <?php |
||
15 | abstract class ResponseHandler implements |
||
16 | FractalAwareInterface, |
||
17 | EndpointInterface, |
||
18 | HttpMessageAwareInterface, |
||
19 | UtilityAwareInterface |
||
20 | { |
||
21 | use FractalAwareTrait; |
||
22 | use HttpMessageAwareTrait; |
||
23 | use UtilityAwareTrait; |
||
24 | |||
25 | const CODE_WRONG_ARGS = 'API-MALFORMED-REQUEST'; |
||
26 | const CODE_NOT_FOUND = 'API-NOT-FOUND'; |
||
27 | const CODE_INTERNAL_ERROR = 'API-DOH'; |
||
28 | const CODE_EXTERNAL_ERROR = 'API-NOT-MY-PROBLEM'; |
||
29 | const CODE_UNAUTHORIZED = 'API-UNAUTHORIZED'; |
||
30 | const CODE_FORBIDDEN = 'API-DENIED'; |
||
31 | const CODE_EMPTY = 'API-EMPTY'; |
||
32 | |||
33 | /** |
||
34 | * Stores the status code |
||
35 | * |
||
36 | * @var integer |
||
37 | */ |
||
38 | protected $statusCode = 200; |
||
39 | |||
40 | /** |
||
41 | * Flag whether to send back a "is cached" header |
||
42 | * |
||
43 | * @var boolean |
||
44 | */ |
||
45 | protected $sendCachedHeader = false; |
||
46 | |||
47 | /** |
||
48 | * Getter for statusCode |
||
49 | * |
||
50 | * @return int |
||
51 | */ |
||
52 | public function getStatusCode() |
||
56 | |||
57 | /** |
||
58 | * Setter for statusCode |
||
59 | * |
||
60 | * @param integer $statusCode Value to set |
||
61 | * |
||
62 | * @return self |
||
63 | */ |
||
64 | public function setStatusCode(int $statusCode) |
||
69 | |||
70 | /** |
||
71 | * Master function to split out appropriate calls |
||
72 | * |
||
73 | * @param string $kind The kind of data we wish to return |
||
74 | * @param array $data The data itself |
||
75 | * @param TransformerAbstract $transformer The transformer class to call |
||
76 | * |
||
77 | * @return ResponseInterface |
||
78 | */ |
||
79 | public function respond(string $kind, array $data, TransformerAbstract $transformer) |
||
95 | |||
96 | /** |
||
97 | * Builds an item response in Fractal then hands off to the responder |
||
98 | * |
||
99 | * @param array $item The item to transform |
||
100 | * @param TransformerAbstract $transformer The Transformer to pass through to Fractal |
||
101 | * |
||
102 | * @return ResponseInterface |
||
103 | */ |
||
104 | public function respondWithItem(array $item, TransformerAbstract $transformer) |
||
108 | |||
109 | /** |
||
110 | * Builds a collection of items from Fractal then hands off to the responder |
||
111 | * |
||
112 | * @param array $collection The collection to transform |
||
113 | * @param TransformerAbstract $transformer The Transformer to pass through to Fractal |
||
114 | * |
||
115 | * @return ResponseInterface |
||
116 | */ |
||
117 | public function respondWithCollection(array $collection, TransformerAbstract $transformer) |
||
121 | |||
122 | /** |
||
123 | * Responds gracefully with an error. |
||
124 | * |
||
125 | * @param string $message Response message to put in the error |
||
126 | * @param int $code Error code to set |
||
127 | * |
||
128 | * @return ResponseInterface |
||
129 | */ |
||
130 | public function respondWithError(string $message, $code) |
||
168 | |||
169 | /** |
||
170 | * The final step where the formatted array is now sent back as a response in JSON form |
||
171 | * |
||
172 | * @param array $data |
||
173 | * |
||
174 | * @return ResponseInterface |
||
175 | */ |
||
176 | public function respondWithData(array $data) |
||
191 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: