Skip to content

Commit

Permalink
Treat an empty string as invalid JSON, as PHP 7 always does.
Browse files Browse the repository at this point in the history
  • Loading branch information
Crell committed Jul 24, 2016
1 parent 26fc886 commit 82c6676
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions src/HttpConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Crell\ApiProblem;

use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Stream;

/**
* Utility class to convert a problem object to an HTTP Response, using PSR-7.
Expand Down Expand Up @@ -42,10 +41,8 @@ public function __construct($pretty = false)
public function toJsonResponse(ApiProblem $problem, ResponseInterface $response)
{
$body = $response->getBody();
$body->rewind();
$body->write($problem->asJson($this->pretty));


return $this->toResponse($problem, $response)
->withHeader('Content-Type', 'application/problem+json')
->withBody($body);
Expand All @@ -64,16 +61,12 @@ public function toJsonResponse(ApiProblem $problem, ResponseInterface $response)
*/
public function toXmlResponse(ApiProblem $problem, ResponseInterface $response)
{
// @todo Figure out why Diactoros' stream implementation isn't handling
// this for us, and for that matter is there a way to avoid relying on
// Diactoros?
$stream = fopen('php://temp', 'w');
fwrite($stream, $problem->asXml($this->pretty));
rewind($stream);
$body = $response->getBody();
$body->write($problem->asXml($this->pretty));

return $this->toResponse($problem, $response)
->withHeader('Content-Type', 'application/problem+xml')
->withBody(new Stream($stream));
->withBody($body);
}

/**
Expand Down

0 comments on commit 82c6676

Please sign in to comment.