XmlExportService   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 19
eloc 33
c 3
b 0
f 0
dl 0
loc 118
ccs 34
cts 34
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getObject() 0 3 1
B export() 0 30 10
A getDocument() 0 3 1
A setDocument() 0 4 1
A getIsAttribute() 0 4 3
A setObject() 0 4 1
A createElement() 0 4 1
1
<?php
2
3
namespace Horat1us\Services;
4
5
use Horat1us\XmlConvertibleInterface;
6
7
/**
8
 * Class XmlExportService
9
 * @package Horat1us\Services
10
 */
11
class XmlExportService
12
{
13
    /**
14
     * @var XmlConvertibleInterface
15
     */
16
    public $object;
17
18
    /**
19
     * @var \DOMDocument
20
     */
21
    public $document;
22
23
    /**
24
     * XmlExportService constructor.
25
     * @param XmlConvertibleInterface $object
26
     * @param \DOMDocument|null $document
27
     */
28
    public function __construct(XmlConvertibleInterface $object, ?\DOMDocument $document = null)
29
    {
30
        $this->setDocument($document)
31
            ->setObject($object);
32
    }
33
34
    /**
35
     * Converting object to \DOMElement
36 14
     *
37
     * @return \DOMElement
38 14
     */
39 14
    public function export()
40 14
    {
41
        $xml = $this->createElement();
42
43
        foreach ($this->getObject()->getXmlChildren() ?? [] as $child) {
44
            if (!is_object($child)) {
45
                throw new \TypeError("Invalid child node type: " . gettype($child));
46
            }
47 14
            if ($child instanceof XmlConvertibleInterface) {
48
                $child = $child->toXml($this->document);
49 14
            }
50
            if (!$child instanceof \DOMNode) {
51 14
                throw new \TypeError("Invalid child node class: " . get_class($child));
52 13
            }
53
            $xml->appendChild(
54 6
                $child instanceof XmlConvertibleInterface
55 13
                    ? $child->toXml($this->document)
56
                    : $child
57
            );
58 13
        }
59
60 7
        foreach ($this->getObject()->getXmlProperties() as $property) {
61 7
            $value = $this->getObject()->getXmlProperty($property);
62 13
            if (is_array($value) || is_object($value) || is_null($value)) {
63 13
                continue;
64
            }
65 6
            $xml->setAttribute($property, $value);
66 13
        }
67
68 13
        return $xml;
69
    }
70
71
    /**
72
     * Creating new element to put object into
73
     *
74
     * @return \DOMElement
75
     */
76 14
    protected function createElement(): \DOMElement
77
    {
78 14
        return $this->getDocument()->createElement(
79 14
            $this->getObject()->getXmlElementName()
80
        );
81
    }
82
83
    /**
84
     * Can we put current attribute to XML
85
     *
86
     * @return \Closure
87
     */
88 13
    protected function getIsAttribute(): \Closure
89
    {
90
        return function ($value): bool {
91 6
            return !is_array($value) && !is_object($value) && !is_null($value);
92 5
        };
93 6
    }
94 13
95
    /**
96
     * @return XmlConvertibleInterface
97
     */
98
    public function getObject(): XmlConvertibleInterface
99
    {
100
        return $this->object;
101
    }
102
103 13
    /**
104 7
     * @param XmlConvertibleInterface $object
105 13
     * @return $this
106
     */
107
    public function setObject(XmlConvertibleInterface $object)
108
    {
109
        $this->object = $object;
110
        return $this;
111 14
    }
112
113 14
    /**
114
     * @return \DOMDocument
115
     */
116
    public function getDocument(): \DOMDocument
117
    {
118
        return $this->document;
119
    }
120 14
121
    /**
122 14
     * @param \DOMDocument $document
123 14
     * @return $this
124
     */
125
    public function setDocument(?\DOMDocument $document = null)
126
    {
127
        $this->document = $document ?? new \DOMDocument();
128
        return $this;
129 14
    }
130
}
131