1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\XMLStream; |
3
|
|
|
|
4
|
|
|
class XMLStreamObject |
5
|
|
|
{ |
6
|
|
|
use \PHPDaemon\Traits\ClassWatchdog; |
7
|
|
|
use \PHPDaemon\Traits\StaticObjectWatchdog; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Tag name |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
public $name; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Namespace |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
public $ns; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Attributes |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
public $attrs = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Subs? |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
public $subs = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Node data |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public $data = ''; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor |
46
|
|
|
* |
47
|
|
|
* @param string $name |
48
|
|
|
* @param string $ns |
49
|
|
|
* @param array $attrs |
50
|
|
|
* @param string $data |
51
|
|
|
*/ |
52
|
|
|
public function __construct($name, $ns = '', $attrs = [], $data = '') |
53
|
|
|
{ |
54
|
|
|
$this->name = strtolower($name); |
55
|
|
|
$this->ns = $ns; |
56
|
|
|
if (is_array($attrs) && count($attrs)) { |
57
|
|
|
foreach ($attrs as $key => $value) { |
58
|
|
|
$this->attrs[strtolower($key)] = $value; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
$this->data = $data; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Dump this XML Object to output. |
66
|
|
|
* |
67
|
|
|
* @param integer $depth |
68
|
|
|
*/ |
69
|
|
|
public function printObj($depth = 0) |
70
|
|
|
{ |
71
|
|
|
$s = str_repeat("\t", $depth) . $this->name . " " . $this->ns . ' ' . $this->data . "\n"; |
72
|
|
|
foreach ($this->subs as $sub) { |
73
|
|
|
$s .= $sub->printObj($depth + 1); |
74
|
|
|
} |
75
|
|
|
return $s; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return this XML Object in xml notation |
80
|
|
|
* |
81
|
|
|
* @param string $str |
82
|
|
|
*/ |
83
|
|
|
public function toString($str = '') |
84
|
|
|
{ |
85
|
|
|
$str .= "<{$this->name} xmlns='{$this->ns}' "; |
86
|
|
|
foreach ($this->attrs as $key => $value) { |
87
|
|
|
if ($key !== 'xmlns') { |
88
|
|
|
$value = htmlspecialchars($value); |
89
|
|
|
$str .= "$key='$value' "; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
$str .= ">"; |
93
|
|
|
foreach ($this->subs as $sub) { |
94
|
|
|
$str .= $sub->toString(); |
95
|
|
|
} |
96
|
|
|
$body = htmlspecialchars($this->data); |
97
|
|
|
$str .= "$body</{$this->name}>"; |
98
|
|
|
return $str; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Has this XML Object the given sub? |
103
|
|
|
* |
104
|
|
|
* @param string $name |
105
|
|
|
* @return boolean |
106
|
|
|
*/ |
107
|
|
View Code Duplication |
public function hasSub($name, $ns = null) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
foreach ($this->subs as $sub) { |
110
|
|
|
if (($name === "*" or $sub->name === $name) and ($ns === null or $sub->ns === $ns)) { |
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Return a sub |
119
|
|
|
* |
120
|
|
|
* @param string $name |
121
|
|
|
* @param string $attrs |
|
|
|
|
122
|
|
|
* @param string $ns |
|
|
|
|
123
|
|
|
*/ |
124
|
|
View Code Duplication |
public function sub($name, $attrs = null, $ns = null) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
//@TODO: attrs is ignored |
127
|
|
|
foreach ($this->subs as $sub) { |
128
|
|
|
if ($sub->name === $name and ($ns === null or $sub->ns === $ns)) { |
129
|
|
|
return $sub; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
return null; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.