Skip to content

Commit 1615abb

Browse files
authored
Merge pull request #4 from harryosmar/json-encode
add json encode method
2 parents 7a5ef5f + 6220030 commit 1615abb

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

src/Response.php

+35-5
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
use PhpRestfulApiResponse\Contracts\PhpRestfulApiResponse;
1717
use Zend\Diactoros\MessageTrait;
1818
use InvalidArgumentException;
19+
use Zend\Diactoros\Response\InjectContentTypeTrait;
20+
use Zend\Diactoros\Response\JsonResponse;
1921

2022
class Response implements PhpRestfulApiResponse
2123
{
22-
use MessageTrait;
24+
use MessageTrait, InjectContentTypeTrait;
2325

2426
const MIN_STATUS_CODE_VALUE = 100;
2527
const MAX_STATUS_CODE_VALUE = 599;
@@ -130,6 +132,7 @@ public function __construct($body = 'php://memory', int $status = 200, $errorCod
130132
$this->setStatusCode($status);
131133
$this->setErrorCode($errorCode);
132134
$this->stream = $this->getStream($body, 'wb+');
135+
$headers = $this->injectContentType('application/json', $headers);
133136
$this->setHeaders($headers);
134137
}
135138

@@ -176,8 +179,7 @@ public function withArray($data, $code = 200, array $headers = [])
176179
{
177180
$new = clone $this;
178181
$new->setStatusCode($code);
179-
$new->getBody()->write(json_encode($data));
180-
$new = $new->withHeader('Content-Type', 'application/json');
182+
$new->getBody()->write($this->jsonEncode($data));
181183
$new->headers = array_merge($new->headers, $headers);
182184
return $new;
183185
}
@@ -250,7 +252,7 @@ public function withError($message, int $statusCode, $errorCode = null, array $h
250252
$new->setStatusCode($statusCode);
251253
$new->setErrorCode($errorCode);
252254
$new->getBody()->write(
253-
json_encode(
255+
$this->jsonEncode(
254256
[
255257
'error' => array_filter([
256258
'http_code' => $new->statusCode,
@@ -261,7 +263,6 @@ public function withError($message, int $statusCode, $errorCode = null, array $h
261263
]
262264
)
263265
);
264-
$new = $new->withHeader('Content-Type', 'application/json');
265266
$new->headers = array_merge($new->headers, $headers);
266267
return $new;
267268
}
@@ -420,4 +421,33 @@ private function setStatusCode(int $statusCode)
420421
}
421422
$this->statusCode = $statusCode;
422423
}
424+
425+
/**
426+
* Encode the provided data to JSON.
427+
*
428+
* @param mixed $data
429+
* @return string
430+
* @throws InvalidArgumentException if unable to encode the $data to JSON.
431+
*/
432+
private function jsonEncode($data)
433+
{
434+
if (is_resource($data)) {
435+
throw new InvalidArgumentException('Cannot JSON encode resources');
436+
}
437+
438+
// Clear json_last_error()
439+
json_encode(null);
440+
441+
$json = json_encode($data, JsonResponse::DEFAULT_JSON_FLAGS);
442+
443+
if (JSON_ERROR_NONE !== json_last_error()) {
444+
throw new InvalidArgumentException(sprintf(
445+
'Unable to encode data to JSON in %s: %s',
446+
__CLASS__,
447+
json_last_error_msg()
448+
));
449+
}
450+
451+
return $json;
452+
}
423453
}

0 commit comments

Comments
 (0)