Skip to content

Commit

Permalink
#2: zlib inflate on strings, streams can be appended by a filter to i…
Browse files Browse the repository at this point in the history
…nflate
  • Loading branch information
proggeler committed Jun 19, 2024
1 parent 8a7e0fc commit 59af3b1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 43 deletions.
40 changes: 0 additions & 40 deletions src/Source/GzipStreamParser.php

This file was deleted.

28 changes: 28 additions & 0 deletions src/Source/GzipStringParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace DMT\XmlParser\Source;

class GzipStringParser implements Parser
{
private string $source;
private int $length;

public function __construct(string $source, int $length = 1024)
{
$this->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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
Expand Down

0 comments on commit 59af3b1

Please sign in to comment.