diff --git a/CHANGELOG-4.x.md b/CHANGELOG-4.x.md index a3693bc..e3117be 100644 --- a/CHANGELOG-4.x.md +++ b/CHANGELOG-4.x.md @@ -52,3 +52,4 @@ must instantiate the object first, and then call `fromText` method. All paramete * [Issue #80](https://github.com/Badcow/DNS/issues/80) - Parser now supports the $INCLUDE directive to import and parse child or subdomain zone files. * [PR #82](https://github.com/Badcow/DNS/pull/82) - Fix character escaping in TXT records. (Thank you, [@fbett](https://github.com/fbett)) +* [Issue #84](https://github.com/Badcow/DNS/issues/84) - `TXT::toText()` now splits string into 255-byte chunks. (Thank you, [@fbett](https://github.com/fbett)) diff --git a/lib/AlignedRdataFormatters.php b/lib/AlignedRdataFormatters.php index 43939e7..a6ca326 100644 --- a/lib/AlignedRdataFormatters.php +++ b/lib/AlignedRdataFormatters.php @@ -93,7 +93,6 @@ public static function TXT(TXT $txt, int $padding): string $rdata = Tokens::OPEN_BRACKET.Tokens::SPACE; foreach ($lines as $line) { - $txtSplit = new TXT(); $txtSplit->setText($line); diff --git a/lib/Rdata/TXT.php b/lib/Rdata/TXT.php index 8d6b6e3..b2b8c6c 100755 --- a/lib/Rdata/TXT.php +++ b/lib/Rdata/TXT.php @@ -52,10 +52,11 @@ public function getText(): ?string */ public function toText(): string { - return sprintf( - Tokens::DOUBLE_QUOTES . '%s' . Tokens::DOUBLE_QUOTES, - addcslashes($this->text ?? '', Tokens::DOUBLE_QUOTES . '\\') - ); + $chunks = array_map(function (string $chunk) { + return sprintf('"%s"', addcslashes($chunk, Tokens::DOUBLE_QUOTES.Tokens::BACKSLASH)); + }, str_split($this->text ?? '', 255)); + + return implode(' ', $chunks); } /** diff --git a/tests/Rdata/TxtTest.php b/tests/Rdata/TxtTest.php index 5236aa5..425bd71 100644 --- a/tests/Rdata/TxtTest.php +++ b/tests/Rdata/TxtTest.php @@ -27,14 +27,30 @@ public function testSetText(): void $this->assertEquals($text, $txt->getText()); } - public function testOutput(): void + public function dp_testToText(): array + { + return [ + //[$text, $expectation] + ['"This is some quoted text". It\'s a nice piece of text.', '"\"This is some quoted text\". It\'s a nice piece of text."'], + [ + 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis vel lorem in massa elementum blandit nec sed massa. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec eu purus id arcu venenatis elementum in quis enim. Aenean at urna varius sapien dapibus.', + '"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis vel lorem in massa elementum blandit nec sed massa. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec eu purus id arcu venenatis elementum in quis enim. Aenean at urna varius sapie" "n dapibus."', + ], + ]; + } + + /** + * @dataProvider dp_testToText + * + * @param string $text the input text value + * @param string $expectation The expected output of TXT::toText() + */ + public function testToText(string $text, string $expectation): void { - $text = '"This is some quoted text". It\'s a nice piece of text.'; - $expected = '"\"This is some quoted text\". It\'s a nice piece of text."'; $txt = new TXT(); $txt->setText($text); - $this->assertEquals($expected, $txt->toText()); + $this->assertEquals($expectation, $txt->toText()); } public function testFromTxt(): void