Completed
Push — master ( 1b3e01...ba7d86 )
by Nikola
07:03
created

AbstractStackedSaxHandler::onElementEnd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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 element stack size
57
     *
58
     * @return int
59
     */
60
    protected function getStackSize()
61
    {
62
        return count($this->stack);
63
    }
64
65
    /**
66
     * Element start handler, executed when XML tag is entered.
67
     *
68
     * @param resource $parser Parser handler.
69
     * @param string $name Tag name.
70
     * @param array $attributes Element attributes.
71
     */
72
    abstract protected function handleOnElementStart($parser, $name, $attributes);
73
74
    /**
75
     * Element end handler, executed when XML tag is leaved.
76
     *
77
     * @param resource $parser Parser handler.
78
     * @param string $name Tag name.
79
     */
80
    abstract protected function handleOnElementEnd($parser, $name);
81
}
82