Skip to content
Permalink
Browse files

Replace json_*() with Nette\Utils\Json

  • Loading branch information...
mabar committed Sep 26, 2019
1 parent 3475b4b commit 345b1ac1a96718da0796b5b6ec22bb7aba47cc31
Showing with 22 additions and 5 deletions.
  1. +2 −1 composer.json
  2. +2 −1 src/Extra/ExtraRequestTrait.php
  3. +4 −3 src/Extra/ExtraResponseTrait.php
  4. +14 −0 tests/cases/Psr7Response.phpt
@@ -20,7 +20,8 @@
],
"require": {
"php": "^7.2",
"guzzlehttp/psr7": "^1.5.2"
"guzzlehttp/psr7": "^1.5.2",
"nette/utils": "~3.0.0"
},
"require-dev": {
"nette/application": "~3.0.0",
@@ -4,6 +4,7 @@
use Contributte\Psr7\Psr7Stream;
use Contributte\Psr7\Psr7Uri;
use Nette\Utils\Json;
/**
* @method Psr7Stream getBody()
@@ -39,7 +40,7 @@ public function getContentsCopy()
*/
public function getJsonBody(bool $assoc = true)
{
return json_decode((string) $this->getBody(), $assoc);
return Json::decode((string) $this->getBody(), $assoc ? Json::FORCE_ARRAY : 0);
}
/**
@@ -4,6 +4,7 @@
use Contributte\Psr7\Psr7Stream;
use JsonSerializable;
use Nette\Utils\Json;
/**
* @method Psr7Stream getBody()
@@ -50,7 +51,7 @@ public function writeBody($body)
public function writeJsonBody(array $data)
{
return $this
->writeBody(json_encode($data))
->writeBody(Json::encode($data))
->withHeader('Content-Type', 'application/json');
}
@@ -60,7 +61,7 @@ public function writeJsonBody(array $data)
public function writeJsonObject(JsonSerializable $object)
{
return $this
->writeBody(json_encode($object))
->writeBody(Json::encode($object))
->withHeader('Content-Type', 'application/json');
}
@@ -69,7 +70,7 @@ public function writeJsonObject(JsonSerializable $object)
*/
public function getJsonBody(bool $assoc = true)
{
return json_decode($this->getContents(), $assoc);
return Json::decode($this->getContents(), $assoc ? Json::FORCE_ARRAY : 0);
}
/**
@@ -56,6 +56,13 @@ test(function (): void {
Assert::equal(['foo' => 'bar'], $response->getJsonBody());
});
test(function (): void {
$response = Psr7ResponseFactory::fromGlobal();
$response = $response->writeJsonBody(['foo' => 'ěščřžýáíé']);
Assert::equal(['foo' => 'ěščřžýáíé'], $response->getJsonBody());
});
// writeJsonObject
test(function (): void {
$response = Psr7ResponseFactory::fromGlobal();
@@ -64,6 +71,13 @@ test(function (): void {
Assert::equal(['foo' => 'bar'], $response->getJsonBody());
});
test(function (): void {
$response = Psr7ResponseFactory::fromGlobal();
$jsonObject = new JsonObject('ěščřžýáíé');
$response = $response->writeJsonObject($jsonObject);
Assert::equal(['foo' => 'ěščřžýáíé'], $response->getJsonBody());
});
// appendBody
test(function (): void {
$response = Psr7ResponseFactory::fromGlobal();

0 comments on commit 345b1ac

Please sign in to comment.
You can’t perform that action at this time.