This repository was archived by the owner on Jan 29, 2020. It is now read-only.
File tree 4 files changed +43
-7
lines changed
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".
50
50
modifies the constructor of ` Zend\ProblemDetails\ProblemDetailsNotFoundHandler ` ;
51
51
the ` $responseFactory ` argument is now required.
52
52
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
+
53
59
### Deprecated
54
60
55
61
- Nothing.
@@ -67,7 +73,7 @@ Versions 0.3.0 and prior were released as "weierophinney/problem-details".
67
73
### Added
68
74
69
75
- [ #30 ] ( https://github.com/zendframework/zend-problem-details/pull/30 )
70
- adds PSR-15 support.
76
+ adds PSR-15 support.
71
77
72
78
### Changed
73
79
Original file line number Diff line number Diff line change @@ -116,8 +116,11 @@ where:
116
116
- ` int $jsonFlags ` is an integer bitmask of [ JSON encoding
117
117
constants] ( http://php.net/manual/en/json.constants.php ) to use with
118
118
` 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.
121
124
- ` bool $exceptionDetailsInResponse ` is a flag indicating whether or not to
122
125
include exception details (in particular, the message) when creating the
123
126
problem details response. By default, for non-` ProblemDetailsExceptionInterface `
Original file line number Diff line number Diff line change @@ -146,7 +146,10 @@ class ProblemDetailsResponseFactory
146
146
/**
147
147
* JSON flags to use when generating JSON response payload.
148
148
*
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
150
153
*
151
154
* @var int
152
155
*/
@@ -191,8 +194,13 @@ public function __construct(
191
194
return $ responseFactory ();
192
195
};
193
196
$ 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 ;
196
204
$ this ->exceptionDetailsInResponse = $ exceptionDetailsInResponse ;
197
205
$ this ->defaultDetailMessage = $ defaultDetailMessage ;
198
206
}
Original file line number Diff line number Diff line change @@ -77,7 +77,7 @@ public function testLackOfConfigServiceResultsInFactoryUsingDefaults() : void
77
77
$ this ->assertInstanceOf (ProblemDetailsResponseFactory::class, $ factory );
78
78
$ this ->assertAttributeSame (ProblemDetailsResponseFactory::EXCLUDE_THROWABLE_DETAILS , 'isDebug ' , $ factory );
79
79
$ 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 ,
81
81
'jsonFlags ' ,
82
82
$ factory
83
83
);
@@ -86,6 +86,25 @@ public function testLackOfConfigServiceResultsInFactoryUsingDefaults() : void
86
86
$ this ->assertResponseFactoryReturns ($ response , $ factory );
87
87
}
88
88
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
+
89
108
public function testUsesDebugSettingFromConfigWhenPresent () : void
90
109
{
91
110
$ this ->container ->has ('config ' )->willReturn (true );
You can’t perform that action at this time.
0 commit comments