-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonResponse.php
64 lines (57 loc) · 1.55 KB
/
JsonResponse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <mihail@kodeart.com>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*
*/
namespace Koded\Http;
use Koded\Http\Interfaces\HttpStatus;
use Koded\Stdlib\Serializer\JsonSerializer;
/**
* HTTP response object for JSON format.
*/
class JsonResponse extends ServerResponse
{
public function __construct(
mixed $content = null,
int $statusCode = HttpStatus::OK,
array $headers = [])
{
parent::__construct(
$this->process($content),
$statusCode,
$headers);
}
/**
* Converts the <, >, ', & and " to UTF-8 variants.
* The content is safe for embedding it into HTML.
*
* @return JsonResponse
*/
public function safe(): JsonResponse
{
$this->stream = create_stream(\json_encode(
\json_decode($this->stream->getContents(), true),
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
));
return $this;
}
public function getContentType(): string
{
return $this->getHeaderLine('Content-Type') ?: 'application/json';
}
private function process(mixed $content): mixed
{
if (\is_array($content)) {
return \json_encode($content, JsonSerializer::OPTIONS);
}
if (\is_iterable($content)) {
return \json_encode(\iterator_to_array($content), JsonSerializer::OPTIONS);
}
return $content;
}
}