SetNamespaceTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 13
dl 0
loc 83
c 0
b 0
f 0
ccs 12
cts 12
cp 1
rs 10

3 Methods

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