Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 92be429

Browse files
committed
Merge branch 'feature/34-json-pretty-debug' into release-1.0.0
Close #34
2 parents fff24df + 0886d41 commit 92be429

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ Versions 0.3.0 and prior were released as "weierophinney/problem-details".
5050
modifies the constructor of `Zend\ProblemDetails\ProblemDetailsNotFoundHandler`;
5151
the `$responseFactory` argument is now required.
5252

53+
- [#34](https://github.com/zendframework/zend-problem-details/pull/34) updates
54+
the behavior when passing null as the `$jsonFlag` parameter to the
55+
`Zend\ProblemDetails\ProblemDetailsResponseFactory` constructor; in such
56+
situations, the default `json_encode()` flags will include `JSON_PRETTY_PRINT`
57+
only when the `$isDebug` argument is boolean `true`.
58+
5359
### Deprecated
5460

5561
- Nothing.
@@ -67,7 +73,7 @@ Versions 0.3.0 and prior were released as "weierophinney/problem-details".
6773
### Added
6874

6975
- [#30](https://github.com/zendframework/zend-problem-details/pull/30)
70-
adds PSR-15 support.
76+
adds PSR-15 support.
7177

7278
### Changed
7379

docs/book/response.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,11 @@ where:
116116
- `int $jsonFlags` is an integer bitmask of [JSON encoding
117117
constants](http://php.net/manual/en/json.constants.php) to use with
118118
`json_encode()` when generating JSON problem details. If you pass a `null`
119-
value, `JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
120-
JSON_PRESERVE_ZERO_FRACTION` will be used.
119+
value, and the `$isDebug` flag is true,
120+
`JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION`
121+
will be used; otherwise,
122+
`JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION`
123+
will be used.
121124
- `bool $exceptionDetailsInResponse` is a flag indicating whether or not to
122125
include exception details (in particular, the message) when creating the
123126
problem details response. By default, for non-`ProblemDetailsExceptionInterface`

src/ProblemDetailsResponseFactory.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ class ProblemDetailsResponseFactory
146146
/**
147147
* JSON flags to use when generating JSON response payload.
148148
*
149-
* Defaults to JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION
149+
* On non-debug mode:
150+
* defaults to JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION
151+
* On debug mode:
152+
* defaults to JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION
150153
*
151154
* @var int
152155
*/
@@ -191,8 +194,13 @@ public function __construct(
191194
return $responseFactory();
192195
};
193196
$this->isDebug = $isDebug;
194-
$this->jsonFlags = $jsonFlags
195-
?: JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION;
197+
if (! $jsonFlags) {
198+
$jsonFlags = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION;
199+
if ($isDebug) {
200+
$jsonFlags = JSON_PRETTY_PRINT | $jsonFlags;
201+
}
202+
}
203+
$this->jsonFlags = $jsonFlags;
196204
$this->exceptionDetailsInResponse = $exceptionDetailsInResponse;
197205
$this->defaultDetailMessage = $defaultDetailMessage;
198206
}

test/ProblemDetailsResponseFactoryFactoryTest.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function testLackOfConfigServiceResultsInFactoryUsingDefaults() : void
7777
$this->assertInstanceOf(ProblemDetailsResponseFactory::class, $factory);
7878
$this->assertAttributeSame(ProblemDetailsResponseFactory::EXCLUDE_THROWABLE_DETAILS, 'isDebug', $factory);
7979
$this->assertAttributeSame(
80-
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION,
80+
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION,
8181
'jsonFlags',
8282
$factory
8383
);
@@ -86,6 +86,25 @@ public function testLackOfConfigServiceResultsInFactoryUsingDefaults() : void
8686
$this->assertResponseFactoryReturns($response, $factory);
8787
}
8888

89+
public function testUsesPrettyPrintFlagOnEnabledDebugMode() : void
90+
{
91+
$this->container->has('config')->willReturn(true);
92+
$this->container->get('config')->willReturn([
93+
'debug' => true,
94+
]);
95+
$this->container->get(ResponseInterface::class)->willReturn(function () {
96+
});
97+
98+
$factoryFactory = new ProblemDetailsResponseFactoryFactory();
99+
$factory = $factoryFactory($this->container->reveal());
100+
101+
$this->assertAttributeSame(
102+
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION,
103+
'jsonFlags',
104+
$factory
105+
);
106+
}
107+
89108
public function testUsesDebugSettingFromConfigWhenPresent() : void
90109
{
91110
$this->container->has('config')->willReturn(true);

0 commit comments

Comments
 (0)