diff --git a/sixdreams/StreamReader/XmlStreamReader.zep b/sixdreams/StreamReader/XmlStreamReader.zep index f17d4bf..33b55de 100644 --- a/sixdreams/StreamReader/XmlStreamReader.zep +++ b/sixdreams/StreamReader/XmlStreamReader.zep @@ -16,7 +16,6 @@ class XmlStreamReader implements StreamReaderInterface protected collecting; protected collected; - protected collectedRef; public function parse(resource data, int buffer = 1024) -> bool @@ -61,11 +60,18 @@ class XmlStreamReader implements StreamReaderInterface return true; } - public function registerCallback(string! collectPath, string extractPath, callable callback) -> + public function registerCallback(string collectPath, string! extractPath, callable callback) -> { let this->extractPath = strtolower(extractPath); - let this->collectPath = strtolower(collectPath); - let this->callback = callback; + if (collectPath === "") { + let this->collectPath = this->extractPath; + } else { + let this->collectPath = strtolower(collectPath); + } + let this->callback = callback; + if strpos(this->extractPath, this->collectPath) !== 0 { + throw new \Exception("Path must extractPath must contain collectPath!"); + } return this; } @@ -82,7 +88,7 @@ class XmlStreamReader implements StreamReaderInterface let this->currentPath = this->currentPath . "/" . strtolower(name); this->checkPath(); - if this->collecting { + if this->collecting || this->extracting { if this->extracting && !this->isExtract() { this->addData(this->buildElement([name, attributes, ""])); return; @@ -114,8 +120,8 @@ class XmlStreamReader implements StreamReaderInterface if this->extracting && !extract { this->addData(this->closeElement(name)); } else { - unset(this->collected[this->collectedRef - 1]); let this->collectedRef = this->collectedRef - 1; + unset(this->collected[this->collectedRef]); } } diff --git a/tests/XmlStreamReaderTest.php b/tests/XmlStreamReaderTest.php index 38a4558..0aa244e 100644 --- a/tests/XmlStreamReaderTest.php +++ b/tests/XmlStreamReaderTest.php @@ -27,6 +27,15 @@ public function testBase(?string $collect, string $xtract, array $excepted): voi } } + /** + * Tests invalid path. + */ + public function testInvalidPath(): void + { + $this->expectExceptionMessage('Path must extractPath must contain collectPath!'); + $this->requestData('/a/b/c', '/h/c/d/e'); + } + /** * Data provider for test. * @@ -39,6 +48,16 @@ public function dataProvider(): array '', '', '' + ]], + [null, '/xml/sport/league/game', [ + '', + '', + '' + ]], + ['/xml/sport/league/game', '/xml/sport/league/game', [ + '', + '', + '' ]] ]; } @@ -56,13 +75,15 @@ protected function requestData(?string $collect, string $extract): array $handle = \fopen(__DIR__ . '/sample.xml', 'rb'); $collected = []; - (new StreamReader\XmlStreamReader()) - ->registerCallback($collect, $extract, function (string $data) use (&$collected) { - $collected[] = $data; - }) - ->parse($handle); - - \fclose($handle); + try { + (new StreamReader\XmlStreamReader()) + ->registerCallback($collect, $extract, function (string $data) use (&$collected) { + $collected[] = $data; + }) + ->parse($handle); + } finally { + \fclose($handle); + } return $collected; }