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 | 5 | public function __construct(array $options = array()) |
|
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | 5 | final public function parse(StreamInterface $stream) |
|
63 | |||
64 | /** |
||
65 | * Document start handler, executed when parsing process started. |
||
66 | * |
||
67 | * @param resource $parser Parser handler. |
||
68 | * @param StreamInterface $stream XML stream. |
||
69 | */ |
||
70 | abstract protected function onDocumentStart($parser, $stream); |
||
71 | |||
72 | /** |
||
73 | * Element start handler, executed when XML tag is entered. |
||
74 | * |
||
75 | * @param resource $parser Parser handler. |
||
76 | * @param string $name Tag name. |
||
77 | * @param array $attributes Element attributes. |
||
78 | */ |
||
79 | abstract protected function onElementStart($parser, $name, $attributes); |
||
80 | |||
81 | /** |
||
82 | * Element CDATA handler, executed when XML tag CDATA is parsed. |
||
83 | * |
||
84 | * @param resource $parser Parser handler. |
||
85 | * @param string $data Element CDATA. |
||
86 | */ |
||
87 | abstract protected function onElementData($parser, $data); |
||
88 | |||
89 | /** |
||
90 | * Element end handler, executed when XML tag is leaved. |
||
91 | * |
||
92 | * @param resource $parser Parser handler. |
||
93 | * @param string $name Tag name. |
||
94 | */ |
||
95 | abstract protected function onElementEnd($parser, $name); |
||
96 | |||
97 | /** |
||
98 | * Document end handler, executed when parsing process ended. |
||
99 | * |
||
100 | * @param resource $parser Parser handler. |
||
101 | * @param StreamInterface $stream XML stream. |
||
102 | */ |
||
103 | abstract protected function onDocumentEnd($parser, $stream); |
||
104 | |||
105 | /** |
||
106 | * Parsing error handler. |
||
107 | * |
||
108 | * @param string $message Parsing error message. |
||
109 | * @param int $code Error code. |
||
110 | * @param int $lineno XML line number which caused error. |
||
111 | */ |
||
112 | abstract protected function onParseError($message, $code, $lineno); |
||
113 | |||
114 | /** |
||
115 | * Get parsing result. |
||
116 | * |
||
117 | * Considering that your handler processed XML document, this method will collect |
||
118 | * parsing result. This method is called last and it will provide parsing result to invoker. |
||
119 | * |
||
120 | * @return mixed Parsing result |
||
121 | */ |
||
122 | abstract protected function getResult(); |
||
123 | |||
124 | /** |
||
125 | * Parse path to XML document/string content. |
||
126 | * |
||
127 | * @param resource $parser Parser. |
||
128 | * @param StreamInterface $stream XML document stream. |
||
129 | * @return AbstractSaxHandler $this Fluent interface. |
||
130 | * |
||
131 | * @throws \RuntimeException |
||
132 | */ |
||
133 | 5 | private function process($parser, StreamInterface $stream) |
|
145 | |||
146 | /** |
||
147 | * Attach handlers. |
||
148 | * |
||
149 | * @param resource $parser XML parser. |
||
150 | * @return AbstractSaxHandler $this Fluent interface. |
||
151 | */ |
||
152 | 5 | private function attachHandlers($parser) |
|
172 | } |
||
173 |