Passed
Push — master ( 221c35...fdb910 )
by Petr
02:59
created

SetNamespaceTrait::setNodeName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace kalanis\Pohoda\Common;
6
7
trait SetNamespaceTrait
8
{
9
    protected ?string $namespace = null;
10
11
    protected ?string $nodeName = null;
12
13
    /**
14
     * Set namespace.
15
     *
16
     * @param string $namespace
17
     *
18
     * @return void
19
     */
20 82
    public function setNamespace(string $namespace): void
21
    {
22 82
        $this->namespace = $namespace;
23
    }
24
25
    /**
26
     * Set node name.
27
     *
28
     * @param string $nodeName
29
     *
30
     * @return void
31
     */
32 63
    public function setNodeName(string $nodeName): void
33
    {
34 63
        $this->nodeName = $nodeName;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 55
    public function getXML(): \SimpleXMLElement
41
    {
42 55
        if (is_null($this->namespace)) {
43 1
            throw new \LogicException('Namespace not set.');
44
        }
45
46 54
        if (is_null($this->nodeName)) {
47 1
            throw new \LogicException('Node name not set.');
48
        }
49
50 53
        $xml = $this->createXML()->addChild($this->namespace . ':' . $this->nodeName, '', $this->namespace($this->namespace));
51
52 53
        $this->addElements($xml, $this->getDataElements(), 'typ');
53
54 53
        return $xml;
55
    }
56
57
    /**
58
     * Get namespace.
59
     *
60
     * @param string $short
61
     *
62
     * @return string
63
     */
64
    abstract protected function namespace(string $short): string;
65
66
    /**
67
     * Create XML.
68
     *
69
     * @return \SimpleXMLElement
70
     */
71
    abstract protected function createXML(): \SimpleXMLElement;
72
73
    /**
74
     * Add batch elements.
75
     *
76
     * @param \SimpleXMLElement $xml
77
     * @param string[]          $elements
78
     * @param string|null       $namespace
79
     *
80
     * @return void
81
     */
82
    abstract protected function addElements(\SimpleXMLElement $xml, array $elements, ?string $namespace = null): void;
83
84
    /**
85
     * Add elements defined as properties of DTOs
86
     *
87
     * @return string[]
88
     */
89
    abstract protected function getDataElements(bool $withAttributes = false): array;
90
}
91