1 | <?php |
||
23 | abstract class AbstractSaxHandler implements SaxHandlerInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $options; |
||
29 | |||
30 | /** |
||
31 | * AbstractSaxHandler constructor. |
||
32 | * |
||
33 | * @param array $options |
||
34 | */ |
||
35 | 7 | public function __construct(array $options = array()) |
|
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | 6 | final public function parse(StreamInterface $stream) |
|
52 | { |
||
53 | 6 | $parser = (true === $this->options['namespaces']) |
|
54 | ? |
||
55 | xml_parser_create_ns($this->options['encoding'], $this->options['separator']) |
||
56 | : |
||
57 | 6 | xml_parser_create($this->options['encoding']); |
|
58 | |||
59 | 6 | if (false === $this->options['case_folding']) { |
|
60 | xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); |
||
61 | } |
||
62 | |||
63 | 6 | if (null === $this->options['skip_tagstart']) { |
|
64 | 6 | xml_parser_set_option($parser, XML_OPTION_SKIP_TAGSTART, $this->options['skip_tagstart']); |
|
65 | } |
||
66 | |||
67 | 6 | if (null === $this->options['skip_white']) { |
|
68 | 6 | xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $this->options['skip_white']); |
|
69 | } |
||
70 | |||
71 | 6 | $this->onDocumentStart($parser, $stream); |
|
72 | |||
73 | 6 | $this->attachHandlers($parser); |
|
74 | |||
75 | 6 | $this->process($parser, $stream); |
|
76 | |||
77 | 5 | $this->onDocumentEnd($parser, $stream); |
|
78 | |||
79 | 5 | xml_parser_free($parser); |
|
80 | |||
81 | 5 | $stream->close(); |
|
82 | |||
83 | 5 | return $this->getResult(); |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * Document start handler, executed when parsing process started. |
||
88 | * |
||
89 | * @param resource $parser Parser handler. |
||
90 | * @param StreamInterface $stream XML stream. |
||
91 | */ |
||
92 | abstract protected function onDocumentStart($parser, $stream); |
||
93 | |||
94 | /** |
||
95 | * Element start handler, executed when XML tag is entered. |
||
96 | * |
||
97 | * @param resource $parser Parser handler. |
||
98 | * @param string $name Tag name. |
||
99 | * @param array $attributes Element attributes. |
||
100 | */ |
||
101 | abstract protected function onElementStart($parser, $name, $attributes); |
||
102 | |||
103 | /** |
||
104 | * Element CDATA handler, executed when XML tag CDATA is parsed. |
||
105 | * |
||
106 | * @param resource $parser Parser handler. |
||
107 | * @param string $data Element CDATA. |
||
108 | */ |
||
109 | abstract protected function onElementData($parser, $data); |
||
110 | |||
111 | /** |
||
112 | * Element end handler, executed when XML tag is leaved. |
||
113 | * |
||
114 | * @param resource $parser Parser handler. |
||
115 | * @param string $name Tag name. |
||
116 | */ |
||
117 | abstract protected function onElementEnd($parser, $name); |
||
118 | |||
119 | /** |
||
120 | * Document end handler, executed when parsing process ended. |
||
121 | * |
||
122 | * @param resource $parser Parser handler. |
||
123 | * @param StreamInterface $stream XML stream. |
||
124 | */ |
||
125 | abstract protected function onDocumentEnd($parser, $stream); |
||
126 | |||
127 | /** |
||
128 | * Start namespace declaration handler, executed when namespace declaration started. |
||
129 | * |
||
130 | * @param resource $parser Parser handler. |
||
131 | * @param string $prefix Namespace reference within an XML object. |
||
132 | * @param string $uri Uniform Resource Identifier (URI) of namespace. |
||
133 | */ |
||
134 | protected function onNamespaceDeclarationStart($parser, $prefix, $uri) |
||
138 | |||
139 | /** |
||
140 | * End namespace declaration handler, executed when namespace declaration ended. |
||
141 | * |
||
142 | * @param resource $parser Parser handler. |
||
143 | * @param string $prefix Namespace reference within an XML object. |
||
144 | */ |
||
145 | protected function onNamespaceDeclarationEnd($parser , string $prefix) |
||
149 | |||
150 | /** |
||
151 | * Parsing error handler. |
||
152 | * |
||
153 | * @param string $message Parsing error message. |
||
154 | * @param int $code Error code. |
||
155 | * @param int $lineno XML line number which caused error. |
||
156 | */ |
||
157 | abstract protected function onParseError($message, $code, $lineno); |
||
158 | |||
159 | /** |
||
160 | * Get parsing result. |
||
161 | * |
||
162 | * Considering that your handler processed XML document, this method will collect |
||
163 | * parsing result. This method is called last and it will provide parsing result to invoker. |
||
164 | * |
||
165 | * @return mixed Parsing result |
||
166 | */ |
||
167 | abstract protected function getResult(); |
||
168 | |||
169 | /** |
||
170 | * Parse path to XML document/string content. |
||
171 | * |
||
172 | * @param resource $parser Parser. |
||
173 | * @param StreamInterface $stream XML document stream. |
||
174 | * @return AbstractSaxHandler $this Fluent interface. |
||
175 | * |
||
176 | * @throws \RuntimeException |
||
177 | */ |
||
178 | 6 | private function process($parser, StreamInterface $stream) |
|
190 | |||
191 | /** |
||
192 | * Attach handlers. |
||
193 | * |
||
194 | * @param resource $parser XML parser. |
||
195 | * @return AbstractSaxHandler $this Fluent interface. |
||
196 | */ |
||
197 | private function attachHandlers($parser) |
||
232 | } |
||
233 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.