Completed
Pull Request — master (#8)
by
unknown
02:25
created

XmlWriter::writeItem()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 11
nc 2
nop 1
crap 4
1
<?php
2
3
namespace Mathielen\DataImport\Writer;
4
5
use Ddeboer\DataImport\Writer\WriterInterface;
6
7
/**
8
 * Writes data to a xml file.
9
 */
10
class XmlWriter implements WriterInterface
11
{
12
    /**
13
     * @var \DOMDocument
14
     */
15
    private $xml;
16
    private $filename;
17
18
    /**
19
     * @var \DOMNode
20
     */
21
    private $rootNode;
22
23
    private $rowNodeName;
24
    private $rootNodeName;
25
    private $encoding;
26 1
    private $version;
27
28 1 View Code Duplication
    public function __construct(\SplFileObject $file, $rowNodeName = 'node', $rootNodeName = 'root', $encoding = null, $version = '1.0')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29 1
    {
30 1
        $this->filename = $file->getRealPath();
31 1
        $this->rowNodeName = $rowNodeName;
32
        $this->rootNodeName = $rootNodeName;
33
        $this->encoding = $encoding;
34
        $this->version = $version;
35
    }
36 1
37
    /**
38 1
     * {@inheritdoc}
39 1
     */
40 1
    public function prepare()
41
    {
42 1
        $this->xml = (!empty($this->encoding)) ? new \DOMDocument($this->version, $this->encoding) : new \DOMDocument();
43
        $this->rootNode = $this->xml->createElement($this->rootNodeName);
44
        $this->xml->appendChild($this->rootNode);
45
46
        return $this;
47
    }
48
49
    /**
50 1
     * (non-PHPdoc).
51
     *
52 1
     * @see \Ddeboer\DataImport\Writer\WriterInterface::writeItem()
53
     */
54
    public function writeItem(array $item)
55 1
    {
56 1
        $newNode = $this->xml->createElement($this->rowNodeName);
57 1
58 1
        //attributes
59 1
        if (isset($item['@attributes']) && is_array($item['@attributes'])) {
60
            foreach ($item['@attributes'] as $key => $value) {
61 1
                $attr = $this->xml->createAttribute($key);
62
                $attr->value = $value;
63
                $newNode->appendChild($attr);
64
            }
65 1
            unset($item['@attributes']);
66 1
        }
67 1
68 1
        //values
69
        $this->writeChild($newNode, $item);
70
71 1
        $this->rootNode->appendChild($newNode);
72
73 1
        return $this;
74
    }
75
76
    /**
77
     * @param \DOMElement $newNode
78
     * @param array $item
79 1
     */
80
    protected function writeChild(\DOMElement $newNode, array $item)
81 1
    {
82
        foreach ($item as $key => $value) {
83 1
            $node = $this->xml->createElement($key);
84
            if(is_array($value)){
85
                $this->writeChild($node, $value);
86
            } else {
87
                $node->nodeValue = $value;
88
            }
89
            $newNode->appendChild($node);
90
        }
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function finish()
97
    {
98
        $this->xml->save($this->filename);
99
100
        return $this;
101
    }
102
}
103