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