Skip to content

Commit

Permalink
Replace json_*() with Nette\Utils\Json
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Sep 26, 2019
1 parent 3475b4b commit 345b1ac
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/Extra/ExtraRequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Contributte\Psr7\Psr7Stream;
use Contributte\Psr7\Psr7Uri;
use Nette\Utils\Json;

/**
* @method Psr7Stream getBody()
Expand Down Expand Up @@ -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);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Extra/ExtraResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Contributte\Psr7\Psr7Stream;
use JsonSerializable;
use Nette\Utils\Json;

/**
* @method Psr7Stream getBody()
Expand Down Expand Up @@ -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');
}

Expand All @@ -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');
}

Expand All @@ -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);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/cases/Psr7Response.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit 345b1ac

Please sign in to comment.