Skip to content

Commit ab4d12c

Browse files
authored
Merge pull request #1 from harryosmar/add-error-code
add error code parameter
2 parents 29fb571 + 057f273 commit ab4d12c

File tree

3 files changed

+122
-41
lines changed

3 files changed

+122
-41
lines changed

src/Contracts/PhpRestfulApiResponse.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,90 +50,100 @@ public function withCollection($data, $transformer, $code = 200, $resourceKey =
5050
* Generates a response with custom code HTTP header and a given message.
5151
*
5252
* @param $message
53-
* @param $code
53+
* @param int $statusCode
54+
* @param int|string $errorCode
5455
* @param array $headers
5556
* @return mixed
5657
*/
57-
public function withError($message, $code, array $headers = []);
58+
public function withError($message, int $statusCode, $errorCode, array $headers = []);
5859

5960
/**
6061
* Generates a response with a 403 HTTP header and a given message.
6162
*
6263
* @param string $message
64+
* @param int|string $errorCode
6365
* @param array $headers
6466
* @return mixed
6567
*/
66-
public function errorForbidden(string $message = '', array $headers = []);
68+
public function errorForbidden(string $message = '', $errorCode, array $headers = []);
6769

6870
/**
6971
* Generates a response with a 500 HTTP header and a given message.
7072
*
7173
* @param string $message
74+
* @param int|string $errorCode
7275
* @param array $headers
7376
* @return mixed
7477
*/
75-
public function errorInternalError(string $message = '', array $headers = []);
78+
public function errorInternalError(string $message = '', $errorCode, array $headers = []);
7679

7780
/**
7881
* Generates a response with a 404 HTTP header and a given message.
7982
*
8083
* @param string $message
84+
* @param int|string $errorCode
8185
* @param array $headers
8286
* @return mixed
8387
*/
84-
public function errorNotFound(string $message = '', array $headers = []);
88+
public function errorNotFound(string $message = '', $errorCode, array $headers = []);
8589

8690
/**
8791
* Generates a response with a 401 HTTP header and a given message.
8892
*
8993
* @param string $message
94+
* @param int|string $errorCode
9095
* @param array $headers
9196
* @return mixed
9297
*/
93-
public function errorUnauthorized(string $message = '', array $headers = []);
98+
public function errorUnauthorized(string $message = '', $errorCode, array $headers = []);
9499

95100
/**
96101
* Generates a response with a 400 HTTP header and a given message.
97102
*
98103
* @param array $message
104+
* @param int|array $errorCode
99105
* @param array $headers
100106
* @return mixed
101107
*/
102-
public function errorWrongArgs(array $message, array $headers = []);
108+
public function errorWrongArgs(array $message, $errorCode, array $headers = []);
103109

104110
/**
105111
* Generates a response with a 410 HTTP header and a given message.
106112
*
107113
* @param string $message
114+
* @param int|string $errorCode
108115
* @param array $headers
109116
* @return mixed
110117
*/
111-
public function errorGone(string $message = '', array $headers = []);
118+
public function errorGone(string $message = '', $errorCode, array $headers = []);
112119

113120
/**
114121
* Generates a response with a 405 HTTP header and a given message.
115122
*
116123
* @param string $message
124+
* @param int|string $errorCode
117125
* @param array $headers
118126
* @return mixed
119127
*/
120-
public function errorMethodNotAllowed(string $message = '', array $headers = []);
128+
public function errorMethodNotAllowed(string $message = '', $errorCode, array $headers = []);
121129

122130
/**
123131
* Generates a Response with a 431 HTTP header and a given message.
124132
*
125133
* @param string $message
134+
* @param int|string $errorCode
126135
* @param array $headers
127136
* @return mixed
128137
*/
129-
public function errorUnwillingToProcess(string $message = '', array $headers = []);
138+
public function errorUnwillingToProcess(string $message = '', $errorCode, array $headers = []);
130139

131140
/**
132141
* Generates a Response with a 422 HTTP header and a given message.
133142
*
134143
* @param string $message
144+
* @param int|string $errorCode
135145
* @param array $headers
136146
* @return mixed
137147
*/
138-
public function errorUnprocessable(string $message = '', array $headers = []);
148+
public function errorUnprocessable(string $message = '', $errorCode, array $headers = []);
139149
}

src/Response.php

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)