Skip to content

Commit 9c3d3e1

Browse files
authored
Merge pull request #9 from eugene-borovov/content-type
Encoder content type
2 parents 1443256 + ed568c3 commit 9c3d3e1

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

src/Encoder/EncoderInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@ interface EncoderInterface
1919
* @return string The encoded string
2020
*/
2121
public function encode($data): string;
22+
23+
/**
24+
* Return MIME compatible content type.
25+
*
26+
* @return string Response content type
27+
*/
28+
public function getContentType(): string;
2229
}

src/Encoder/JsonEncoder.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,14 @@ public function encode($data): string
3030

3131
return $result;
3232
}
33+
34+
/**
35+
* Return MIME compatible content type.
36+
*
37+
* @return string Response content type
38+
*/
39+
public function getContentType(): string
40+
{
41+
return 'application/json';
42+
}
3343
}

src/Middleware/ValidationExceptionMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
6363
} catch (ValidationException $exception) {
6464
$response = $this->responseFactory->createResponse()
6565
->withStatus((int)$exception->getCode())
66-
->withHeader('Content-Type', 'application/json');
66+
->withHeader('Content-Type', $this->encoder->getContentType());
6767

6868
$validationResult = $exception->getValidationResult();
6969
if ($validationResult) {

tests/Encoder/JsonEncoderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,14 @@ public function testInvalidEncoding(): void
3838
$encoder = new JsonEncoder();
3939
$encoder->encode(['key' => "\x00\x81"]);
4040
}
41+
42+
/**
43+
* Test.
44+
*/
45+
public function testContentType(): void
46+
{
47+
$encoder = new JsonEncoder();
48+
49+
$this->assertEquals('application/json', $encoder->getContentType());
50+
}
4151
}

0 commit comments

Comments
 (0)