AbstractRequest::toXml()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Ratepay\Business\Api\Model;
9
10
use SprykerEco\Zed\Ratepay\Business\Api\Builder\BuilderInterface;
11
use SprykerEco\Zed\Ratepay\Business\Api\SimpleXMLElement;
12
13
abstract class AbstractRequest implements RequestInterface
14
{
15
    /**
16
     * @return array
17
     */
18
    abstract protected function buildData();
19
20
    /**
21
     * @return string
22
     */
23
    abstract public function getRootTag();
24
25
    /**
26
     * @param array $array
27
     * @param \SprykerEco\Zed\Ratepay\Business\Api\SimpleXMLElement &$xml
28
     *
29
     * @return void
30
     */
31
    protected function arrayToXml(array $array, SimpleXMLElement $xml)
32
    {
33
        foreach ($array as $key => $value) {
34
            $this->keyValueToXml($key, $value, $xml);
35
        }
36
    }
37
38
    /**
39
     * @param string $key
40
     * @param mixed $value
41
     * @param \SprykerEco\Zed\Ratepay\Business\Api\SimpleXMLElement &$xml
42
     *
43
     * @return void
44
     */
45
    protected function keyValueToXml($key, $value, SimpleXMLElement $xml)
46
    {
47
        if ($value !== null) {
48
            if ($value instanceof BuilderInterface) {
49
                if (is_numeric($key)) {
50
                    $key = $value->getRootTag();
51
                }
52
                $value = $value->buildData();
53
            }
54
55
            if (substr($key, 0, 1) === '@') {
56
                $attributeKey = substr($key, 1);
57
                $xml->addAttribute($attributeKey, $value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array; however, parameter $value of SimpleXMLElement::addAttribute() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
                $xml->addAttribute($attributeKey, /** @scrutinizer ignore-type */ $value);
Loading history...
58
                return;
59
            }
60
61
            if ($key === '#') {
62
                $dom = dom_import_simplexml($xml);
63
                $owner = $dom->ownerDocument;
64
                $dom->appendChild($owner->createCDATASection($value));
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array; however, parameter $data of DOMDocument::createCDATASection() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
                $dom->appendChild($owner->createCDATASection(/** @scrutinizer ignore-type */ $value));
Loading history...
65
                return;
66
            }
67
68
            if (is_array($value)) {
69
                $subnode = $xml->addChild($key);
70
                $this->arrayToXml($value, $subnode);
71
                return;
72
            }
73
74
            $xml->addCDataChild($key, $value);
75
        }
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function toXml()
82
    {
83
        $data = $this->buildData();
84
        $rootTag = $this->getRootTag();
85
        $xml = new SimpleXMLElement('<' . $rootTag . '></' . $rootTag . '>');
86
        $this->arrayToXml($data, $xml);
87
88
        return $xml->asXML();
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function __toString()
95
    {
96
        return $this->toXml();
97
    }
98
}
99