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