1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: horat1us |
5
|
|
|
* Date: 5/11/17 |
6
|
|
|
* Time: 6:23 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Horat1us\Services; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Horat1us\Arrays\Collection; |
13
|
|
|
use Horat1us\XmlConvertibleInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class XmlExportService |
17
|
|
|
* @package Horat1us\Services |
18
|
|
|
*/ |
19
|
|
|
class XmlExportService |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var XmlConvertibleInterface |
23
|
|
|
*/ |
24
|
|
|
public $object; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \DOMDocument |
28
|
|
|
*/ |
29
|
|
|
public $document; |
30
|
|
|
|
31
|
14 |
|
public function __construct(XmlConvertibleInterface $object, \DOMDocument $document = null) |
32
|
|
|
{ |
33
|
14 |
|
$this->setDocument($document) |
34
|
14 |
|
->setObject($object); |
35
|
14 |
|
} |
36
|
|
|
|
37
|
14 |
|
public function export() |
38
|
|
|
{ |
39
|
14 |
|
$xml = $this->getDocument()->createElement( |
40
|
14 |
|
$this->getObject()->getXmlElementName() |
41
|
|
|
); |
42
|
|
|
|
43
|
14 |
|
Collection::from($this->getObject()->getXmlChildren() ?? []) |
44
|
|
|
->map(function ($child) { |
45
|
6 |
|
return $child instanceof XmlConvertibleInterface |
46
|
5 |
|
? $child->toXml($this->document) |
47
|
6 |
|
: $child; |
48
|
13 |
|
}) |
49
|
|
|
->forEach(function (\DOMNode $child) use ($xml) { |
50
|
6 |
|
$xml->appendChild($child); |
51
|
13 |
|
}); |
52
|
|
|
|
53
|
|
|
|
54
|
13 |
|
Collection::from($this->getObject()->getXmlProperties()) |
55
|
|
|
->reduce(function (Collection $properties, string $property) { |
56
|
7 |
|
$properties[$property] = $this->getObject()->{$property}; |
57
|
7 |
|
return $properties; |
58
|
13 |
|
}, Collection::create()) |
59
|
|
|
->filter(function ($value): bool { |
60
|
7 |
|
return !is_array($value) && !is_object($value) && !is_null($value); |
61
|
13 |
|
}) |
62
|
13 |
|
->forEach(function ($value, string $property) use ($xml) { |
63
|
6 |
|
$xml->setAttribute($property, $value); |
64
|
13 |
|
}); |
65
|
|
|
|
66
|
13 |
|
return $xml; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return XmlConvertibleInterface |
71
|
|
|
*/ |
72
|
14 |
|
public function getObject(): XmlConvertibleInterface |
73
|
|
|
{ |
74
|
14 |
|
return $this->object; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param XmlConvertibleInterface $object |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
14 |
|
public function setObject(XmlConvertibleInterface $object) |
82
|
|
|
{ |
83
|
14 |
|
$this->object = $object; |
84
|
14 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return \DOMDocument |
89
|
|
|
*/ |
90
|
14 |
|
public function getDocument(): \DOMDocument |
91
|
|
|
{ |
92
|
14 |
|
return $this->document; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param \DOMDocument $document |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
14 |
|
public function setDocument(\DOMDocument $document = null) |
100
|
|
|
{ |
101
|
14 |
|
$this->document = $document ?? new \DOMDocument(); |
102
|
14 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
} |