Completed
Push — master ( f41d77...47f5da )
by Alexander
02:28
created

XmlExportService   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 125
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A export() 0 23 1
A createElement() 0 6 1
A mapChild() 0 8 2
A getIsAttribute() 0 5 3
A getObject() 0 4 1
A setObject() 0 5 1
A getDocument() 0 4 1
A setDocument() 0 5 1
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
    /**
32
     * XmlExportService constructor.
33
     * @param XmlConvertibleInterface $object
34
     * @param \DOMDocument|null $document
35
     */
36 14
    public function __construct(XmlConvertibleInterface $object, \DOMDocument $document = null)
37
    {
38 14
        $this->setDocument($document)
39 14
            ->setObject($object);
40 14
    }
41
42
    /**
43
     * Converting object to \DOMElement
44
     *
45
     * @return \DOMElement
46
     */
47 14
    public function export()
48
    {
49 14
        $xml = $this->createElement();
50
51 14
        Collection::from($this->getObject()->getXmlChildren() ?? [])
52 13
            ->map($this->mapChild())
53
            ->forEach(function (\DOMNode $child) use ($xml) {
54 6
                $xml->appendChild($child);
55 13
            });
56
57
58 13
        Collection::from($this->getObject()->getXmlProperties())
59
            ->reduce(function (Collection $properties, string $property) {
60 7
                $properties[$property] = $this->getObject()->{$property};
61 7
                return $properties;
62 13
            }, Collection::create())
63 13
            ->filter($this->getIsAttribute())
64
            ->forEach(function ($value, string $property) use ($xml) {
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
     * Preparing all children to export
85
     *
86
     * @return \Closure
87
     */
88 13
    protected function mapChild() :\Closure
89
    {
90
        return function ($child) {
91 6
            return $child instanceof XmlConvertibleInterface
92 5
                ? $child->toXml($this->document)
93 6
                : $child;
94 13
        };
95
    }
96
97
    /**
98
     * Can we put current attribute to XML
99
     *
100
     * @return \Closure
101
     */
102
    protected function getIsAttribute() :\Closure{
103 13
        return function ($value): bool {
104 7
            return !is_array($value) && !is_object($value) && !is_null($value);
105 13
        };
106
    }
107
108
    /**
109
     * @return XmlConvertibleInterface
110
     */
111 14
    public function getObject(): XmlConvertibleInterface
112
    {
113 14
        return $this->object;
114
    }
115
116
    /**
117
     * @param XmlConvertibleInterface $object
118
     * @return $this
119
     */
120 14
    public function setObject(XmlConvertibleInterface $object)
121
    {
122 14
        $this->object = $object;
123 14
        return $this;
124
    }
125
126
    /**
127
     * @return \DOMDocument
128
     */
129 14
    public function getDocument(): \DOMDocument
130
    {
131 14
        return $this->document;
132
    }
133
134
    /**
135
     * @param \DOMDocument $document
136
     * @return $this
137
     */
138 14
    public function setDocument(\DOMDocument $document = null)
139
    {
140 14
        $this->document = $document ?? new \DOMDocument();
141 14
        return $this;
142
    }
143
}