Test Failed
Push — master ( 677c8f...f41d77 )
by Alexander
02:28
created

XmlExportService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 94
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0
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 14
     * @return \DOMElement
46
     */
47 14
    public function export()
48 14
    {
49
        $xml = $this->createElement();
50
51 14
        Collection::from($this->getObject()->getXmlChildren() ?? [])
52
            ->map($this->mapChild())
53 6
            ->forEach(function (\DOMNode $child) use ($xml) {
54 5
                $xml->appendChild($child);
55 6
            });
56 13
57
58 6
        Collection::from($this->getObject()->getXmlProperties())
59 13
            ->reduce(function (Collection $properties, string $property) {
60
                $properties[$property] = $this->getObject()->{$property};
61
                return $properties;
62 13
            }, Collection::create())
63
            ->filter($this->getIsAttribute())
64 7
            ->forEach(function ($value, string $property) use ($xml) {
65 7
                $xml->setAttribute($property, $value);
66 13
            });
67
68 7
        return $xml;
69 13
    }
70 13
71 6
    /**
72 13
     * Creating new element to put object into
73
     *
74 13
     * @return \DOMElement
75
     */
76
    protected function createElement() :\DOMElement
77
    {
78
        return $this->getDocument()->createElement(
79
            $this->getObject()->getXmlElementName()
80 14
        );
81
    }
82 14
83
    /**
84
     * Preparing all children to export
85
     *
86
     * @return \Closure
87
     */
88
    protected function mapChild() :\Closure
89 14
    {
90
        function ($child) {
91 14
            return $child instanceof XmlConvertibleInterface
92 14
                ? $child->toXml($this->document)
93
                : $child;
94
        }
95
    }
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '}'
Loading history...
96
97
    /**
98 14
     * Can we put current attribute to XML
99
     *
100 14
     * @return \Closure
101
     */
102
    protected function getIsAttribute() :\Closure{
103
        return function ($value): bool {
104
            return !is_array($value) && !is_object($value) && !is_null($value);
105
        };
106
    }
107 14
108
    /**
109 14
     * @return XmlConvertibleInterface
110 14
     */
111
    public function getObject(): XmlConvertibleInterface
112
    {
113
        return $this->object;
114
    }
115
116
    /**
117
     * @param XmlConvertibleInterface $object
118
     * @return $this
119
     */
120
    public function setObject(XmlConvertibleInterface $object)
121
    {
122
        $this->object = $object;
123
        return $this;
124
    }
125
126
    /**
127
     * @return \DOMDocument
128
     */
129
    public function getDocument(): \DOMDocument
130
    {
131
        return $this->document;
132
    }
133
134
    /**
135
     * @param \DOMDocument $document
136
     * @return $this
137
     */
138
    public function setDocument(\DOMDocument $document = null)
139
    {
140
        $this->document = $document ?? new \DOMDocument();
141
        return $this;
142
    }
143
}