|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the runopencode/sax, an RunOpenCode project. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) 2017 RunOpenCode |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace RunOpenCode\Sax; |
|
11
|
|
|
|
|
12
|
|
|
use Psr\Http\Message\StreamInterface; |
|
13
|
|
|
use RunOpenCode\Sax\Contract\SaxHandlerInterface; |
|
14
|
|
|
use RunOpenCode\Sax\Contract\StreamAdapterInterface; |
|
15
|
|
|
use RunOpenCode\Sax\Exception\RuntimeException; |
|
16
|
|
|
use RunOpenCode\Sax\StreamAdapter\DomDocumentAdapter; |
|
17
|
|
|
use RunOpenCode\Sax\StreamAdapter\ResourceAdapter; |
|
18
|
|
|
use RunOpenCode\Sax\StreamAdapter\SimpleXmlAdapter; |
|
19
|
|
|
use RunOpenCode\Sax\StreamAdapter\StringAdapter; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class SaxParser |
|
23
|
|
|
* |
|
24
|
|
|
* Utility class for working with SAX handler and XML document. |
|
25
|
|
|
* |
|
26
|
|
|
* @package RunOpenCode\Sax |
|
27
|
|
|
*/ |
|
28
|
|
|
class SaxParser |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var StreamAdapterInterface[] |
|
32
|
|
|
*/ |
|
33
|
|
|
private $streamAdapters; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* SaxParser constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param StreamAdapterInterface[] $streamAdapters Stream adapters to register to parser. |
|
39
|
|
|
*/ |
|
40
|
3 |
|
public function __construct(array $streamAdapters = array()) |
|
41
|
|
|
{ |
|
42
|
3 |
|
$this->streamAdapters = array(); |
|
43
|
|
|
|
|
44
|
3 |
|
foreach ($streamAdapters as $streamAdapter) { |
|
45
|
1 |
|
$this->addStreamAdapter($streamAdapter); |
|
46
|
|
|
} |
|
47
|
3 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Register stream adapter to parser. |
|
51
|
|
|
* |
|
52
|
|
|
* @param StreamAdapterInterface $streamAdapter Stream adapter to register. |
|
53
|
|
|
* @return SaxParser $this Fluent interface. |
|
54
|
|
|
*/ |
|
55
|
3 |
|
public function addStreamAdapter(StreamAdapterInterface $streamAdapter) |
|
56
|
|
|
{ |
|
57
|
3 |
|
$this->streamAdapters[] = $streamAdapter; |
|
58
|
3 |
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Parse XML document using provided SAX handler. |
|
63
|
|
|
* |
|
64
|
|
|
* @param SaxHandlerInterface $saxHandler Handler to user for parsing document. |
|
65
|
|
|
* @param mixed $xmlDocument XML document source. |
|
66
|
|
|
* |
|
67
|
|
|
* @return mixed Parsing result. |
|
68
|
|
|
*/ |
|
69
|
3 |
|
public function parse(SaxHandlerInterface $saxHandler, $xmlDocument) |
|
70
|
|
|
{ |
|
71
|
3 |
|
$xmlDocument = ($xmlDocument instanceof StreamInterface) ? $xmlDocument : $this->getDocumentStream($xmlDocument); |
|
72
|
2 |
|
return $saxHandler->parse($xmlDocument); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Default SAX parser factory. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $streamClass FQCN to use when converting to XML document source to stream. |
|
79
|
|
|
* @return SaxParser New SAX parser instance. |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public static function factory($streamClass = 'GuzzleHttp\\Psr7\\Stream') |
|
82
|
|
|
{ |
|
83
|
1 |
|
return new static([ |
|
84
|
1 |
|
new ResourceAdapter($streamClass), |
|
85
|
1 |
|
new DomDocumentAdapter($streamClass), |
|
86
|
1 |
|
new SimpleXmlAdapter($streamClass), |
|
87
|
1 |
|
new StringAdapter($streamClass), |
|
88
|
|
|
]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Convert XML document to stream source. |
|
93
|
|
|
* |
|
94
|
|
|
* @param mixed $xmlDocument XML document source. |
|
95
|
|
|
* @return StreamInterface Converted XML document to stream. |
|
96
|
|
|
* |
|
97
|
|
|
* @throws \RuntimeException |
|
98
|
|
|
*/ |
|
99
|
3 |
|
private function getDocumentStream($xmlDocument) |
|
100
|
|
|
{ |
|
101
|
|
|
/** |
|
102
|
|
|
* @var StreamAdapterInterface $streamAdapter |
|
103
|
|
|
*/ |
|
104
|
3 |
|
foreach ($this->streamAdapters as $streamAdapter) { |
|
105
|
|
|
|
|
106
|
3 |
|
if ($streamAdapter->supports($xmlDocument)) { |
|
107
|
3 |
|
return $streamAdapter->convert($xmlDocument); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
throw new RuntimeException(sprintf('Suitable XML document stream adapter is not registered for XML document of type "%s".', is_object($xmlDocument) ? get_class($xmlDocument) : gettype($xmlDocument))); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|