PhpToXml::convert()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace bultonFr\PhpToXml;
4
5
use \XmlWriter;
6
7
class PhpToXml
8
{
9
    /**
10
     * @var \XmlWriter $xmlWriter Instance of XmlWriter used
11
     *  to create the xml document
12
     */
13
    protected $xmlWriter;
14
    
15
    /**
16
     * Constructor
17
     * 
18
     * Initialize XmlWriter instance
19
     */
20
    public function __construct()
21
    {
22
        $this->xmlWriter = new XmlWriter;
23
        $this->xmlWriter->openMemory();
24
        $this->xmlWriter->setIndent(true);
25
    }
26
    
27
    /**
28
     * Convert a php data to xml format
29
     * 
30
     * @param mixed $datas Data to convert
31
     * @param string $encoding (Default "UTF-8") Encoding to use for xml
32
     * 
33
     * @return string Xml document
34
     */
35
    public function convert($datas, $encoding='UTF-8')
36
    {
37
        $this->xmlWriter->startDocument('1.0', $encoding);        
38
        new PhpStructReader($this->xmlWriter, $datas);
39
        $this->xmlWriter->endDocument();
40
        
41
        return $this->xmlWriter->outputMemory(false);
42
    }
43
}
44