commit 0cb06e215036fe677abb20ef66cb2da682bb2c72 Author: Torsten Lücke Date: Wed Apr 27 13:16:21 2022 +0200 init: Erste Version aus Test-App erstellen diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c7a151 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/.idea +/composer.lock +/vendor +/codeception.yml +/tests/*.suite.yml +/tests/_*/ diff --git a/codeception.dist.yml b/codeception.dist.yml new file mode 100644 index 0000000..f0446cc --- /dev/null +++ b/codeception.dist.yml @@ -0,0 +1,10 @@ +paths: + tests: tests + output: tests/_output + data: tests/_data + support: tests/_support + envs: tests/_envs +actor_suffix: Tester +extensions: + enabled: + - Codeception\Extension\RunFailed diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..c0385a0 --- /dev/null +++ b/composer.json @@ -0,0 +1,37 @@ +{ + "name": "torsten-hettstedt/sax-xml-reader", + "description": "SAX-XML-Readertest", + "minimum-stability": "stable", + "license": "GPL-3.0-or-later", + "authors": [ + { + "name": "Torsten L\u00fccke", + "email": "developer@torsten-hettstedt.de" + } + ], + "require": { + "php": ">=7.4", + "ext-xml": "*" + }, + "require-dev": { + "codeception/codeception": "^4.1.18", + "codeception/module-asserts": "^1.3.1", + "phpstan/phpstan": "^1.4.7", + "codeception/module-phpbrowser": "^1.0.0" + }, + "autoload": { + "psr-4": { + "TorstenHettstedt\\XmlReader\\Sax\\": "src" + }, + "exclude-from-classmap": [ + "/example/", + "/.idea/" + ] + }, + "autoload-dev": { + "psr-4": { + "TorstenHettstedt\\XmlReader\\Sax\\Tests\\Unit\\": "tests/unit", + "TorstenHettstedt\\XmlReader\\Sax\\Tests\\Helper\\": "tests/_support/Helper" + } + } +} \ No newline at end of file diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..c1923db --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,6 @@ +parameters: + level: 6 + paths: + - src + - tests/unit + - tests/_support/Helper \ No newline at end of file diff --git a/src/XmlReaderInterfaces.php b/src/XmlReaderInterfaces.php new file mode 100644 index 0000000..356e93d --- /dev/null +++ b/src/XmlReaderInterfaces.php @@ -0,0 +1,13 @@ +xmlReader = $xml_reader; + $this->parser = xml_parser_create(); + + xml_set_object($this->parser, $this); + xml_set_element_handler($this->parser, "tag_open", "tag_close"); + xml_set_character_data_handler($this->parser, "cdata"); + } + + public function __destruct() + { + xml_parser_free($this->parser); + unset($this->parser); + } + + public function parse($data):void + { + xml_parse($this->parser, $data); + } + + /** + * @param string $path + * + * @return void + * @throws InvalidArgumentException + */ + public function parseFile(string $path): void + { + // open xml file + if (!($handle = fopen($path, "r"))) { + throw new InvalidArgumentException('could not open XML file "' . $path . '".'); + } + + while ($data = fread($handle, 4096)) { // read xml file + $this->parse($data); + } + } + + /** @noinspection PhpUnusedParameterInspection */ + protected function tag_open($parser, string $tag_name, array $attributes):void + { + $this->actualTagName = $tag_name; + if ($this->actualTagName !== '') { + $this->xmlReader->tag_open($tag_name, $attributes); + } + } + + /** @noinspection PhpUnusedParameterInspection */ + protected function cdata($parser, string $cdata):void + { + if ($this->actualTagName !== '' && trim($cdata) !== '') { + $this->xmlReader->cdata($this->actualTagName, $cdata); + } + } + + /** @noinspection PhpUnusedParameterInspection */ + protected function tag_close($parser, string $tag_name):void + { + $this->actualTagName = ''; + $this->xmlReader->tag_close($tag_name); + } +} \ No newline at end of file diff --git a/tests/unit/XmlSaxParserTest.php b/tests/unit/XmlSaxParserTest.php new file mode 100644 index 0000000..07ab0d4 --- /dev/null +++ b/tests/unit/XmlSaxParserTest.php @@ -0,0 +1,48 @@ + + + + Android + contact@tutorialspoint.com + + + + Java + + +XML; + + protected $xmlReader = null; + + /** + * @throws Exception + */ + protected function setUp(): void + { + parent::setUp(); + $this->xmlReader = $this->makeEmpty(XmlReaderInterfaces::class, [ + 'tag_open' => Expected::exactly(6), + 'cdata' => Expected::exactly(3), + 'tag_close' => Expected::exactly(6), + ]); + } + + public function testParse() + { + $reader = new XmlSaxParser($this->xmlReader); + $reader->parse(self::XML_DATA); + } +}