From a10a37fc1e77a9952552b3889974ad1e33762722 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 2 Mar 2024 17:31:59 +0300 Subject: [PATCH 1/5] implement --- .../PlainTextDataResponseFormatter.php | 47 +++++++++++++++++++ .../FormatDataResponseAsPlainText.php | 19 ++++++++ 2 files changed, 66 insertions(+) create mode 100644 src/Formatter/PlainTextDataResponseFormatter.php create mode 100644 src/Middleware/FormatDataResponseAsPlainText.php diff --git a/src/Formatter/PlainTextDataResponseFormatter.php b/src/Formatter/PlainTextDataResponseFormatter.php new file mode 100644 index 0000000..393ac92 --- /dev/null +++ b/src/Formatter/PlainTextDataResponseFormatter.php @@ -0,0 +1,47 @@ +getData(); + + if (!is_scalar($data) && $data !== null && !$data instanceof Stringable) { + throw new LogicException(sprintf( + 'Data must be either a scalar value, null, or a stringable object. %s given.', + get_debug_type($data), + )); + } + + return $this->addToResponse($dataResponse->getResponse(), empty($data) ? null : (string) $data); + } +} diff --git a/src/Middleware/FormatDataResponseAsPlainText.php b/src/Middleware/FormatDataResponseAsPlainText.php new file mode 100644 index 0000000..576d508 --- /dev/null +++ b/src/Middleware/FormatDataResponseAsPlainText.php @@ -0,0 +1,19 @@ + Date: Sat, 2 Mar 2024 17:33:21 +0300 Subject: [PATCH 2/5] readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9a5136f..aed972c 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ response. The package could be installed via composer: ```shell -composer require yiisoft/data-response --prefer-dist +composer require yiisoft/data-response ``` ## General usage @@ -86,6 +86,7 @@ The following formatters are available: - `HtmlDataResponseFormatter` - `JsonDataResponseFormatter` - `XmlDataResponseFormatter` +- `PlainTextDataResponseFormatter` ### Middleware From d3a5f0a99c1a37f3062676a0dde0ebdab7e864a7 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 2 Mar 2024 17:42:22 +0300 Subject: [PATCH 3/5] test --- .../PlainTextDataResponseFormatterTest.php | 106 ++++++++++++++++++ .../FormatDataResponseAsPlainTextTest.php | 28 +++++ 2 files changed, 134 insertions(+) create mode 100644 tests/Formatter/PlainTextDataResponseFormatterTest.php create mode 100644 tests/Middleware/FormatDataResponseAsPlainTextTest.php diff --git a/tests/Formatter/PlainTextDataResponseFormatterTest.php b/tests/Formatter/PlainTextDataResponseFormatterTest.php new file mode 100644 index 0000000..11772e6 --- /dev/null +++ b/tests/Formatter/PlainTextDataResponseFormatterTest.php @@ -0,0 +1,106 @@ +createDataResponse('test'); + $result = (new PlainTextDataResponseFormatter())->format($dataResponse); + $result->getBody()->rewind(); + + $this->assertSame( + 'test', + $result->getBody()->getContents(), + ); + $this->assertSame(['text/plain; charset=UTF-8'], $result->getHeader('Content-Type')); + } + + public function testWithEncoding(): void + { + $dataResponse = $this->createDataResponse('test'); + $result = (new PlainTextDataResponseFormatter()) + ->withEncoding('ISO-8859-1') + ->format($dataResponse); + $result->getBody()->rewind(); + + $this->assertSame( + 'test', + $result->getBody()->getContents(), + ); + $this->assertSame(['text/plain; charset=ISO-8859-1'], $result->getHeader('Content-Type')); + } + + public function testWithContentType(): void + { + $dataResponse = $this->createDataResponse('test'); + $result = (new PlainTextDataResponseFormatter()) + ->withContentType('text/html') + ->format($dataResponse); + $result->getBody()->rewind(); + + $this->assertSame( + 'test', + $result->getBody()->getContents(), + ); + $this->assertSame('text/html; charset=UTF-8', $result->getHeaderLine('Content-Type')); + } + + public function testWithIncorrectType(): void + { + $dataResponse = $this->createDataResponse(['test']); + $formatter = new PlainTextDataResponseFormatter(); + + $this->expectException(LogicException::class); + $this->expectExceptionMessage('Data must be either a scalar value, null, or a stringable object. array given.'); + $formatter->format($dataResponse); + } + + public function testDataWithNull(): void + { + $dataResponse = $this->createDataResponse(null); + $result = (new PlainTextDataResponseFormatter())->format($dataResponse); + $result->getBody()->rewind(); + + $this->assertSame( + '', + $result->getBody()->getContents(), + ); + $this->assertSame(['text/plain; charset=UTF-8'], $result->getHeader('Content-Type')); + } + + public function testDataWithStringableObject(): void + { + $data = new class () { + public function __toString(): string + { + return 'test'; + } + }; + + $dataResponse = $this->createDataResponse($data); + $result = (new PlainTextDataResponseFormatter())->format($dataResponse); + $result->getBody()->rewind(); + + $this->assertSame( + 'test', + $result->getBody()->getContents(), + ); + $this->assertSame(['text/plain; charset=UTF-8'], $result->getHeader('Content-Type')); + } + + public function testImmutability(): void + { + $formatter = new PlainTextDataResponseFormatter(); + $this->assertNotSame($formatter, $formatter->withContentType('text/html')); + $this->assertNotSame($formatter, $formatter->withEncoding('utf-8')); + } +} diff --git a/tests/Middleware/FormatDataResponseAsPlainTextTest.php b/tests/Middleware/FormatDataResponseAsPlainTextTest.php new file mode 100644 index 0000000..1c397fb --- /dev/null +++ b/tests/Middleware/FormatDataResponseAsPlainTextTest.php @@ -0,0 +1,28 @@ +createDataResponse('test'); + + $response = $middleware->process( + $this->createRequest(), + $this->createRequestHandler($dataResponse) + ); + + $response->getBody()->rewind(); + + $this->assertSame('test', $response->getBody()->getContents()); + $this->assertSame(['text/plain; charset=UTF-8'], $response->getHeader('Content-Type')); + } +} From 70dcf0c3874e00c72a565996435271a76f958b1c Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 2 Mar 2024 17:43:24 +0300 Subject: [PATCH 4/5] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00d5229..3fa36f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Enh #85: Raise minimum PHP version to 8.1 and refactor code (@vjik) - Enh #80: Add support for `psr/http-message` version `^2.0` (@vjik) - Bug #85: Explicitly add transitive dependencies `psr/http-factory` and `psr/http-server-handler` (@vjik) +- New #88: Add `PlainTextDataResponseFormatter` formatter and `FormatDataResponseAsPlainText` middleware (@vjik) ## 2.0.0 February 15, 2023 From 19ec0be6e0df3bc686a1a65928dc662f0bba5a82 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 2 Mar 2024 14:43:43 +0000 Subject: [PATCH 5/5] Apply fixes from StyleCI --- tests/Formatter/PlainTextDataResponseFormatterTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Formatter/PlainTextDataResponseFormatterTest.php b/tests/Formatter/PlainTextDataResponseFormatterTest.php index 11772e6..9f0d981 100644 --- a/tests/Formatter/PlainTextDataResponseFormatterTest.php +++ b/tests/Formatter/PlainTextDataResponseFormatterTest.php @@ -10,7 +10,6 @@ final class PlainTextDataResponseFormatterTest extends TestCase { - public function testCorrectFormat(): void { $dataResponse = $this->createDataResponse('test');