diff --git a/.gitignore b/.gitignore index eb5ecc9..1dd6b34 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ /.temp/ /ext/ compile*.log -stubs/streamparser \ No newline at end of file +stubs/streamparser +tests/test.php diff --git a/sixdreams/StreamReader/XmlStreamReader.zep b/sixdreams/StreamReader/XmlStreamReader.zep index 33b55de..af82b86 100644 --- a/sixdreams/StreamReader/XmlStreamReader.zep +++ b/sixdreams/StreamReader/XmlStreamReader.zep @@ -18,6 +18,8 @@ class XmlStreamReader implements StreamReaderInterface protected collected; protected collectedRef; + protected lowerCase = false; + public function parse(resource data, int buffer = 1024) -> bool { if (this->extractPath === null) { @@ -83,6 +85,13 @@ class XmlStreamReader implements StreamReaderInterface return this; } + public function setLowerCaseNames(bool state) -> + { + let this->lowerCase = state; + + return this; + } + private function parseStart(parser, string name, array attributes) -> void { let this->currentPath = this->currentPath . "/" . strtolower(name); @@ -169,9 +178,9 @@ class XmlStreamReader implements StreamReaderInterface private function buildElement(array element) -> string { var ret, k, v; - let ret = "<" . element[0]; + let ret = "<" . (this->lowerCase ? strtolower(element[0]) : element[0]); for k, v in element[1] { - let ret = ret . " " . k . "=\"" . htmlentities(v, ENT_QUOTES | ENT_XML1, "UTF-8") . "\""; + let ret = ret . " " . (this->lowerCase ? strtolower(k) : k) . "=\"" . htmlentities(v, ENT_QUOTES | ENT_XML1, "UTF-8") . "\""; } return ret . ">" . element[2]; @@ -179,7 +188,7 @@ class XmlStreamReader implements StreamReaderInterface private function closeElement(string name) -> string { - return ""; + return "lowerCase ? strtolower(name) : name) . ">"; } protected function fireCallback(string text) -> void diff --git a/stubs/StreamReaderInterface.zep.php b/stubs/StreamReaderInterface.zep.php index 7323e55..a36a513 100644 --- a/stubs/StreamReaderInterface.zep.php +++ b/stubs/StreamReaderInterface.zep.php @@ -39,4 +39,12 @@ public function registerCallback(?string $collectPath, string $extractPath, call */ public function setOptionCallbacks(callable $optionsCallback): StreamReaderInterface; + /** + * Change case of all nodes and attributes to lower. + * + * @param bool $state + * + * @return StreamReaderInterface + */ + public function setLowerCaseNames(bool $state): StreamReaderInterface; } diff --git a/stubs/XmlStreamReader.zep.php b/stubs/XmlStreamReader.zep.php index 104fd66..5230c0f 100644 --- a/stubs/XmlStreamReader.zep.php +++ b/stubs/XmlStreamReader.zep.php @@ -22,4 +22,9 @@ public function registerCallback(?string $collectPath, string $extractPath, call * {@inheritdoc} */ public function setOptionCallbacks(callable $optionsCallback): StreamReaderInterface {} + + /** + * {@inheritdoc} + */ + public function setLowerCaseNames(bool $state): StreamReaderInterface {} } diff --git a/tests/XmlStreamReaderTest.php b/tests/XmlStreamReaderTest.php index 0aa244e..74c0726 100644 --- a/tests/XmlStreamReaderTest.php +++ b/tests/XmlStreamReaderTest.php @@ -4,6 +4,7 @@ namespace SixDreams; use PHPUnit\Framework\TestCase; +use SixDreams\StreamReader\XmlStreamReader; /** * Class XmlStreamReaderTest @@ -19,12 +20,21 @@ class XmlStreamReaderTest extends TestCase */ public function testBase(?string $collect, string $xtract, array $excepted): void { - $found = $this->requestData($collect, $xtract); - self::assertCount(\count($excepted), $found); - foreach ($excepted as $id => $chunk) { - self::assertArrayHasKey($id, $found); - self::assertEquals($chunk, $found[$id]); - } + $this->assertStringArray($this->requestData($collect, $xtract), $excepted); + } + + /** + * @dataProvider lowerCaseProvider + * + * @param null|string $collect + * @param string $xtract + * @param array $excepted + */ + public function testLowerCase(?string $collect, string $xtract, array $excepted): void + { + $this->assertStringArray($this->requestData($collect, $xtract, function (XmlStreamReader $reader) { + $reader->setLowerCaseNames(true); + }), $excepted); } /** @@ -37,7 +47,23 @@ public function testInvalidPath(): void } /** - * Data provider for test. + * Data provider for @see testLowerCase. + * + * @return array + */ + public function lowerCaseProvider(): array + { + return [ + ['/xml/sport', '/xml/sport/league/game', [ + '', + '', + '' + ]] + ]; + } + + /** + * Data provider for @see testBase. * * @return array */ @@ -45,18 +71,18 @@ public function dataProvider(): array { return [ ['/xml/sport', '/xml/sport/league/game', [ - '', - '', + '', + '', '' ]], [null, '/xml/sport/league/game', [ - '', - '', + '', + '', '' ]], ['/xml/sport/league/game', '/xml/sport/league/game', [ - '', - '', + '', + '', '' ]] ]; @@ -65,26 +91,47 @@ public function dataProvider(): array /** * Request data from parser. * - * @param null|string $collect - * @param string $extract + * @param null|string $collect + * @param string $extract + * @param callable|null $cb * * @return array */ - protected function requestData(?string $collect, string $extract): array + protected function requestData(?string $collect, string $extract, ?callable $cb = null): array { $handle = \fopen(__DIR__ . '/sample.xml', 'rb'); $collected = []; try { - (new StreamReader\XmlStreamReader()) + $object = (new XmlStreamReader()) ->registerCallback($collect, $extract, function (string $data) use (&$collected) { $collected[] = $data; - }) - ->parse($handle); + }); + + if ($cb) { + $cb($object); + } + + $object->parse($handle); } finally { \fclose($handle); } return $collected; } + + /** + * Make asserts on array. + * + * @param array $actual + * @param array $excepted + */ + protected function assertStringArray(array $actual, array $excepted): void + { + self::assertCount(\count($excepted), $actual); + foreach ($excepted as $id => $chunk) { + self::assertArrayHasKey($id, $actual); + self::assertEquals($chunk, $actual[$id]); + } + } } diff --git a/tests/sample.xml b/tests/sample.xml index e5df039..61c88de 100644 --- a/tests/sample.xml +++ b/tests/sample.xml @@ -2,12 +2,12 @@ 1 - raw + RaW 1 - - test + + TeSt 1