XmlConverter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 16
dl 0
loc 58
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A convert() 0 4 1
A preserveWhiteSpace() 0 3 1
A formatOutput() 0 3 1
A processCountry() 0 8 1
A __construct() 0 7 1
1
<?php
2
3
namespace MLD\Converter;
4
5
use DOMDocument;
6
7
/**
8
 * Class XmlConverter
9
 */
10
class XmlConverter extends AbstractConverter
11
{
12
13
    /**
14
     * @var DOMDocument
15
     */
16
    private $domDocument;
17
18
    /**
19
     * @param array $countries
20
     */
21
    public function __construct(array $countries)
22
    {
23
        $this->domDocument = new \DOMDocument('1.0', 'UTF-8');
24
        $this->formatOutput();
25
        $this->preserveWhiteSpace();
26
        $this->domDocument->appendChild($this->domDocument->createElement('countries'));
27
        parent::__construct($countries);
28
    }
29
30
    /**
31
     * @return string data converted into XML
32
     */
33
    public function convert()
34
    {
35
        array_walk($this->countries, array($this, 'processCountry'));
36
        return $this->domDocument->saveXML();
37
    }
38
39
    /**
40
     * @param bool $formatOutput
41
     * @see \DOMDocument::$formatOutput
42
     */
43
    public function formatOutput($formatOutput = true)
44
    {
45
        $this->domDocument->formatOutput = $formatOutput;
46
    }
47
48
    /**
49
     * @param bool $preserveWhiteSpace
50
     * @see \DOMDocument::$preserveWhiteSpace
51
     */
52
    public function preserveWhiteSpace($preserveWhiteSpace = false)
53
    {
54
        $this->domDocument->preserveWhiteSpace = $preserveWhiteSpace;
55
    }
56
57
    /**
58
     * @param $array
59
     */
60
    private function processCountry(&$array)
61
    {
62
        $countryNode = $this->domDocument->createElement('country');
63
        $array = $this->convertArrays($array);
64
        array_walk($array, function ($value, $key) use ($countryNode) {
65
            $countryNode->setAttribute($key, $value);
66
        });
67
        $this->domDocument->documentElement->appendChild($countryNode);
68
    }
69
}