Completed
Push — master ( 04134d...b75a21 )
by Alexander
02:28
created

XmlExportService::getObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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 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
}