Passed
Branch master (8fa86f)
by Alexander
04:20
created

XmlConvertible::getXmlElementName()   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
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Horat1us;
4
5
use Horat1us\Arrays\Collection;
6
7
/**
8
 * Class XmlConvertible
9
 * @package Horat1us
10
 */
11
trait XmlConvertible
12
{
13
    /**
14
     * @var XmlConvertibleInterface[]|\DOMNode[]|\DOMElement[]|null
15
     */
16
    public $xmlChildren;
17
18
    /**
19
     * Name of xml element (class name will be used by default)
20
     *
21
     * @var string
22
     */
23
    public $xmlElementName;
24
25
    /**
26
     * @param \DOMDocument|null $document
27
     * @return \DOMElement
28
     */
29 10
    public function toXml(\DOMDocument $document = null): \DOMElement
30
    {
31 10
        if (!$document) {
32 6
            $document = new \DOMDocument();
33
        }
34
35 10
        $xml = $document->createElement(
36 10
            $this->getXmlElementName()
37
        );
38 10
        if (!is_null($this->xmlChildren)) {
39 5
            foreach ((array)$this->xmlChildren as $child) {
40 5
                if ($child instanceof XmlConvertibleInterface) {
41 3
                    $xml->appendChild($child->toXml($document));
42 3
                } elseif ($child instanceof \DOMNode || $child instanceof \DOMElement) {
43 2
                    $xml->appendChild($child);
44
                } else {
45 1
                    throw new \UnexpectedValueException(
46 5
                        "Each child element must be an instance of " . XmlConvertibleInterface::class
47
                    );
48
                }
49
            }
50
        }
51
52 9
        $properties = $this->getXmlProperties();
53 9
        foreach ($properties as $property) {
54 4
            $value = $this->{$property};
55 4
            if (is_array($value) || is_object($value) || is_null($value)) {
56 2
                continue;
57
            }
58 3
            $xml->setAttribute($property, $value);
59
        }
60
61 9
        return $xml;
62
    }
63
64
    /**
65
     * Name of xml element (class name will be used by default)
66
     *
67
     * @return string
68
     */
69 10
    public function getXmlElementName(): string
70
    {
71 10
        return $this->xmlElementName ?? (new \ReflectionClass(get_called_class()))->getShortName();
72
    }
73
74
    /**
75
     * @param \DOMDocument|\DOMElement $document
76
     * @param array $aliases
77
     * @return static
78
     */
79 10
    public static function fromXml($document, array $aliases = [])
80
    {
81 10
        if($document instanceof \DOMDocument) {
82 10
            return static::fromXml($document->firstChild, $aliases);
0 ignored issues
show
Documentation introduced by
$document->firstChild is of type object<DOMNode>, but the function expects a object<DOMDocument>|object<DOMElement>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
        }
84
85
        /** @var \DOMElement $document */
86 10
        if (!in_array(get_called_class(), $aliases)) {
87 7
            $aliases[(new \ReflectionClass(get_called_class()))->getShortName()] = get_called_class();
88
        }
89 10
        foreach ($aliases as $key => $alias) {
90 10
            if (is_object($alias)) {
91 3
                if (!$alias instanceof XmlConvertibleInterface) {
92 1
                    throw new \UnexpectedValueException(
93 1
                        "All aliases must be instance or class implements " . XmlConvertibleInterface::class,
94 1
                        1
95
                    );
96
                }
97 2
                $aliases[is_int($key) ? $alias->getXmlElementName() : $key] = $alias;
98 2
                continue;
99
            }
100 9
            if (!is_string($alias)) {
101 1
                throw new \UnexpectedValueException(
102 1
                    "All aliases must be instance or class implements " . XmlConvertibleInterface::class,
103 1
                    2
104
                );
105
            }
106 8
            $instance = new $alias;
107 8
            if (!$instance instanceof XmlConvertibleInterface) {
108 1
                throw new \UnexpectedValueException(
109 1
                    "All aliases must be instance of " . XmlConvertibleInterface::class,
110 1
                    3
111
                );
112
            }
113 7
            unset($aliases[$key]);
114 7
            $aliases[is_int($key) ? $instance->getXmlElementName() : $key] = $instance;
115
        }
116
117 7
        $nodeObject = $aliases[$document->nodeName] ?? new XmlConvertibleObject;
118 7
        $properties = $nodeObject->getXmlProperties();
119
120
        /** @var \DOMAttr $attribute */
121 7
        foreach ($document->attributes as $attribute) {
122 7
            if (!$nodeObject instanceof XmlConvertibleObject) {
123 4
                if (!in_array($attribute->name, $properties)) {
124 1
                    throw new \UnexpectedValueException(
125 1
                        get_class($nodeObject) . ' must have defined ' . $attribute->name . ' XML property',
126 1
                        4
127
                    );
128
                }
129
            }
130 7
            $nodeObject->{$attribute->name} = $attribute->value;
131
        }
132
133 6
        $nodeObject->xmlChildren = [];
134
        /** @var \DOMElement $childNode */
135 6
        foreach ($document->childNodes as $childNode) {
136 6
            $nodeObject->xmlChildren[] = static::fromXml($childNode, $aliases);
137
        }
138 6
        $nodeObject->xmlElementName = $document->nodeName;
139
140 6
        return $nodeObject;
141
    }
142
143
    /**
144
     * Getting array of property names which will be used as attributes in created XML
145
     *
146
     * @param array|null $properties
147
     * @return array|\ReflectionProperty[]
148
     */
149 13
    public function getXmlProperties(array $properties = null): array
150
    {
151 13
        $properties = $properties
152 5
            ? Collection::from($properties)
153 10
            : Collection::from((new \ReflectionClass(get_called_class()))->getProperties(\ReflectionProperty::IS_PUBLIC))
154
                ->map(function (\ReflectionProperty $property) {
155 10
                    return $property->name;
156 13
                });
157
158
        return $properties
159 13
            ->filter(function (string $property): bool {
160 13
                return !in_array($property, ['xmlChildren', 'xmlElementName']);
161 13
            })
162 13
            ->array;
163
    }
164
}