1 | <?php |
||
21 | abstract class AbstractResponse implements Response |
||
22 | { |
||
23 | |||
24 | const CODE_WRONG_ARGS = 'GEN-WRONG-ARGS'; |
||
25 | |||
26 | const CODE_NOT_FOUND = 'GEN-NOT-FOUND'; |
||
27 | |||
28 | const CODE_INTERNAL_ERROR = 'GEN-INTERNAL-ERROR'; |
||
29 | |||
30 | const CODE_UNAUTHORIZED = 'GEN-UNAUTHORIZED'; |
||
31 | |||
32 | const CODE_FORBIDDEN = 'GEN-FORBIDDEN'; |
||
33 | |||
34 | const CODE_GONE = 'GEN-GONE'; |
||
35 | |||
36 | const CODE_METHOD_NOT_ALLOWED = 'GEN-METHOD-NOT-ALLOWED'; |
||
37 | |||
38 | const CODE_UNWILLING_TO_PROCESS = 'GEN-UNWILLING-TO-PROCESS'; |
||
39 | |||
40 | const CODE_UNPROCESSABLE = 'GEN-UNPROCESSABLE'; |
||
41 | |||
42 | |||
43 | /** |
||
44 | * HTTP Status code |
||
45 | * |
||
46 | * @var int |
||
47 | */ |
||
48 | protected $statusCode = 200; |
||
49 | |||
50 | /** |
||
51 | * Fractal manager |
||
52 | * |
||
53 | * @var \League\Fractal\Manager |
||
54 | */ |
||
55 | protected $manager; |
||
56 | |||
57 | /** |
||
58 | * @param \League\Fractal\Manager $manager |
||
59 | */ |
||
60 | 21 | public function __construct(Manager $manager) |
|
64 | |||
65 | /** |
||
66 | * @return \League\Fractal\Manager |
||
67 | */ |
||
68 | 1 | public function getManager() |
|
72 | |||
73 | /** |
||
74 | * Getter for statusCode |
||
75 | * |
||
76 | * @return int |
||
77 | */ |
||
78 | 2 | public function getStatusCode() |
|
82 | |||
83 | /** |
||
84 | * Setter for status code |
||
85 | * |
||
86 | * @param int $statusCode |
||
87 | * @return \EllipseSynergie\ApiResponse\AbstractResponse |
||
88 | */ |
||
89 | 12 | public function setStatusCode($statusCode) |
|
94 | |||
95 | /** |
||
96 | * Implement this !!! |
||
97 | * This method return the final response output |
||
98 | * |
||
99 | * @param array $array |
||
100 | * @param array $headers |
||
101 | */ |
||
102 | abstract public function withArray(array $array, array $headers = []); |
||
103 | |||
104 | /** |
||
105 | * Response for one item |
||
106 | * |
||
107 | * @param $data |
||
108 | * @param callable|\League\Fractal\TransformerAbstract $transformer |
||
109 | * @param string $resourceKey |
||
110 | * @param array $meta |
||
111 | * @param array $headers |
||
112 | * @return mixed |
||
113 | */ |
||
114 | 2 | public function withItem($data, $transformer, $resourceKey = null, $meta = [], array $headers = []) |
|
126 | |||
127 | /** |
||
128 | * Response for collection of items |
||
129 | * |
||
130 | * @param $data |
||
131 | * @param callable|\League\Fractal\TransformerAbstract $transformer |
||
132 | * @param string $resourceKey |
||
133 | * @param Cursor $cursor |
||
134 | * @param array $meta |
||
135 | * @param array $headers |
||
136 | * @return mixed |
||
137 | */ |
||
138 | 3 | public function withCollection($data, $transformer, $resourceKey = null, Cursor $cursor = null, $meta = [], array $headers = []) |
|
154 | |||
155 | /** |
||
156 | * Response for errors |
||
157 | * |
||
158 | * @param string $message |
||
159 | * @param string $errorCode |
||
160 | * @param array $headers |
||
161 | * @return mixed |
||
162 | */ |
||
163 | 11 | public function withError($message, $errorCode, array $headers = []) |
|
175 | |||
176 | /** |
||
177 | * Generates a response with a 403 HTTP header and a given message. |
||
178 | * |
||
179 | * @param string $message |
||
180 | * @param array $headers |
||
181 | * @return mixed |
||
182 | */ |
||
183 | 1 | public function errorForbidden($message = 'Forbidden', array $headers = []) |
|
187 | |||
188 | /** |
||
189 | * Generates a response with a 500 HTTP header and a given message. |
||
190 | * |
||
191 | * @param string $message |
||
192 | * @param array $headers |
||
193 | * @return mixed |
||
194 | */ |
||
195 | 1 | public function errorInternalError($message = 'Internal Error', array $headers = []) |
|
199 | |||
200 | /** |
||
201 | * Generates a response with a 404 HTTP header and a given message. |
||
202 | * |
||
203 | * @param string $message |
||
204 | * @param array $headers |
||
205 | * @return mixed |
||
206 | */ |
||
207 | 1 | public function errorNotFound($message = 'Resource Not Found', array $headers = []) |
|
211 | |||
212 | /** |
||
213 | * Generates a response with a 401 HTTP header and a given message. |
||
214 | * |
||
215 | * @param string $message |
||
216 | * @param array $headers |
||
217 | * @return mixed |
||
218 | */ |
||
219 | 1 | public function errorUnauthorized($message = 'Unauthorized', array $headers = []) |
|
223 | |||
224 | /** |
||
225 | * Generates a response with a 400 HTTP header and a given message. |
||
226 | * |
||
227 | * @param string $message |
||
228 | * @param array $headers |
||
229 | * @return mixed |
||
230 | */ |
||
231 | 2 | public function errorWrongArgs($message = 'Wrong Arguments', array $headers = []) |
|
235 | |||
236 | /** |
||
237 | * Generates a response with a 410 HTTP header and a given message. |
||
238 | * |
||
239 | * @param string $message |
||
240 | * @param array $headers |
||
241 | * @return mixed |
||
242 | */ |
||
243 | 1 | public function errorGone($message = 'Resource No Longer Available', array $headers = []) |
|
247 | |||
248 | /** |
||
249 | * Generates a response with a 405 HTTP header and a given message. |
||
250 | * |
||
251 | * @param string $message |
||
252 | * @param array $headers |
||
253 | * @return mixed |
||
254 | */ |
||
255 | 1 | public function errorMethodNotAllowed($message = 'Method Not Allowed', array $headers = []) |
|
259 | |||
260 | /** |
||
261 | * Generates a Response with a 431 HTTP header and a given message. |
||
262 | * |
||
263 | * @param string $message |
||
264 | * @param array $headers |
||
265 | * @return mixed |
||
266 | */ |
||
267 | 1 | public function errorUnwillingToProcess($message = 'Server is unwilling to process the request', array $headers = []) |
|
271 | |||
272 | /** |
||
273 | * Generates a Response with a 422 HTTP header and a given message. |
||
274 | * |
||
275 | * @param string $message |
||
276 | * @param array $headers |
||
277 | * @return mixed |
||
278 | */ |
||
279 | 1 | public function errorUnprocessable($message = 'Unprocessable Entity', array $headers = []) |
|
283 | } |
||
284 |