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

Commit 8499719

Browse files
committed
Adds support for a default types map
1 parent 23f9553 commit 8499719

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/ProblemDetailsResponseFactory.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,22 @@ class ProblemDetailsResponseFactory
204204
*/
205205
private $defaultDetailMessage;
206206

207+
/**
208+
* A map used to infer the "type" property based on the status code.
209+
*
210+
* Defaults to an empty map.
211+
*
212+
* @var array
213+
*/
214+
private $defaultTypes;
215+
207216
public function __construct(
208217
callable $responseFactory,
209218
bool $isDebug = self::EXCLUDE_THROWABLE_DETAILS,
210219
int $jsonFlags = null,
211220
bool $exceptionDetailsInResponse = false,
212-
string $defaultDetailMessage = self::DEFAULT_DETAIL_MESSAGE
221+
string $defaultDetailMessage = self::DEFAULT_DETAIL_MESSAGE,
222+
array $defaultTypes = []
213223
) {
214224
// Ensures type safety of the composed factory
215225
$this->responseFactory = function () use ($responseFactory) : ResponseInterface {
@@ -228,6 +238,7 @@ public function __construct(
228238
$this->jsonFlags = $jsonFlags;
229239
$this->exceptionDetailsInResponse = $exceptionDetailsInResponse;
230240
$this->defaultDetailMessage = $defaultDetailMessage;
241+
$this->defaultTypes = $defaultTypes;
231242
}
232243

233244
public function createResponse(
@@ -392,7 +403,7 @@ private function createTitleFromStatus(int $status) : string
392403

393404
private function createTypeFromStatus(int $status) : string
394405
{
395-
return sprintf('https://httpstatus.es/%s', $status);
406+
return $this->defaultTypes[$status] ?? sprintf('https://httpstatus.es/%s', $status);
396407
}
397408

398409
private function createThrowableDetail(Throwable $e) : array

src/ProblemDetailsResponseFactoryFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ public function __invoke(ContainerInterface $container) : ProblemDetailsResponse
2121

2222
$problemDetailsConfig = $config['problem-details'] ?? [];
2323
$jsonFlags = $problemDetailsConfig['json_flags'] ?? null;
24+
$defaultTypes = $problemDetailsConfig['default_types'] ?? [];
2425

2526
return new ProblemDetailsResponseFactory(
2627
$container->get(ResponseInterface::class),
2728
$includeThrowableDetail,
2829
$jsonFlags,
29-
$includeThrowableDetail
30+
$includeThrowableDetail,
31+
ProblemDetailsResponseFactory::DEFAULT_DETAIL_MESSAGE,
32+
$defaultTypes
3033
);
3134
}
3235
}

0 commit comments

Comments
 (0)