1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Horat1us; |
4
|
|
|
|
5
|
|
|
use Horat1us\Services\XmlDifferenceService; |
6
|
|
|
use Horat1us\Services\XmlEqualityService; |
7
|
|
|
use Horat1us\Services\XmlExportService; |
8
|
|
|
use Horat1us\Services\XmlIntersectionService; |
9
|
|
|
use Horat1us\Services\XmlParserService; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class XmlConvertible |
13
|
|
|
* @package Horat1us |
14
|
|
|
* |
15
|
|
|
* @mixin XmlConvertibleInterface |
16
|
|
|
*/ |
17
|
|
|
trait XmlConvertible |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var XmlConvertibleInterface[]|\DOMNode[]|\DOMElement[]|null |
21
|
|
|
*/ |
22
|
|
|
public ?array $xmlChildren = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Name of xml element (class name will be used by default) |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public $xmlElementName; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param XmlConvertibleInterface $xml |
33
|
|
|
* @return XmlConvertible|XmlConvertibleInterface|null |
34
|
|
|
*/ |
35
|
|
|
public function xmlIntersect( |
36
|
4 |
|
XmlConvertibleInterface $xml |
37
|
|
|
) { |
38
|
|
|
$service = new XmlIntersectionService($this, $xml); |
|
|
|
|
39
|
|
|
return $service->intersect(); |
40
|
4 |
|
} |
41
|
4 |
|
|
42
|
|
|
/** |
43
|
|
|
* @param XmlConvertibleInterface $xml |
44
|
|
|
* @return XmlConvertibleInterface|XmlConvertible|null |
45
|
|
|
*/ |
46
|
|
|
public function xmlDiff(XmlConvertibleInterface $xml) |
47
|
|
|
{ |
48
|
4 |
|
$service = new XmlDifferenceService($this, $xml); |
|
|
|
|
49
|
|
|
return $service->difference(); |
50
|
4 |
|
} |
51
|
4 |
|
|
52
|
|
|
/** |
53
|
|
|
* Converts object to XML and compares it with given |
54
|
|
|
* |
55
|
|
|
* @param XmlConvertibleInterface $xml |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function xmlEqual(XmlConvertibleInterface $xml): bool |
59
|
|
|
{ |
60
|
3 |
|
$service = new XmlEqualityService($this, $xml); |
|
|
|
|
61
|
|
|
return $service->compare(); |
62
|
3 |
|
} |
63
|
3 |
|
|
64
|
|
|
/** |
65
|
|
|
* @param \DOMDocument|\DOMElement $document |
66
|
|
|
* @param array $aliases |
67
|
|
|
* @return XmlConvertibleInterface |
68
|
|
|
*/ |
69
|
|
|
public static function fromXml($document, array $aliases = []) |
70
|
|
|
{ |
71
|
12 |
|
/** @var \DOMElement $document */ |
72
|
|
|
if (!in_array(get_called_class(), $aliases)) { |
73
|
|
|
$aliases[(new \ReflectionClass(get_called_class()))->getShortName()] = get_called_class(); |
74
|
12 |
|
} |
75
|
7 |
|
return (new XmlParserService($document, $aliases))->convert(); |
76
|
|
|
} |
77
|
12 |
|
|
78
|
|
|
/** |
79
|
|
|
* @param \DOMDocument|null $document |
80
|
|
|
* @return \DOMElement |
81
|
|
|
*/ |
82
|
|
|
public function toXml(?\DOMDocument $document = null): \DOMElement |
83
|
|
|
{ |
84
|
14 |
|
$service = new XmlExportService($this, $document); |
|
|
|
|
85
|
|
|
return $service->export(); |
86
|
14 |
|
} |
87
|
14 |
|
|
88
|
|
|
/** |
89
|
|
|
* Name of xml element (class name will be used by default) |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getXmlElementName(): string |
94
|
|
|
{ |
95
|
23 |
|
return $this->xmlElementName ?? (new \ReflectionClass(get_called_class()))->getShortName(); |
96
|
|
|
} |
97
|
23 |
|
|
98
|
|
|
/** |
99
|
|
|
* Settings name of xml element |
100
|
|
|
* |
101
|
|
|
* @param string $name |
102
|
|
|
* @return static |
103
|
|
|
*/ |
104
|
|
|
public function setXmlElementName(?string $name = null) |
105
|
|
|
{ |
106
|
1 |
|
$this->xmlElementName = $name; |
107
|
|
|
return $this; |
108
|
1 |
|
} |
109
|
1 |
|
|
110
|
|
|
/** |
111
|
|
|
* @return XmlConvertibleInterface[]|\DOMNode[]|\DOMElement[]|null |
112
|
|
|
*/ |
113
|
|
|
public function getXmlChildren() |
114
|
|
|
{ |
115
|
23 |
|
return $this->xmlChildren; |
116
|
|
|
} |
117
|
23 |
|
|
118
|
|
|
/** |
119
|
|
|
* @param XmlConvertibleInterface[]|\DOMNode[]|\DOMElement[]|null $xmlChildren |
120
|
|
|
* @return static |
121
|
|
|
*/ |
122
|
|
|
public function setXmlChildren(?array $xmlChildren = null) |
123
|
|
|
{ |
124
|
14 |
|
$this->xmlChildren = $xmlChildren ?: null; |
125
|
|
|
return $this; |
126
|
14 |
|
} |
127
|
14 |
|
|
128
|
|
|
/** |
129
|
|
|
* Getting array of property names which will be used as attributes in created XML |
130
|
|
|
* |
131
|
|
|
* @param array|null $properties |
132
|
|
|
* @return array|string[] |
133
|
|
|
*/ |
134
|
|
|
public function getXmlProperties(?array $properties = null): array |
135
|
|
|
{ |
136
|
23 |
|
$properties = $properties |
137
|
|
|
?? array_map( |
138
|
23 |
|
function (\ReflectionProperty $property) { |
139
|
|
|
return $property->getName(); |
140
|
18 |
|
}, |
141
|
23 |
|
(new \ReflectionClass(get_called_class())) |
142
|
|
|
->getProperties(\ReflectionProperty::IS_PUBLIC), |
143
|
|
|
); |
144
|
|
|
|
145
|
23 |
|
|
146
|
23 |
|
return array_filter($properties, function (string $property) { |
147
|
|
|
return !in_array($property, ['xmlChildren', 'xmlElementName']); |
148
|
|
|
}); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getXmlProperty(string $property) |
152
|
|
|
{ |
153
|
|
|
if (!in_array($property, $this->getXmlProperties())) { |
154
|
18 |
|
return; |
155
|
5 |
|
} |
156
|
18 |
|
return $this->{$property}; |
157
|
18 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Cloning all children by default |
161
|
|
|
*/ |
162
|
|
|
public function __clone() |
163
|
|
|
{ |
164
|
|
|
$this->xmlChildren = array_map(function ($xmlChild) { |
165
|
|
|
return clone $xmlChild; |
166
|
|
|
}, $this->xmlChildren ?? []) ?: null; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|