@@ -113,15 +113,22 @@ class Response implements PhpRestfulApiResponse
113113 */
114114 private $ statusCode ;
115115
116+ /**
117+ * @var int|string
118+ */
119+ private $ errorCode ;
120+
116121 /**
117122 * Response constructor.
118123 * @param string $body
119124 * @param int $status
125+ * @param int $errorCode
120126 * @param array $headers
121127 */
122- public function __construct ($ body = 'php://memory ' , $ status = 200 , array $ headers = [])
128+ public function __construct ($ body = 'php://memory ' , int $ status = 200 , $ errorCode = null , array $ headers = [])
123129 {
124130 $ this ->setStatusCode ($ status );
131+ $ this ->setErrorCode ($ errorCode );
125132 $ this ->stream = $ this ->getStream ($ body , 'wb+ ' );
126133 $ this ->setHeaders ($ headers );
127134 }
@@ -232,19 +239,22 @@ public function withCollection($data, $transformer, $code = 200, $resourceKey =
232239 * Response for errors
233240 *
234241 * @param string|array $message
235- * @param int $code
242+ * @param int $statusCode
243+ * @param int|string $errorCode
236244 * @param array $headers
237245 * @return mixed
238246 */
239- public function withError ($ message , $ code , array $ headers = [])
247+ public function withError ($ message , int $ statusCode , $ errorCode = null , array $ headers = [])
240248 {
241249 $ new = clone $ this ;
242- $ new ->setStatusCode ($ code );
250+ $ new ->setStatusCode ($ statusCode );
251+ $ new ->setErrorCode ($ errorCode );
243252 $ new ->getBody ()->write (
244253 json_encode (
245254 [
246255 'error ' => array_filter ([
247256 'http_code ' => $ new ->statusCode ,
257+ 'code ' => $ errorCode ,
248258 'phrase ' => $ new ->getReasonPhrase (),
249259 'message ' => $ message
250260 ])
@@ -260,128 +270,154 @@ public function withError($message, $code, array $headers = [])
260270 * Generates a response with a 403 HTTP header and a given message.
261271 *
262272 * @param string $message
273+ * @param int|string $errorCode
263274 * @param array $headers
264275 * @return mixed
265276 */
266- public function errorForbidden (string $ message = '' , array $ headers = [])
277+ public function errorForbidden (string $ message = '' , $ errorCode = null , array $ headers = [])
267278 {
268- return $ this ->withError ($ message , 403 , $ headers );
279+ return $ this ->withError ($ message , 403 , $ errorCode , $ headers );
269280 }
270281
271282 /**
272283 * Generates a response with a 500 HTTP header and a given message.
273284 *
274285 * @param string $message
275- * @param array $headers
286+ * @param int|string $errorCode
287+ * @param array $headers
276288 * @return mixed
277289 */
278- public function errorInternalError (string $ message = '' , array $ headers = [])
290+ public function errorInternalError (string $ message = '' , $ errorCode = null , array $ headers = [])
279291 {
280- return $ this ->withError ($ message , 500 , $ headers );
292+ return $ this ->withError ($ message , 500 , $ errorCode , $ headers );
281293 }
282294
283295 /**
284296 * Generates a response with a 404 HTTP header and a given message.
285297 *
286298 * @param string $message
299+ * @param int|string $errorCode
287300 * @param array $headers
288301 * @return mixed
289302 */
290- public function errorNotFound (string $ message = '' , array $ headers = [])
303+ public function errorNotFound (string $ message = '' , $ errorCode = null , array $ headers = [])
291304 {
292- return $ this ->withError ($ message , 404 , $ headers );
305+ return $ this ->withError ($ message , 404 , $ errorCode , $ headers );
293306 }
294307
295308 /**
296309 * Generates a response with a 401 HTTP header and a given message.
297310 *
298311 * @param string $message
312+ * @param int|string $errorCode
299313 * @param array $headers
300314 * @return mixed
301315 */
302- public function errorUnauthorized (string $ message = '' , array $ headers = [])
316+ public function errorUnauthorized (string $ message = '' , $ errorCode = null , array $ headers = [])
303317 {
304- return $ this ->withError ($ message , 401 , $ headers );
318+ return $ this ->withError ($ message , 401 , $ errorCode , $ headers );
305319 }
306320
307321 /**
308322 * Generates a response with a 400 HTTP header and a given message.
309323 *
310324 * @param array $message
325+ * @param int|array $errorCode
311326 * @param array $headers
312327 * @return mixed
313328 */
314- public function errorWrongArgs (array $ message , array $ headers = [])
329+ public function errorWrongArgs (array $ message , $ errorCode = null , array $ headers = [])
315330 {
316- return $ this ->withError ($ message , 400 , $ headers );
331+ return $ this ->withError ($ message , 400 , $ errorCode , $ headers );
317332 }
318333
319334 /**
320335 * Generates a response with a 410 HTTP header and a given message.
321336 *
322337 * @param string $message
338+ * @param int|string $errorCode
323339 * @param array $headers
324340 * @return mixed
325341 */
326- public function errorGone (string $ message = '' , array $ headers = [])
342+ public function errorGone (string $ message = '' , $ errorCode = null , array $ headers = [])
327343 {
328- return $ this ->withError ($ message , 410 , $ headers );
344+ return $ this ->withError ($ message , 410 , $ errorCode , $ headers );
329345 }
330346
331347 /**
332348 * Generates a response with a 405 HTTP header and a given message.
333349 *
334350 * @param string $message
351+ * @param int|string $errorCode
335352 * @param array $headers
336353 * @return mixed
337354 */
338- public function errorMethodNotAllowed (string $ message = '' , array $ headers = [])
355+ public function errorMethodNotAllowed (string $ message = '' , $ errorCode = null , array $ headers = [])
339356 {
340- return $ this ->withError ($ message , 405 , $ headers );
357+ return $ this ->withError ($ message , 405 , $ errorCode , $ headers );
341358 }
342359
343360 /**
344361 * Generates a Response with a 431 HTTP header and a given message.
345362 *
346363 * @param string $message
364+ * @param int|string $errorCode
347365 * @param array $headers
348366 * @return mixed
349367 */
350- public function errorUnwillingToProcess (string $ message = '' , array $ headers = [])
368+ public function errorUnwillingToProcess (string $ message = '' , $ errorCode = null , array $ headers = [])
351369 {
352- return $ this ->withError ($ message , 431 , $ headers );
370+ return $ this ->withError ($ message , 431 , $ errorCode , $ headers );
353371 }
354372
355373 /**
356374 * Generates a Response with a 422 HTTP header and a given message.
357375 *
358376 * @param string $message
377+ * @param int|string $errorCode
359378 * @param array $headers
360379 * @return mixed
361380 */
362- public function errorUnprocessable (string $ message = '' , array $ headers = [])
381+ public function errorUnprocessable (string $ message = '' , $ errorCode = null , array $ headers = [])
382+ {
383+ return $ this ->withError ($ message , 422 , $ errorCode , $ headers );
384+ }
385+
386+ /**
387+ * @return int|string
388+ */
389+ public function getErrorCode ()
363390 {
364- return $ this ->withError ($ message , 422 , $ headers );
391+ return $ this ->errorCode ;
392+ }
393+
394+ /**
395+ * @param $errorCode
396+ * @return $this
397+ */
398+ public function setErrorCode ($ errorCode )
399+ {
400+ $ this ->errorCode = $ errorCode ;
365401 }
366402
367403 /**
368404 * Set a valid status code.
369405 *
370- * @param int $code
406+ * @param int $statusCode
371407 * @throws InvalidArgumentException on an invalid status code.
372408 */
373- private function setStatusCode (int $ code )
409+ private function setStatusCode (int $ statusCode )
374410 {
375- if ($ code < static ::MIN_STATUS_CODE_VALUE
376- || $ code > static ::MAX_STATUS_CODE_VALUE
411+ if ($ statusCode < static ::MIN_STATUS_CODE_VALUE
412+ || $ statusCode > static ::MAX_STATUS_CODE_VALUE
377413 ) {
378414 throw new InvalidArgumentException (sprintf (
379415 'Invalid status code "%s"; must be an integer between %d and %d, inclusive ' ,
380- (is_scalar ($ code ) ? $ code : gettype ($ code )),
416+ (is_scalar ($ statusCode ) ? $ statusCode : gettype ($ statusCode )),
381417 static ::MIN_STATUS_CODE_VALUE ,
382418 static ::MAX_STATUS_CODE_VALUE
383419 ));
384420 }
385- $ this ->statusCode = $ code ;
421+ $ this ->statusCode = $ statusCode ;
386422 }
387423}
0 commit comments