Passed
Push — master ( 3c19d1...2ca449 )
by Evgenii
09:11
created

BaseXmlObject::addValueAsString()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 4
rs 10
1
<?php
2
3
4
namespace floor12\DalliApi\Models;
5
6
7
use ReflectionClass;
8
use ReflectionException;
9
use SimpleXMLElement;
10
11
abstract class BaseXmlObject
12
{
13
    /**
14
     * @param BaseXmlObject $object
15
     * @return SimpleXMLElement
16
     * @throws ReflectionException
17
     */
18 7
    public function getAsXmlObject(BaseXmlObject $object = null): SimpleXMLElement
19
    {
20 7
        if (empty($object))
21 7
            $object = $this;
22 7
        $className = mb_strtolower((new ReflectionClass($object))->getShortName());
23 7
        $mainElement = new SimpleXMLElement("<$className></$className>");
24 7
        foreach ($object as $attributeName => $attributeValue) {
25 7
            $this->processAttributeNameAndValue($mainElement, $attributeName, $attributeValue);
26
        }
27 7
        return $mainElement;
28
    }
29
30
    /**
31
     * @param $mainElement
32
     * @param $attributeName
33
     * @param $attributeValue
34
     */
35 7
    protected function processAttributeNameAndValue($mainElement, $attributeName, $attributeValue): void
36
    {
37 7
        if (empty($attributeValue))
38 4
            return;
39 7
        if (is_array($attributeValue)) {
40 2
            $this->addValueAsArray($mainElement, $attributeName, $attributeValue);
41 7
        } elseif (is_object($attributeValue)) {
42 2
            $this->addValueAsObject($mainElement, $attributeValue);
43
        } else {
44 7
            $this->addValueAsString($mainElement, $attributeName, $attributeValue);
45
        }
46 7
    }
47
48
    /**
49
     * @param $mainElement
50
     * @param $attributeName
51
     * @param $attributeValue
52
     */
53 2
    protected function addValueAsArray($mainElement, $attributeName, $attributeValue)
54
    {
55 2
        $itemsElement = $mainElement->addChild($attributeName);
56 2
        foreach ($attributeValue as $item) {
57 2
            $itemElement = $itemsElement->addChild($item->getAsXmlObject()->getName(), $item->getAsXmlObject());
58 2
            foreach ($item as $itemAttributeName => $itemAttributeValue) {
59 2
                $this->addValueAsString($itemElement, $itemAttributeName, $itemAttributeValue);
60
            }
61
        }
62 2
    }
63
64
    /**
65
     * @param $mainElement
66
     * @param $attributeValue
67
     */
68 2
    protected function addValueAsObject($mainElement, $attributeValue)
69
    {
70 2
        $newXmlObject = $mainElement->addChild($attributeValue->getAsXmlObject()->getName());
71 2
        foreach ($attributeValue->getAsXmlObject()->children() as $child)
72 2
            $newXmlObject->addChild($child->getName(), $child);
73 2
    }
74
75
    /**
76
     * @param SimpleXMLElement $mainElement
77
     * @param $attributeName
78
     * @param $attributeValue
79
     * @return void
80
     */
81 7
    protected function addValueAsString(SimpleXMLElement $mainElement, $attributeName, $attributeValue):void
82
    {
83 7
        if (empty($attributeValue))
84 2
            return;
85 7
        if ($attributeName === 'title') {
86 1
            $mainElement[0]=$attributeValue;
87 7
        } elseif (substr($attributeName, 0, 1) === '_') {
88 6
             $mainElement->addAttribute(substr($attributeName, 1), $attributeValue);
89
        } else {
90 3
             $mainElement->addChild($attributeName, $attributeValue);
91
        }
92 7
    }
93
94
    /**
95
     * @param BaseXmlObject|null $object
96
     * @return string
97
     * @throws ReflectionException
98
     */
99 6
    public function getAsXmlString(BaseXmlObject $object = null): string
100
    {
101 6
        return $this->getAsXmlObject($object)->asXML();
102
    }
103
}
104