DalliApiBody::getAsXmlString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace floor12\DalliApi\Models;
4
5
use floor12\DalliApi\Exceptions\EmptyApiMethodException;
6
use ReflectionClass;
7
use ReflectionException;
8
use SimpleXMLElement;
9
10
class DalliApiBody extends BaseXmlObject
11
{
12
    /** @var string */
13
    protected $authToken;
14
    /** @var string */
15
    protected $apiMethodName;
16
    /** @var SimpleXMLElement */
17
    public $mainElement;
18
    /** @var array|null */
19
    private $params;
20
21
    public $user_id;
22
    public $order_id;
23
24
    /**
25
     * DalliApiBody constructor.
26
     * @param string|null $apiMethodName
27 6
     * @param array|null $params
28
     * @throws EmptyApiMethodException
29 6
     */
30 1
    public function __construct(?string $apiMethodName, ?array $params = [], ?int $user_id = null, ?int $order_id = null)
31
    {
32 5
        if (empty($apiMethodName))
33 5
            throw new EmptyApiMethodException();
34 5
35 5
        $this->apiMethodName = $apiMethodName;
36 5
        $this->mainElement = new SimpleXMLElement("<$this->apiMethodName></$this->apiMethodName>");
37
        $this->params = $params;
38
        $this->user_id = $user_id;
39 5
        $this->order_id = $order_id;
40
        $this->parseParamsToXml();
41 5
    }
42 4
43 1
44 1
    private function parseParamsToXml(): void
45
    {
46
        if (empty($this->params))
47
            return;
48
        $this->addParamArrayToElement($this->mainElement, $this->params);
49
    }
50 1
51
    /**
52 1
     * @param SimpleXMLElement $element
53 1
     * @param array $paramsArray
54 1
     */
55 1
    private function addParamArrayToElement(SimpleXMLElement $element, array $paramsArray): void
56
    {
57 1
        foreach ($paramsArray as $paramName => $paramValue) {
58
            $child = $element->addChild($paramName);
59
            if (is_array($paramValue)) {
60 1
                $this->addParamArrayToElement($child, $paramValue);
61
            } else {
62
                $child[0] = $paramValue;
63
            }
64
        }
65
    }
66
67 1
    /**
68
     * @param BaseXmlObject $object
69 1
     * @return $this
70 1
     * @throws ReflectionException
71 1
     */
72 1
    public function add(BaseXmlObject $object): self
73
    {
74 1
        $className = mb_strtolower((new ReflectionClass($object))->getShortName());
75
        $mainElement = $this->mainElement->addChild($className);
76
        foreach ($object as $attributeName => $attributeValue) {
77
            $this->processAttributeNameAndValue($mainElement, $attributeName, $attributeValue);
78
        }
79
        return $this;
80
    }
81 5
82
    /**
83 5
     * @param BaseXmlObject|null $object
84
     * @return string
85
     */
86
    public function getAsXmlString(BaseXmlObject $object = null): string
87
    {
88
        return $this->mainElement->asXML();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->mainElement->asXML() could return the type true which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
89
    }
90
}
91