XmlWriter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 93
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A prepare() 0 6 1
A writeItem() 0 10 2
A finish() 0 6 1
A flush() 0 4 1
1
<?php
2
3
namespace Port\Xml;
4
5
use Port\Writer;
6
use Port\Writer\FlushableWriter;
7
8
/**
9
 * Write data to XML
10
 *
11
 * @author Márk Sági-Kazár <[email protected]>
12
 */
13
class XmlWriter implements Writer, FlushableWriter
14
{
15
    /**
16
     * @var \XmlWriter
17
     */
18
    protected $xmlWriter;
19
20
    /**
21
     * @var string
22
     */
23
    protected $file;
24
25
    /**
26
     * @var string
27
     */
28
    protected $rootElement;
29
30
    /**
31
     * @var string
32
     */
33
    protected $itemElement;
34
35
    /**
36
     * @var string
37
     */
38
    protected $version;
39
40
    /**
41
     * @var string|null
42
     */
43
    protected $encoding;
44
45
    /**
46
     * @param string $file
47
     */
48
    public function __construct(
49
        \XmlWriter $xmlWriter,
50
        $file,
51
        $rootElement = 'items',
52
        $itemElement = 'item',
53
        $version = '1.0',
54
        $encoding = null
55
    ) {
56
        $this->xmlWriter = $xmlWriter;
57
        $this->file = $file;
58
        $this->rootElement = $rootElement;
59
        $this->itemElement = $itemElement;
60
        $this->version = $version;
61
        $this->encoding = $encoding;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function prepare()
68
    {
69
        $this->xmlWriter->openUri($this->file);
70
        $this->xmlWriter->startDocument($this->version, $this->encoding);
71
        $this->xmlWriter->startElement($this->rootElement);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function writeItem(array $item)
78
    {
79
        $this->xmlWriter->startElement($this->itemElement);
80
81
        foreach ($item as $key => $value) {
82
            $this->xmlWriter->writeElement($key, $value);
83
        }
84
85
        $this->xmlWriter->endElement();
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function finish()
92
    {
93
        $this->xmlWriter->endElement();
94
        $this->xmlWriter->endDocument();
95
        $this->flush();
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function flush()
102
    {
103
        $this->xmlWriter->flush();
104
    }
105
}
106