PhpToXml   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A convert() 0 7 1
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