1 | <?php |
||
22 | abstract class AbstractSaxHandler implements SaxHandlerInterface |
||
23 | { |
||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $options; |
||
28 | |||
29 | /** |
||
30 | * @var string|null |
||
31 | */ |
||
32 | private $currentElement = null; |
||
33 | |||
34 | /** |
||
35 | * @var int |
||
36 | */ |
||
37 | private $stackSize = 0; |
||
38 | |||
39 | /** |
||
40 | * @var string|null |
||
41 | */ |
||
42 | private $dataBuffer = null; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | private $namespaces = []; |
||
48 | |||
49 | /** |
||
50 | * AbstractSaxHandler constructor. |
||
51 | * |
||
52 | * @param array $options |
||
53 | */ |
||
54 | 7 | public function __construct(array $options = array()) |
|
55 | { |
||
56 | 7 | $this->options = array_merge(array( |
|
57 | 7 | 'buffer_size' => 4096, |
|
58 | 'case_folding' => true, |
||
59 | 'separator' => ':', |
||
60 | 'encoding' => 'UTF-8', |
||
61 | 'skip_tagstart' => null, |
||
62 | 'skip_white' => null, |
||
63 | 7 | ), $options); |
|
64 | 7 | } |
|
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | 6 | final public function parse(StreamInterface $stream) |
|
70 | { |
||
71 | 6 | $parser = xml_parser_create_ns($this->options['encoding'], $this->options['separator']); |
|
72 | |||
73 | 6 | if (false === $this->options['case_folding']) { |
|
74 | xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); |
||
75 | } |
||
76 | |||
77 | 6 | if (null === $this->options['skip_tagstart']) { |
|
78 | 6 | xml_parser_set_option($parser, XML_OPTION_SKIP_TAGSTART, $this->options['skip_tagstart']); |
|
79 | } |
||
80 | |||
81 | 6 | if (null === $this->options['skip_white']) { |
|
82 | 6 | xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $this->options['skip_white']); |
|
83 | } |
||
84 | |||
85 | 6 | $this->onDocumentStart($parser, $stream); |
|
86 | |||
87 | 6 | $this->attachHandlers($parser); |
|
88 | |||
89 | 6 | $this->process($parser, $stream); |
|
90 | |||
91 | 5 | $this->onDocumentEnd($parser, $stream); |
|
92 | |||
93 | 5 | xml_parser_free($parser); |
|
94 | |||
95 | 5 | $stream->close(); |
|
96 | |||
97 | 5 | return $this->getResult(); |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Document start handler, executed when parsing process started. |
||
102 | * |
||
103 | * @param resource $parser Parser handler. |
||
104 | * @param StreamInterface $stream XML stream. |
||
105 | */ |
||
106 | abstract protected function onDocumentStart($parser, $stream); |
||
107 | |||
108 | /** |
||
109 | * Element start handler, executed when XML tag is entered. |
||
110 | * |
||
111 | * @param resource $parser Parser handler. |
||
112 | * @param string $name Tag name. |
||
113 | * @param array $attributes Element attributes. |
||
114 | */ |
||
115 | abstract protected function onElementStart($parser, $name, $attributes); |
||
116 | |||
117 | /** |
||
118 | * Element CDATA handler, executed when XML tag CDATA is parsed. |
||
119 | * |
||
120 | * @param resource $parser Parser handler. |
||
121 | * @param string $data Element CDATA. |
||
122 | */ |
||
123 | abstract protected function onElementData($parser, $data); |
||
124 | |||
125 | /** |
||
126 | * Element end handler, executed when XML tag is leaved. |
||
127 | * |
||
128 | * @param resource $parser Parser handler. |
||
129 | * @param string $name Tag name. |
||
130 | */ |
||
131 | abstract protected function onElementEnd($parser, $name); |
||
132 | |||
133 | /** |
||
134 | * Document end handler, executed when parsing process ended. |
||
135 | * |
||
136 | * @param resource $parser Parser handler. |
||
137 | * @param StreamInterface $stream XML stream. |
||
138 | */ |
||
139 | abstract protected function onDocumentEnd($parser, $stream); |
||
140 | |||
141 | /** |
||
142 | * Parsing error handler. |
||
143 | * |
||
144 | * @param string $message Parsing error message. |
||
145 | * @param int $code Error code. |
||
146 | * @param int $lineno XML line number which caused error. |
||
147 | */ |
||
148 | abstract protected function onParseError($message, $code, $lineno); |
||
149 | |||
150 | /** |
||
151 | * Get parsing result. |
||
152 | * |
||
153 | * Considering that your handler processed XML document, this method will collect |
||
154 | * parsing result. This method is called last and it will provide parsing result to invoker. |
||
155 | * |
||
156 | * @return mixed Parsing result |
||
157 | */ |
||
158 | abstract protected function getResult(); |
||
159 | |||
160 | /** |
||
161 | * Start namespace declaration handler, executed when namespace declaration started. |
||
162 | * |
||
163 | * @param resource $parser Parser handler. |
||
164 | * @param string $prefix Namespace reference within an XML object. |
||
165 | * @param string $uri Uniform Resource Identifier (URI) of namespace. |
||
166 | */ |
||
167 | protected function onNamespaceDeclarationStart($parser, $prefix, $uri) |
||
171 | |||
172 | /** |
||
173 | * End namespace declaration handler, executed when namespace declaration ended. |
||
174 | * |
||
175 | * @param resource $parser Parser handler. |
||
176 | * @param string $prefix Namespace reference within an XML object. |
||
177 | */ |
||
178 | protected function onNamespaceDeclarationEnd($parser, $prefix) |
||
182 | |||
183 | /** |
||
184 | * Get declared namespaces. |
||
185 | * |
||
186 | * Retrieve declared namespaces as associative array where keys are |
||
187 | * used prefixes within XML document. Note that only processed namespace |
||
188 | * declarations will be provided. |
||
189 | * |
||
190 | * @return array |
||
191 | */ |
||
192 | final protected function getDeclaredNamespaces() |
||
196 | |||
197 | /** |
||
198 | * Parse path to XML document/string content. |
||
199 | * |
||
200 | * @param resource $parser Parser. |
||
201 | * @param StreamInterface $stream XML document stream. |
||
202 | * @return AbstractSaxHandler $this Fluent interface. |
||
203 | * |
||
204 | * @throws \RuntimeException |
||
205 | */ |
||
206 | 6 | private function process($parser, StreamInterface $stream) |
|
218 | |||
219 | /** |
||
220 | * Attach handlers. |
||
221 | * |
||
222 | * @param resource $parser XML parser. |
||
223 | * @return AbstractSaxHandler $this Fluent interface. |
||
224 | */ |
||
225 | private function attachHandlers($parser) |
||
275 | |||
276 | /** |
||
277 | * Normalize namespaced tag name. |
||
278 | * |
||
279 | * @param $name |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | 6 | private function normalize($name) |
|
287 | } |
||
288 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.