Completed
Push — development ( 1b87d2...43bb99 )
by Thomas
06:02
created

xml2Array::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 1
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
/***************************************************************************
3
 * for license information see LICENSE.md
4
 ***************************************************************************/
5
6
class xml2Array
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    public $stack = [];
9
    public $stack_ref;
10
    public $arrOutput = [];
11
    public $resParser;
12
    public $strXmlData;
13
14
    public function push_pos(&$pos)
15
    {
16
        $this->stack[count($this->stack)] =& $pos;
17
        $this->stack_ref =& $pos;
18
    }
19
20
    public function pop_pos()
21
    {
22
        unset($this->stack[count($this->stack) - 1]);
23
        $this->stack_ref =& $this->stack[count($this->stack) - 1];
24
    }
25
26
    /**
27
     * @param false|string $strInputXML
28
     */
29
    public function parse($strInputXML)
30
    {
31
        $this->resParser = xml_parser_create('UTF-8');
32
        xml_set_object($this->resParser, $this);
33
        xml_set_element_handler($this->resParser, 'tagOpen', 'tagClosed');
34
35
        xml_set_character_data_handler($this->resParser, 'tagData');
36
37
        $this->push_pos($this->arrOutput);
38
39
        $this->strXmlData = xml_parse($this->resParser, $strInputXML);
40
        if (!$this->strXmlData) {
41
            die(sprintf(
0 ignored issues
show
Coding Style Compatibility introduced by
The method parse() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
42
                'XML error: %s at line %d',
43
                xml_error_string(xml_get_error_code($this->resParser)),
44
                xml_get_current_line_number($this->resParser)
45
            ));
46
        }
47
48
        xml_parser_free($this->resParser);
49
50
        return $this->arrOutput;
51
    }
52
53
    public function tagOpen($parser, $name, $attrs)
0 ignored issues
show
Unused Code introduced by
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
    {
55
        if (isset($this->stack_ref[$name])) {
56
            if (!isset($this->stack_ref[$name][0])) {
57
                $tmp = $this->stack_ref[$name];
58
                unset($this->stack_ref[$name]);
59
                $this->stack_ref[$name][0] = $tmp;
60
            }
61
            $cnt = count($this->stack_ref[$name]);
62
            $this->stack_ref[$name][$cnt] = [];
63
            if (isset($attrs)) {
64
                $this->stack_ref[$name][$cnt] = $attrs;
65
            }
66
            $this->push_pos($this->stack_ref[$name][$cnt]);
67
        } else {
68
            $this->stack_ref[$name] = [];
69
            if (isset($attrs)) {
70
                $this->stack_ref[$name] = $attrs;
71
            }
72
            $this->push_pos($this->stack_ref[$name]);
73
        }
74
    }
75
76
    public function tagData($parser, $tagData)
0 ignored issues
show
Unused Code introduced by
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
    {
78
        if (mb_trim($tagData)) {
79
            if (isset($this->stack_ref['DATA'])) {
80
                $this->stack_ref['DATA'] .= $tagData;
81
            } else {
82
                $this->stack_ref['DATA'] = $tagData;
83
            }
84
        }
85
    }
86
87
    public function tagClosed($parser, $name)
0 ignored issues
show
Unused Code introduced by
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
    {
89
        $this->pop_pos();
90
    }
91
}
92