26
26
use function preg_replace ;
27
27
use function print_r ;
28
28
use function sprintf ;
29
+ use function str_contains ;
29
30
use function str_replace ;
30
- use function strpos ;
31
31
32
32
use const JSON_PARTIAL_OUTPUT_ON_ERROR ;
33
33
use const JSON_PRESERVE_ZERO_FRACTION ;
@@ -149,15 +149,6 @@ class ProblemDetailsResponseFactory
149
149
'application/*+xml ' ,
150
150
];
151
151
152
- /**
153
- * Whether or not to include debug details.
154
- *
155
- * Debug details are only included for responses created from throwables,
156
- * and include full exception details and previous exceptions and their
157
- * details.
158
- */
159
- private bool $ isDebug ;
160
-
161
152
/**
162
153
* JSON flags to use when generating JSON response payload.
163
154
*
@@ -177,38 +168,39 @@ class ProblemDetailsResponseFactory
177
168
*/
178
169
private ResponseFactoryInterface $ responseFactory ;
179
170
180
- /**
181
- * Flag to enable show exception details in detail field.
182
- *
183
- * Disabled by default for security reasons.
184
- */
185
- private bool $ exceptionDetailsInResponse ;
186
-
187
- /**
188
- * Default detail field value. Will be visible when
189
- * $exceptionDetailsInResponse disabled.
190
- *
191
- * Empty string by default
192
- */
193
- private string $ defaultDetailMessage ;
194
-
195
- /**
196
- * A map used to infer the "type" property based on the status code.
197
- *
198
- * Defaults to an empty map.
199
- */
200
- private array $ defaultTypesMap ;
201
-
202
171
/**
203
172
* @param (callable():ResponseInterface)|ResponseFactoryInterface $responseFactory
204
173
*/
205
174
public function __construct (
206
175
$ responseFactory ,
207
- bool $ isDebug = self ::EXCLUDE_THROWABLE_DETAILS ,
176
+ /**
177
+ * Whether or not to include debug details.
178
+ *
179
+ * Debug details are only included for responses created from throwables,
180
+ * and include full exception details and previous exceptions and their
181
+ * details.
182
+ */
183
+ private bool $ isDebug = self ::EXCLUDE_THROWABLE_DETAILS ,
208
184
?int $ jsonFlags = null ,
209
- bool $ exceptionDetailsInResponse = false ,
210
- string $ defaultDetailMessage = self ::DEFAULT_DETAIL_MESSAGE ,
211
- array $ defaultTypesMap = []
185
+ /**
186
+ * Flag to enable show exception details in detail field.
187
+ *
188
+ * Disabled by default for security reasons.
189
+ */
190
+ private bool $ exceptionDetailsInResponse = false ,
191
+ /**
192
+ * Default detail field value. Will be visible when
193
+ * $exceptionDetailsInResponse disabled.
194
+ *
195
+ * Empty string by default
196
+ */
197
+ private string $ defaultDetailMessage = self ::DEFAULT_DETAIL_MESSAGE ,
198
+ /**
199
+ * A map used to infer the "type" property based on the status code.
200
+ *
201
+ * Defaults to an empty map.
202
+ */
203
+ private array $ defaultTypesMap = []
212
204
) {
213
205
if (is_callable ($ responseFactory )) {
214
206
$ responseFactory = new CallableResponseFactoryDecorator (
@@ -217,7 +209,6 @@ public function __construct(
217
209
}
218
210
// Ensures type safety of the composed factory
219
211
$ this ->responseFactory = $ responseFactory ;
220
- $ this ->isDebug = $ isDebug ;
221
212
if (! $ jsonFlags ) {
222
213
$ jsonFlags = JSON_UNESCAPED_SLASHES
223
214
| JSON_UNESCAPED_UNICODE
@@ -227,10 +218,7 @@ public function __construct(
227
218
$ jsonFlags = JSON_PRETTY_PRINT | $ jsonFlags ;
228
219
}
229
220
}
230
- $ this ->jsonFlags = $ jsonFlags ;
231
- $ this ->exceptionDetailsInResponse = $ exceptionDetailsInResponse ;
232
- $ this ->defaultDetailMessage = $ defaultDetailMessage ;
233
- $ this ->defaultTypesMap = $ defaultTypesMap ;
221
+ $ this ->jsonFlags = $ jsonFlags ;
234
222
}
235
223
236
224
public function createResponse (
@@ -254,7 +242,7 @@ public function createResponse(
254
242
255
243
if ($ additional ) {
256
244
// ensure payload can be json_encoded
257
- array_walk_recursive ($ additional , function (&$ value ) {
245
+ array_walk_recursive ($ additional , static function (&$ value ): void {
258
246
if (is_resource ($ value )) {
259
247
$ value = print_r ($ value , true ) . ' of type ' . get_resource_type ($ value );
260
248
}
@@ -374,7 +362,7 @@ private function getResponseGenerator(ServerRequestInterface $request): callable
374
362
$ accept = $ request ->getHeaderLine ('Accept ' ) ?: '*/* ' ;
375
363
$ mediaType = (new Negotiator ())->getBest ($ accept , self ::NEGOTIATION_PRIORITIES );
376
364
377
- return ! $ mediaType || false === strpos ($ mediaType ->getValue (), 'json ' )
365
+ return ! $ mediaType || ! str_contains ($ mediaType ->getValue (), 'json ' )
378
366
? Closure::fromCallable ([$ this , 'generateXmlResponse ' ])
379
367
: Closure::fromCallable ([$ this , 'generateJsonResponse ' ]);
380
368
}
0 commit comments