Completed
Push — master ( 4474f6...7e8ad4 )
by Sergey
04:22 queued 02:10
created

XMLFormatter::appendPhonesNodes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace seregazhuk\SmsIntel\Formatters;
4
5
use DOMElement;
6
use DOMDocument;
7
8
class XMLFormatter
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $params;
14
15
    /**
16
     * @var DOMDocument
17
     */
18
    protected $dom;
19
20
    /**
21
     * @var DOMElement
22
     */
23
    protected $dataNode;
24
25
    public function __construct(array $params)
26
    {
27
        $this->params = $params;
28
    }
29
    /**
30
     * @return string
31
     */
32
    public function toXml()
33
    {
34
        return $this
35
            ->initDom()
36
            ->createParamsNodes()
37
            ->getXml();
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    protected function getXml()
44
    {
45
        return trim($this->dom->saveXml($this->dom, LIBXML_NOEMPTYTAG));
46
    }
47
48
    /**
49
     * @return $this
50
     */
51
    protected function initDom()
52
    {
53
        $this->dom = new \DOMDocument();
54
        $this->dataNode = $this->dom->createElement('data');
55
        $this->dom->appendChild($this->dataNode);
56
57
        return $this;
58
    }
59
60
    /**
61
     * @return $this
62
     */
63
    protected function createParamsNodes()
64
    {
65
        foreach ($this->params as $key => $value) {
66
            $this->createParamNode($key, $value);
67
        }
68
69
        return $this;
70
    }
71
72
    /**
73
     * @param array $phones
74
     */
75
    protected function appendPhonesNodes(array $phones)
76
    {
77
        foreach ($phones as $phone) {
78
            $phoneNode = $this->dom->createElement('to');
79
            $phoneNode->setAttribute('number', $phone);
80
81
            $this->dataNode->appendChild($phoneNode);
82
        }
83
    }
84
85
    /**
86
     * @param string $key
87
     * @param mixed $value
88
     */
89
    protected function createParamNode($key, $value)
90
    {
91
        if ($key == 'to') {
92
            $this->appendPhonesNodes($value);
93
            return;
94
        }
95
96
        $node = $this->dom->createElement($key, $value);
97
        $this->dataNode->appendChild($node);
98
    }
99
}
100