From 59af3b1930d9fb2b692a451b7004b4015a5f4ebd Mon Sep 17 00:00:00 2001 From: proggeler Date: Wed, 19 Jun 2024 18:51:46 +0200 Subject: [PATCH] #2: zlib inflate on strings, streams can be appended by a filter to inflate --- src/Source/GzipStreamParser.php | 40 ------------------- src/Source/GzipStringParser.php | 28 +++++++++++++ ...arserTest.php => GzipStringParserTest.php} | 6 +-- 3 files changed, 31 insertions(+), 43 deletions(-) delete mode 100644 src/Source/GzipStreamParser.php create mode 100644 src/Source/GzipStringParser.php rename tests/Source/{GzipStreamParserTest.php => GzipStringParserTest.php} (78%) diff --git a/src/Source/GzipStreamParser.php b/src/Source/GzipStreamParser.php deleted file mode 100644 index 9ed7668..0000000 --- a/src/Source/GzipStreamParser.php +++ /dev/null @@ -1,40 +0,0 @@ -stream = $stream; - $this->length = $length; - } - - /** - * @inheritDoc - */ - public function parse(): iterable - { - $tmp = inflate_init(ZLIB_ENCODING_GZIP); - - while ($chunk = @fread($this->stream, $this->length)) { - yield from str_split(inflate_add($tmp, $chunk)); - } - - if ($chunk === false && !feof($this->stream)) { - throw new \RuntimeException('Could not read from source'); - } - } -} \ No newline at end of file diff --git a/src/Source/GzipStringParser.php b/src/Source/GzipStringParser.php new file mode 100644 index 0000000..cca14a4 --- /dev/null +++ b/src/Source/GzipStringParser.php @@ -0,0 +1,28 @@ +source = $source; + $this->length = $length; + } + + /** + * @inheritDoc + */ + public function parse(): iterable + { + $tmp = inflate_init(ZLIB_ENCODING_GZIP); + $chunks = str_split($this->source, $this->length); + + foreach ($chunks as $chunk) { + yield from str_split(inflate_add($tmp, $chunk)); + } + } +} \ No newline at end of file diff --git a/tests/Source/GzipStreamParserTest.php b/tests/Source/GzipStringParserTest.php similarity index 78% rename from tests/Source/GzipStreamParserTest.php rename to tests/Source/GzipStringParserTest.php index ea3a2f7..70b14be 100644 --- a/tests/Source/GzipStreamParserTest.php +++ b/tests/Source/GzipStringParserTest.php @@ -2,10 +2,10 @@ namespace DMT\Test\XmlParser\Source; -use DMT\XmlParser\Source\GzipStreamParser; +use DMT\XmlParser\Source\GzipStringParser; use PHPUnit\Framework\TestCase; -class GzipStreamParserTest extends TestCase +class GzipStringParserTest extends TestCase { public function testParse(): void { @@ -15,7 +15,7 @@ public function testParse(): void fwrite($handle, zlib_encode($books, ZLIB_ENCODING_GZIP)); rewind($handle); - $parser = new GzipStreamParser($handle); + $parser = new GzipStringParser(stream_get_contents($handle)); $contents = ''; foreach ($parser->parse() as $char) { $contents .= $char;