XmlFormatter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 24
dl 0
loc 62
ccs 26
cts 26
cp 1
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A raw() 0 6 2
A rowXml() 0 16 4
A toText() 0 3 1
A anydatasetXml() 0 12 2
1
<?php
2
3
namespace ByJG\AnyDataset\Core\Formatter;
4
5
use ByJG\AnyDataset\Core\GenericIterator;
6
use ByJG\Util\XmlUtil;
7
use DOMDocument;
8
use DOMNode;
9
10
class XmlFormatter extends BaseFormatter
11
{
12
    /**
13
     * Return a DOMNode representing AnyDataset
14
     *
15
     * @param array $collection
16
     * @return DOMNode
17
     */
18 4
    protected function anydatasetXml($collection)
19
    {
20 4
        $anyDataSet = XmlUtil::createXmlDocumentFromStr("<anydataset></anydataset>");
21 4
        $nodeRoot = $anyDataSet->getElementsByTagName("anydataset")->item(0);
22 4
        foreach ($collection as $sr) {
23 4
            $row = $this->rowXml($sr);
24 4
            $nodeRow = $row->getElementsByTagName("row")->item(0);
25 4
            $newRow = XmlUtil::createChild($nodeRoot, "row");
26 4
            XmlUtil::addNodeFromNode($newRow, $nodeRow);
27
        }
28
29 4
        return $anyDataSet;
30
    }
31
32
    /**
33
     * @param array $row
34
     * @return DOMDocument
35
     */
36 5
    protected function rowXml($row)
37
    {
38 5
        $node = XmlUtil::createXmlDocumentFromStr("<row></row>");
39 5
        $root = $node->getElementsByTagName("row")->item(0);
40 5
        foreach ($row as $key => $value) {
41 5
            if (!is_array($value)) {
42 5
                $field = XmlUtil::createChild($root, "field", $value);
43 5
                XmlUtil::addAttribute($field, "name", $key);
44
            } else {
45 1
                foreach ($value as $valueItem) {
46 1
                    $field = XmlUtil::createChild($root, "field", $valueItem);
47 1
                    XmlUtil::addAttribute($field, "name", $key);
48
                }
49
            }
50
        }
51 5
        return $node;
52
    }
53
54
55
    /**
56
     * @inheritDoc
57
     */
58 5
    public function raw()
59
    {
60 5
        if ($this->object instanceof GenericIterator) {
61 4
            return $this->anydatasetXml($this->object->toArray());
62
        }
63 1
        return $this->rowXml($this->object->toArray());
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69 3
    public function toText()
70
    {
71 3
        return $this->raw()->saveXML();
72
    }
73
}