|
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\Handler; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class AbstractStackedSaxHandler |
|
14
|
|
|
* |
|
15
|
|
|
* Sax handler prototype with implemented elements stack. |
|
16
|
|
|
* |
|
17
|
|
|
* @package RunOpenCode\Sax\Handler |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class AbstractStackedSaxHandler extends AbstractSaxHandler |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array Elements stack |
|
23
|
|
|
*/ |
|
24
|
|
|
private $stack = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
1 |
|
protected function onElementStart($parser, $name, $attributes) |
|
30
|
|
|
{ |
|
31
|
1 |
|
array_push($this->stack, $name); |
|
32
|
1 |
|
$this->handleOnElementStart($parser, $name, $attributes); |
|
33
|
1 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
1 |
|
protected function onElementEnd($parser, $name) |
|
39
|
|
|
{ |
|
40
|
1 |
|
array_pop($this->stack); |
|
41
|
1 |
|
$this->handleOnElementEnd($parser, $name); |
|
42
|
1 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get current processing element name (uppercase), or null, if there is no element on stack |
|
46
|
|
|
* (processing didn't started or it is ended) |
|
47
|
|
|
* |
|
48
|
|
|
* @return string|null |
|
49
|
|
|
*/ |
|
50
|
1 |
|
protected function getCurrentElementName() |
|
51
|
|
|
{ |
|
52
|
1 |
|
return (($count = count($this->stack)) > 0) ? $this->stack[$count-1] : null; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get current stack trace. |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
1 |
|
protected function getStack() |
|
61
|
|
|
{ |
|
62
|
1 |
|
return $this->stack; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get current element stack size |
|
67
|
|
|
* |
|
68
|
|
|
* @return int |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function getStackSize() |
|
71
|
|
|
{ |
|
72
|
|
|
return count($this->stack); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Element start handler, executed when XML tag is entered. |
|
77
|
|
|
* |
|
78
|
|
|
* @param resource $parser Parser handler. |
|
79
|
|
|
* @param string $name Tag name. |
|
80
|
|
|
* @param array $attributes Element attributes. |
|
81
|
|
|
*/ |
|
82
|
|
|
abstract protected function handleOnElementStart($parser, $name, $attributes); |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Element end handler, executed when XML tag is leaved. |
|
86
|
|
|
* |
|
87
|
|
|
* @param resource $parser Parser handler. |
|
88
|
|
|
* @param string $name Tag name. |
|
89
|
|
|
*/ |
|
90
|
|
|
abstract protected function handleOnElementEnd($parser, $name); |
|
91
|
|
|
} |
|
92
|
|
|
|