XmlWriter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 58
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A openWriter() 0 4 1
A addRowToWriter() 0 8 2
A closeWriter() 0 4 1
A generateNode() 0 10 4
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-exportable-widget project.
5
 * (c) 2amigOS! <http://2amigos.us/>
6
 * For the full copyright and license information, please view
7
 * the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace dosamigos\exportable\writers;
11
12
use Box\Spout\Writer\AbstractWriter;
13
use dosamigos\exportable\exceptions\InvalidDataFormatException;
14
use RuntimeException;
15
16
class XmlWriter extends AbstractWriter
17
{
18
    /**
19
     * @var string Content-Type value for the header
20
     */
21
    protected static $headerContentType = 'text/xml';
22
    /**
23
     * @var string the root element of the data to be exported
24
     */
25
    protected $rootElement = 'data';
26
    /**
27
     * @var string the child element of each row data
28
     */
29
    protected $childElement = 'row';
30
31
    /**
32
     * @inheritdoc
33
     */
34
    protected function openWriter()
35
    {
36
        fwrite($this->filePointer, sprintf("<?xml version=\"1.0\" ?>\n<%s>\n", $this->rootElement));
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42
    protected function addRowToWriter(array $dataRow, $style)
43
    {
44
        fwrite($this->filePointer, sprintf("<%s>\n", $this->childElement));
45
        foreach ($dataRow as $key => $value) {
46
            $this->generateNode($key, $value);
47
        }
48
        fwrite($this->filePointer, sprintf("</%s>\n", $this->childElement));
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    protected function closeWriter()
55
    {
56
        fwrite($this->filePointer, sprintf("</%s>\n", $this->rootElement));
57
    }
58
59
    /**
60
     * @param string $name
61
     * @param string $value
62
     */
63
    protected function generateNode($name, $value)
64
    {
65
        if (is_array($value)) {
66
            throw new RuntimeException('Not implemented');
67
        } elseif (is_scalar($value) || is_null($value)) {
68
            fwrite($this->filePointer, sprintf("<%s><![CDATA[%s]]></%s>\n", $name, $value, $name));
69
        } else {
70
            throw new InvalidDataFormatException('Invalid data');
71
        }
72
    }
73
}
74