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
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class SaxParser |
22
|
|
|
* |
23
|
|
|
* Utility class for working with SAX handler and XML document. |
24
|
|
|
* |
25
|
|
|
* @package RunOpenCode\Sax |
26
|
|
|
*/ |
27
|
|
|
class SaxParser |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var StreamAdapterInterface[] |
31
|
|
|
*/ |
32
|
|
|
private $streamAdapters; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* SaxParser constructor. |
36
|
|
|
* |
37
|
|
|
* @param StreamAdapterInterface[] $streamAdapters Stream adapters to register to parser. |
38
|
|
|
*/ |
39
|
2 |
|
public function __construct(array $streamAdapters = array()) |
40
|
|
|
{ |
41
|
2 |
|
$this->streamAdapters = array(); |
42
|
|
|
|
43
|
2 |
|
foreach ($streamAdapters as $streamAdapter) { |
44
|
1 |
|
$this->addStreamAdapter($streamAdapter); |
45
|
|
|
} |
46
|
2 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Register stream adapter to parser. |
50
|
|
|
* |
51
|
|
|
* @param StreamAdapterInterface $streamAdapter Stream adapter to register. |
52
|
|
|
* @return SaxParser $this Fluent interface. |
53
|
|
|
*/ |
54
|
2 |
|
public function addStreamAdapter(StreamAdapterInterface $streamAdapter) |
55
|
|
|
{ |
56
|
2 |
|
$this->streamAdapters[] = $streamAdapter; |
57
|
2 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Parse XML document using provided SAX handler. |
62
|
|
|
* |
63
|
|
|
* @param SaxHandlerInterface $saxHandler Handler to user for parsing document. |
64
|
|
|
* @param mixed $xmlDocument XML document source. |
65
|
|
|
* |
66
|
|
|
* @return mixed Parsing result. |
67
|
|
|
*/ |
68
|
2 |
|
public function parse(SaxHandlerInterface $saxHandler, $xmlDocument) |
69
|
|
|
{ |
70
|
2 |
|
$xmlDocument = ($xmlDocument instanceof StreamInterface) ? $xmlDocument : $this->getDocumentStream($xmlDocument); |
71
|
2 |
|
return $saxHandler->parse($xmlDocument); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Default SAX parser factory. |
76
|
|
|
* |
77
|
|
|
* @param string $streamClass FQCN to use when converting to XML document source to stream. |
78
|
|
|
* @return SaxParser New SAX parser instance. |
79
|
|
|
*/ |
80
|
1 |
|
public static function factory($streamClass = 'GuzzleHttp\\Psr7\\Stream') |
81
|
|
|
{ |
82
|
1 |
|
return new static([ |
83
|
1 |
|
new ResourceAdapter($streamClass), |
84
|
1 |
|
new DomDocumentAdapter($streamClass), |
85
|
1 |
|
new SimpleXmlAdapter($streamClass), |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Convert XML document to stream source. |
91
|
|
|
* |
92
|
|
|
* @param mixed $xmlDocument XML document source. |
93
|
|
|
* @return StreamInterface Converted XML document to stream. |
94
|
|
|
* |
95
|
|
|
* @throws \RuntimeException |
96
|
|
|
*/ |
97
|
2 |
|
private function getDocumentStream($xmlDocument) |
98
|
|
|
{ |
99
|
|
|
/** |
100
|
|
|
* @var StreamAdapterInterface $streamAdapter |
101
|
|
|
*/ |
102
|
2 |
|
foreach ($this->streamAdapters as $streamAdapter) { |
103
|
|
|
|
104
|
2 |
|
if ($streamAdapter->supports($xmlDocument)) { |
105
|
2 |
|
return $streamAdapter->convert($xmlDocument); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
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))); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|