This repository was archived by the owner on Jan 29, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +43
-7
lines changed Expand file tree Collapse file tree 4 files changed +43
-7
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 `
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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 );
You can’t perform that action at this time.
0 commit comments