Skip to content

Commit

Permalink
improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
fenric committed Apr 13, 2020
1 parent 6efe695 commit 8ffa127
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
51 changes: 51 additions & 0 deletions src/Exception/JsonException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types=1);

/**
* It's free open-source software released under the MIT License.
*
* @author Anatoly Fenric <[email protected]>
* @copyright Copyright (c) 2018, Anatoly Fenric
* @license https://github.com/sunrise-php/http-message/blob/master/LICENSE
* @link https://github.com/sunrise-php/http-message
*/

namespace Sunrise\Http\Message\Exception;

/**
* Import classes
*/
use RuntimeException;

/**
* Import functions
*/
use function json_last_error;
use function json_last_error_msg;

/**
* Import constants
*/
use const JSON_ERROR_NONE;

/**
* JsonException
*/
class JsonException extends RuntimeException
{

/**
* @return void
*
* @throws self
*/
public static function assert() : void
{
$code = json_last_error();

if (JSON_ERROR_NONE === $code) {
return;
}

throw new self(json_last_error_msg(), $code);
}
}
22 changes: 17 additions & 5 deletions src/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ public function createResponse(int $code = 200, string $reasonPhrase = '') : Res
*/
public function createHtmlResponse(int $status, $content) : ResponseInterface
{
$content = (string) $content;

$body = (new StreamFactory)->createStream();
$body->write((string) $content);
$body->write($content);

return (new Response)
->withStatus($status)
Expand All @@ -63,22 +65,32 @@ public function createHtmlResponse(int $status, $content) : ResponseInterface
}

/**
* Creates a JSON response object
* Creates a JSON response instance
*
* @param int $status
* @param mixed $payload
* @param int $options
* @param int $depth
*
* @return ResponseInterface
*
* @throws Exception\JsonException
*/
public function createJsonResponse(int $status, $payload, int $options = 0) : ResponseInterface
public function createJsonResponse(int $status, $payload, int $options = 0, int $depth = 512) : ResponseInterface
{
// clears a previous error...
json_encode(null);

$content = json_encode($payload, $options, $depth);

Exception\JsonException::assert();

$body = (new StreamFactory)->createStream();
$body->write(json_encode($payload, $options));
$body->write($content);

return (new Response)
->withStatus($status)
->withHeader('Content-Type', 'application/json')
->withHeader('Content-Type', 'application/json; charset=utf-8')
->withBody($body);
}
}
15 changes: 14 additions & 1 deletion tests/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Sunrise\Http\Message\Exception\JsonException;
use Sunrise\Http\Message\ResponseFactory;

/**
Expand Down Expand Up @@ -89,7 +90,19 @@ public function testCreateJsonResponse() : void

$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertSame(400, $response->getStatusCode());
$this->assertSame('application/json', $response->getHeaderLine('Content-Type'));
$this->assertSame('application/json; charset=utf-8', $response->getHeaderLine('Content-Type'));
$this->assertSame(json_encode($payload, $options), (string) $response->getBody());
}

/**
* @return void
*/
public function testCreateResponseWithInvalidJson() : void
{
$this->expectException(JsonException::class);
$this->expectExceptionMessage('Maximum stack depth exceeded');

$response = (new ResponseFactory)
->createJsonResponse(200, [[]], 0, 1);
}
}

0 comments on commit 8ffa127

Please sign in to comment.