-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathParser.php
141 lines (109 loc) · 3.81 KB
/
Parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace DemandwareXml;
use DemandwareXml\Parser\NodeParserInterface;
use Generator;
use InvalidArgumentException;
use XMLReader;
class Parser
{
protected array $parsed = [];
public function __construct(protected string $file)
{
}
public function validate(bool $useSchema = true): bool
{
if (! file_exists($this->file)) {
throw new XmlException('XML file does not exist: ' . basename($this->file));
}
if (! is_readable($this->file)) {
throw new XmlException('XML file is not readable: ' . basename($this->file));
}
$previousErrors = libxml_use_internal_errors(true);
libxml_clear_errors();
set_error_handler(function (int $severity, string $message, string $file, int $line): void {
throw new XmlException($message . ' in ' . basename($file) . ' on line ' . $line, $severity);
});
try {
$reader = new XMLReader;
$reader->open($this->file);
if ($useSchema) {
$reader->setSchema(realpath(__DIR__ . '/../xsd/catalog.xsd'));
}
while ($reader->read());
$reader->close();
$errors = libxml_get_errors();
if (count($errors) > 0) {
throw $this->libXmlErrorToException(reset($errors));
}
} finally {
libxml_use_internal_errors($previousErrors);
libxml_clear_errors();
restore_error_handler();
}
return true;
}
protected function libXmlErrorToException($error): XMLException
{
return new XmlException(sprintf(
'%s: %s in %s on line %s column %s',
$this->libXmlErrorLevelToString($error->level),
trim($error->message),
basename($error->file),
$error->line,
$error->column,
), $error->code);
}
protected function libXmlErrorLevelToString(int $level): string
{
return match ($level) {
LIBXML_ERR_WARNING => 'Warning',
LIBXML_ERR_ERROR => 'Error',
LIBXML_ERR_FATAL => 'Fatal',
};
}
protected function validateNodeParserClass(string $class): void
{
if (! is_subclass_of($class, NodeParserInterface::class)) {
throw new InvalidArgumentException('Node parser class "' . $class . '" must implement ' . NodeParserInterface::class);
}
}
public function parse(string $class): Generator
{
$this->validateNodeParserClass($class);
$reader = new XMLReader;
$reader->open($this->file);
while ($reader->read()) {
$nodeParser = new $class($reader);
if ($nodeParser->isMatch()) {
['id' => $id, 'data' => $data] = $nodeParser->parse();
yield $id => $data;
}
}
$reader->close();
}
public function parseToArray(array $classes, array $groupedByKey = []): array
{
$this->parsed = [];
foreach ($classes as $index => $class) {
$this->validateNodeParserClass($class);
$this->parsed[$index] = [];
}
$reader = new XMLReader;
$reader->open($this->file);
while ($reader->read()) {
foreach ($classes as $index => $class) {
$nodeParser = new $class($reader);
if ($nodeParser->isMatch()) {
['id' => $id, 'data' => $data] = $nodeParser->parse();
if (in_array($index, $groupedByKey, true)) {
$this->parsed[$index][$id][] = $data;
} else {
$this->parsed[$index][$id] = $data;
}
}
}
}
$reader->close();
return $this->parsed;
}
}