@@ -113,15 +113,22 @@ class Response implements PhpRestfulApiResponse
113
113
*/
114
114
private $ statusCode ;
115
115
116
+ /**
117
+ * @var int|string
118
+ */
119
+ private $ errorCode ;
120
+
116
121
/**
117
122
* Response constructor.
118
123
* @param string $body
119
124
* @param int $status
125
+ * @param int $errorCode
120
126
* @param array $headers
121
127
*/
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 = [])
123
129
{
124
130
$ this ->setStatusCode ($ status );
131
+ $ this ->setErrorCode ($ errorCode );
125
132
$ this ->stream = $ this ->getStream ($ body , 'wb+ ' );
126
133
$ this ->setHeaders ($ headers );
127
134
}
@@ -232,19 +239,22 @@ public function withCollection($data, $transformer, $code = 200, $resourceKey =
232
239
* Response for errors
233
240
*
234
241
* @param string|array $message
235
- * @param int $code
242
+ * @param int $statusCode
243
+ * @param int|string $errorCode
236
244
* @param array $headers
237
245
* @return mixed
238
246
*/
239
- public function withError ($ message , $ code , array $ headers = [])
247
+ public function withError ($ message , int $ statusCode , $ errorCode = null , array $ headers = [])
240
248
{
241
249
$ new = clone $ this ;
242
- $ new ->setStatusCode ($ code );
250
+ $ new ->setStatusCode ($ statusCode );
251
+ $ new ->setErrorCode ($ errorCode );
243
252
$ new ->getBody ()->write (
244
253
json_encode (
245
254
[
246
255
'error ' => array_filter ([
247
256
'http_code ' => $ new ->statusCode ,
257
+ 'code ' => $ errorCode ,
248
258
'phrase ' => $ new ->getReasonPhrase (),
249
259
'message ' => $ message
250
260
])
@@ -260,128 +270,154 @@ public function withError($message, $code, array $headers = [])
260
270
* Generates a response with a 403 HTTP header and a given message.
261
271
*
262
272
* @param string $message
273
+ * @param int|string $errorCode
263
274
* @param array $headers
264
275
* @return mixed
265
276
*/
266
- public function errorForbidden (string $ message = '' , array $ headers = [])
277
+ public function errorForbidden (string $ message = '' , $ errorCode = null , array $ headers = [])
267
278
{
268
- return $ this ->withError ($ message , 403 , $ headers );
279
+ return $ this ->withError ($ message , 403 , $ errorCode , $ headers );
269
280
}
270
281
271
282
/**
272
283
* Generates a response with a 500 HTTP header and a given message.
273
284
*
274
285
* @param string $message
275
- * @param array $headers
286
+ * @param int|string $errorCode
287
+ * @param array $headers
276
288
* @return mixed
277
289
*/
278
- public function errorInternalError (string $ message = '' , array $ headers = [])
290
+ public function errorInternalError (string $ message = '' , $ errorCode = null , array $ headers = [])
279
291
{
280
- return $ this ->withError ($ message , 500 , $ headers );
292
+ return $ this ->withError ($ message , 500 , $ errorCode , $ headers );
281
293
}
282
294
283
295
/**
284
296
* Generates a response with a 404 HTTP header and a given message.
285
297
*
286
298
* @param string $message
299
+ * @param int|string $errorCode
287
300
* @param array $headers
288
301
* @return mixed
289
302
*/
290
- public function errorNotFound (string $ message = '' , array $ headers = [])
303
+ public function errorNotFound (string $ message = '' , $ errorCode = null , array $ headers = [])
291
304
{
292
- return $ this ->withError ($ message , 404 , $ headers );
305
+ return $ this ->withError ($ message , 404 , $ errorCode , $ headers );
293
306
}
294
307
295
308
/**
296
309
* Generates a response with a 401 HTTP header and a given message.
297
310
*
298
311
* @param string $message
312
+ * @param int|string $errorCode
299
313
* @param array $headers
300
314
* @return mixed
301
315
*/
302
- public function errorUnauthorized (string $ message = '' , array $ headers = [])
316
+ public function errorUnauthorized (string $ message = '' , $ errorCode = null , array $ headers = [])
303
317
{
304
- return $ this ->withError ($ message , 401 , $ headers );
318
+ return $ this ->withError ($ message , 401 , $ errorCode , $ headers );
305
319
}
306
320
307
321
/**
308
322
* Generates a response with a 400 HTTP header and a given message.
309
323
*
310
324
* @param array $message
325
+ * @param int|array $errorCode
311
326
* @param array $headers
312
327
* @return mixed
313
328
*/
314
- public function errorWrongArgs (array $ message , array $ headers = [])
329
+ public function errorWrongArgs (array $ message , $ errorCode = null , array $ headers = [])
315
330
{
316
- return $ this ->withError ($ message , 400 , $ headers );
331
+ return $ this ->withError ($ message , 400 , $ errorCode , $ headers );
317
332
}
318
333
319
334
/**
320
335
* Generates a response with a 410 HTTP header and a given message.
321
336
*
322
337
* @param string $message
338
+ * @param int|string $errorCode
323
339
* @param array $headers
324
340
* @return mixed
325
341
*/
326
- public function errorGone (string $ message = '' , array $ headers = [])
342
+ public function errorGone (string $ message = '' , $ errorCode = null , array $ headers = [])
327
343
{
328
- return $ this ->withError ($ message , 410 , $ headers );
344
+ return $ this ->withError ($ message , 410 , $ errorCode , $ headers );
329
345
}
330
346
331
347
/**
332
348
* Generates a response with a 405 HTTP header and a given message.
333
349
*
334
350
* @param string $message
351
+ * @param int|string $errorCode
335
352
* @param array $headers
336
353
* @return mixed
337
354
*/
338
- public function errorMethodNotAllowed (string $ message = '' , array $ headers = [])
355
+ public function errorMethodNotAllowed (string $ message = '' , $ errorCode = null , array $ headers = [])
339
356
{
340
- return $ this ->withError ($ message , 405 , $ headers );
357
+ return $ this ->withError ($ message , 405 , $ errorCode , $ headers );
341
358
}
342
359
343
360
/**
344
361
* Generates a Response with a 431 HTTP header and a given message.
345
362
*
346
363
* @param string $message
364
+ * @param int|string $errorCode
347
365
* @param array $headers
348
366
* @return mixed
349
367
*/
350
- public function errorUnwillingToProcess (string $ message = '' , array $ headers = [])
368
+ public function errorUnwillingToProcess (string $ message = '' , $ errorCode = null , array $ headers = [])
351
369
{
352
- return $ this ->withError ($ message , 431 , $ headers );
370
+ return $ this ->withError ($ message , 431 , $ errorCode , $ headers );
353
371
}
354
372
355
373
/**
356
374
* Generates a Response with a 422 HTTP header and a given message.
357
375
*
358
376
* @param string $message
377
+ * @param int|string $errorCode
359
378
* @param array $headers
360
379
* @return mixed
361
380
*/
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 ()
363
390
{
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 ;
365
401
}
366
402
367
403
/**
368
404
* Set a valid status code.
369
405
*
370
- * @param int $code
406
+ * @param int $statusCode
371
407
* @throws InvalidArgumentException on an invalid status code.
372
408
*/
373
- private function setStatusCode (int $ code )
409
+ private function setStatusCode (int $ statusCode )
374
410
{
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
377
413
) {
378
414
throw new InvalidArgumentException (sprintf (
379
415
'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 )),
381
417
static ::MIN_STATUS_CODE_VALUE ,
382
418
static ::MAX_STATUS_CODE_VALUE
383
419
));
384
420
}
385
- $ this ->statusCode = $ code ;
421
+ $ this ->statusCode = $ statusCode ;
386
422
}
387
423
}
0 commit comments