Skip to content

Commit

Permalink
Added option to make all nodes and attributes lower case
Browse files Browse the repository at this point in the history
  • Loading branch information
dotfry committed Sep 20, 2018
1 parent 2b5933a commit a840faa
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/.temp/
/ext/
compile*.log
stubs/streamparser
stubs/streamparser
tests/test.php
15 changes: 12 additions & 3 deletions sixdreams/StreamReader/XmlStreamReader.zep
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -83,6 +85,13 @@ class XmlStreamReader implements StreamReaderInterface
return this;
}

public function setLowerCaseNames(bool state) -> <StreamReaderInterface>
{
let this->lowerCase = state;

return this;
}

private function parseStart(parser, string name, array attributes) -> void
{
let this->currentPath = this->currentPath . "/" . strtolower(name);
Expand Down Expand Up @@ -169,17 +178,17 @@ 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];
}

private function closeElement(string name) -> string
{
return "</" . name . ">";
return "</" . (this->lowerCase ? strtolower(name) : name) . ">";
}

protected function fireCallback(string text) -> void
Expand Down
8 changes: 8 additions & 0 deletions stubs/StreamReaderInterface.zep.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions stubs/XmlStreamReader.zep.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 66 additions & 19 deletions tests/XmlStreamReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace SixDreams;

use PHPUnit\Framework\TestCase;
use SixDreams\StreamReader\XmlStreamReader;

/**
* Class XmlStreamReaderTest
Expand All @@ -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);
}

/**
Expand All @@ -37,26 +47,42 @@ 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', [
'<sport name="football" id="2"><league name="FBL02"><game team1="g1t1" team2="g1t2"><![CDATA[RaW]]></game></league></sport>',
'<sport name="football" id="2"><league name="supsuckers"><game team2="test_team_name"><var><![CDATA[TeSt]]></var></game></league></sport>',
'<sport name="snooker" id="1"><league name="SNK01"><game team1="t1" team2="t34"></game></league></sport>'
]]
];
}

/**
* Data provider for @see testBase.
*
* @return array
*/
public function dataProvider(): array
{
return [
['/xml/sport', '/xml/sport/league/game', [
'<sport name="football" id="2"><league name="FBL02"><game team1="g1t1" team2="g1t2"><![CDATA[raw]]></game></league></sport>',
'<sport name="football" id="2"><LEAGUE name="supsuckers"><game team2="test_team_name"><var><![CDATA[test]]></var></game></LEAGUE></sport>',
'<sport name="football" id="2"><league name="FBL02"><game team1="g1t1" team2="g1t2"><![CDATA[RaW]]></game></league></sport>',
'<sport name="football" id="2"><LEAGUE name="supsuckers"><game Team2="test_team_name"><var><![CDATA[TeSt]]></var></game></LEAGUE></sport>',
'<sport name="snooker" id="1"><league name="SNK01"><game team1="t1" team2="t34"></game></league></sport>'
]],
[null, '/xml/sport/league/game', [
'<game team1="g1t1" team2="g1t2"><![CDATA[raw]]></game>',
'<game team2="test_team_name"><var><![CDATA[test]]></var></game>',
'<game team1="g1t1" team2="g1t2"><![CDATA[RaW]]></game>',
'<game Team2="test_team_name"><var><![CDATA[TeSt]]></var></game>',
'<game team1="t1" team2="t34"></game>'
]],
['/xml/sport/league/game', '/xml/sport/league/game', [
'<game team1="g1t1" team2="g1t2"><![CDATA[raw]]></game>',
'<game team2="test_team_name"><var><![CDATA[test]]></var></game>',
'<game team1="g1t1" team2="g1t2"><![CDATA[RaW]]></game>',
'<game Team2="test_team_name"><var><![CDATA[TeSt]]></var></game>',
'<game team1="t1" team2="t34"></game>'
]]
];
Expand All @@ -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]);
}
}
}
6 changes: 3 additions & 3 deletions tests/sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<error>1</error>
<sport name="football" id="2">
<league name="FBL02">
<game team1="g1t1" team2="g1t2">raw</game>
<game team1="g1t1" team2="g1t2">RaW</game>
<error>1</error>
</league>
<LEAGUE name="supsuckers">
<game team2="test_team_name">
<var>test</var>
<game Team2="test_team_name">
<var>TeSt</var>
</game>
</LEAGUE>
<error>1</error>
Expand Down

0 comments on commit a840faa

Please sign in to comment.