1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace KingsonDe\Marshal; |
6
|
|
|
|
7
|
|
|
class XmlNode { |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @var string|int |
11
|
|
|
*/ |
12
|
|
|
private $name; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string[] |
16
|
|
|
*/ |
17
|
|
|
private $attributes = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var mixed |
21
|
|
|
*/ |
22
|
|
|
private $nodeValue; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
private $cData = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array|null |
31
|
|
|
*/ |
32
|
|
|
private $childrenNodes; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
private $isCollection; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string|int $name |
41
|
|
|
*/ |
42
|
|
|
public function __construct($name) { |
43
|
|
|
$this->name = $name; |
44
|
|
|
$this->isCollection = \is_int($name); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return int|string |
49
|
|
|
*/ |
50
|
|
|
public function getName() { |
51
|
|
|
return $this->name; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string[] |
56
|
|
|
*/ |
57
|
|
|
public function getAttributes(): array { |
58
|
|
|
return $this->attributes; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param array $attributes |
63
|
|
|
*/ |
64
|
|
|
public function setAttributes(array $attributes) { |
65
|
|
|
$this->attributes = array_map([$this, 'castValueToString'], $attributes); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function hasNodeValue(): bool { |
69
|
|
|
return (null !== $this->nodeValue); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
|
|
public function getNodeValue() { |
76
|
|
|
return $this->nodeValue; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param mixed $nodeValue |
81
|
|
|
*/ |
82
|
|
|
public function setNodeValue($nodeValue) { |
83
|
|
|
$this->nodeValue = $this->castValueToString($nodeValue); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function isCData(): bool { |
87
|
|
|
return $this->cData; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function setIsCData(bool $cDataValue) { |
91
|
|
|
$this->cData = $cDataValue; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return array|null |
96
|
|
|
*/ |
97
|
|
|
public function getChildrenNodes() { |
98
|
|
|
return $this->childrenNodes; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function setChildrenNodes(array $childrenNodes) { |
102
|
|
|
$this->childrenNodes = $childrenNodes; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function hasChildrenNodes(): bool { |
106
|
|
|
return \is_array($this->childrenNodes); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function isCollection(): bool { |
110
|
|
|
return $this->isCollection; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param mixed $value |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
private function castValueToString($value): string { |
118
|
|
|
return (\is_string($value) ? $value : var_export($value, true)); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|