feature: Beispiele des Parsers erstellen

This commit is contained in:
2022-04-27 13:37:55 +02:00
parent 0cb06e2150
commit cda93cfed7
8 changed files with 197 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
/composer.lock
/vendor
+36
View File
@@ -0,0 +1,36 @@
<?php
use TorstenHettstedt\SaxXmlExample\TutorsReader;
use TorstenHettstedt\XmlReader\Sax\XmlSaxParser;
require_once '../vendor/autoload.php';
const SHOULD_TEXT = <<<TXT
course Name - Android
Country - India
Email - contact@tutorialspoint.com
Phone - 123456789
course Name - Java
Country - India
Email - contact@tutorialspoint.com
Phone - 123456789
course Name - HTML
Country - India
Email - contact@tutorialspoint.com
Phone - 123456789
TXT;
foreach (glob('../xml/*.xml') as $file_path) {
$tutors = new TutorsReader();
$parser = new XmlSaxParser($tutors);
$parser->parseFile($file_path);
unset($parser);
printf(PHP_EOL . '## %s', $file_path);
echo $tutors->printTutors();
assert($tutors->printTutors() === SHOULD_TEXT);
}
+27
View File
@@ -0,0 +1,27 @@
{
"name": "torsten-hettstedt/sax-xml-reader-example",
"description": "SAX-XML-Reader Beispiele",
"minimum-stability": "dev",
"license": "GPL-3.0-or-later",
"repositories": [
{
"type": "path",
"url": "../"
}
],
"authors": [
{
"name": "Torsten L\u00fccke",
"email": "developer@torsten-hettstedt.de"
}
],
"require": {
"php": ">=7.4",
"torsten-hettstedt/sax-xml-reader": "*"
},
"autoload": {
"psr-4": {
"TorstenHettstedt\\SaxXmlExample\\": "src"
}
}
}
+49
View File
@@ -0,0 +1,49 @@
<?php
namespace TorstenHettstedt\SaxXmlExample;
use TorstenHettstedt\XmlReader\Sax\XmlReaderInterfaces;
class TutorsReader implements XmlReaderInterfaces
{
/** @var array[] */
protected array $tutors = [];
function tag_open($tag_name, $attributes): void
{
if ($tag_name == 'COURSE') {
$this->tutors[] = [];
}
}
function cdata($tag_name, $cdata): void
{
if ($tag_name == 'NAME' || $tag_name == 'COUNTRY' || $tag_name == 'EMAIL' || $tag_name == 'PHONE') {
$this->tutors[count($this->tutors) - 1][$tag_name] = trim($cdata);
}
}
function tag_close($tag_name): void
{}
/** @return array[] */
public function getTutors(): array
{
return $this->tutors;
}
public function printTutors(): string
{
$text = '';
foreach ($this->getTutors() as $course) {
$text .= PHP_EOL;
$text .= "course Name - " . $course['NAME'] . PHP_EOL;
$text .= "Country - " . $course['COUNTRY'] . PHP_EOL;
$text .= "Email - " . $course['EMAIL'] . PHP_EOL;
$text .= "Phone - " . $course['PHONE'] . PHP_EOL;
}
return $text;
}
}
+23
View File
@@ -0,0 +1,23 @@
<?xml version = "1.0" encoding = "utf-8"?>
<tutors>
<course>
<name>Android</name>
<country>India</country>
<email>contact@tutorialspoint.com</email>
<phone>123456789</phone>
</course>
<course>
<name>Java</name>
<country>India</country>
<email>contact@tutorialspoint.com</email>
<phone>123456789</phone>
</course>
<course>
<name>HTML</name>
<country>India</country>
<email>contact@tutorialspoint.com</email>
<phone>123456789</phone>
</course>
</tutors>
+20
View File
@@ -0,0 +1,20 @@
<?xml version = "1.0" encoding = "utf-8"?>
<tutors>
<course>
<name>Android
<country>India
<email>contact@tutorialspoint.com
<phone>123456789
<course>
<name>Java
<country>India
<email>contact@tutorialspoint.com
<phone>123456789
<course>
<name>HTML
<country>India
<email>contact@tutorialspoint.com
<phone>123456789
</tutors>
+20
View File
@@ -0,0 +1,20 @@
<?xml version = "1.0" encoding = "utf-8"?>
<tutors>
<course>
<name>Android
<country>India
<email>contact@tutorialspoint.com
<phone>123456789
<course>
<name>Java
<country>India
<email>contact@tutorialspoint.com
<phone>123456789
<course>
<name>HTML
<country>India
<email>contact@tutorialspoint.com
<phone>123456789
<tutors>
+20
View File
@@ -0,0 +1,20 @@
<?xml version = "1.0" encoding = "utf-8"?>
<tutors>
<course>
<name>Android
<country>India
<email>contact@tutorialspoint.com
<phone>123456789
<course>
<name>Java
<country>India
<email>contact@tutorialspoint.com
<phone>123456789
<course>
<name>HTML
<country>India
<email>contact@tutorialspoint.com
<phone>123456789
<