Skip to content

Commit 7dcc115

Browse files
committed
separate json encode (removes stream dependency)
1 parent 7eac973 commit 7dcc115

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/JsonResponse.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55

66
namespace Valar;
77

8-
use Valar\Stream\JsonStream;
9-
108
class JsonResponse
119
extends Response
1210
{
11+
/**
12+
* JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES
13+
*/
14+
const ENCODE_OPTIONS = 79;
15+
1316
/**
1417
* @param $data
1518
* @param int $status
1619
* @param array $headers
1720
*/
1821
function __construct($data, $status = 200, array $headers = [])
1922
{
20-
parent::__construct(JsonStream::encode($data), $status, $headers + ['Content-Type' => 'application/json']);
23+
parent::__construct(
24+
json_encode($data, static::ENCODE_OPTIONS), $status, $headers + ['Content-Type' => 'application/json']
25+
);
2126
}
2227
}

src/Stream/JsonStream.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,17 @@ class JsonStream
1111
extends Stream
1212
{
1313
/**
14-
* @param $data
14+
* JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES
1515
*/
16-
function __construct($data)
17-
{
18-
parent::__construct('php://memory', 'wb+');
19-
$this->write($this->encode($data));
20-
$this->rewind();
21-
}
16+
const ENCODE_OPTIONS = 79;
2217

2318
/**
2419
* @param $data
25-
* @return string
2620
*/
27-
static function encode($data)
21+
function __construct($data)
2822
{
29-
return json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES);
23+
parent::__construct('php://memory', 'wb+');
24+
$this->write(json_encode($data, static::ENCODE_OPTIONS));
25+
$this->rewind();
3026
}
3127
}

0 commit comments

Comments
 (0)