Skip to content

Commit

Permalink
Merge pull request #278 from pine3ree/patch-1
Browse files Browse the repository at this point in the history
add custom json-flags ctor-param
  • Loading branch information
philipobenito committed May 18, 2020
2 parents 021497f + ae4d8dc commit b2d3cf6
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Strategy/JsonStrategy.php
Expand Up @@ -10,6 +10,10 @@
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface};
use Throwable;

use function is_array;
use function is_object;
use function json_encode;

class JsonStrategy extends AbstractStrategy implements ContainerAwareInterface
{
use ContainerAwareTrait;
Expand All @@ -19,14 +23,20 @@ class JsonStrategy extends AbstractStrategy implements ContainerAwareInterface
*/
protected $responseFactory;

/**
* @var int
*/
protected $jsonFlags;

/**
* Construct.
*
* @param ResponseFactoryInterface $responseFactory
*/
public function __construct(ResponseFactoryInterface $responseFactory)
public function __construct(ResponseFactoryInterface $responseFactory, int $jsonFlags = 0)
{
$this->responseFactory = $responseFactory;
$this->jsonFlags = $jsonFlags;

$this->addDefaultResponseHeader('content-type', 'application/json');
}
Expand All @@ -40,7 +50,7 @@ public function invokeRouteCallable(Route $route, ServerRequestInterface $reques
$response = $controller($request, $route->getVars());

if ($this->isJsonEncodable($response)) {
$body = json_encode($response);
$body = json_encode($response, $this->jsonFlags);
$response = $this->responseFactory->createResponse();
$response->getBody()->write($body);
}
Expand Down

0 comments on commit b2d3cf6

Please sign in to comment.