PhpStructReader   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A readSimpleElement() 0 3 1
A __construct() 0 4 1
A readStructure() 0 24 6
A readArrayElement() 0 11 4
A readObjectElement() 0 5 1
1
<?php
2
3
namespace bultonFr\PhpToXml;
4
5
use \XmlWriter;
6
7
class PhpStructReader
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
     * Read php date and call XmlWriter method to write elements
19
     * 
20
     * @param XmlWriter $xmlWriter Instance of XmlWriter
21
     * @param mixed $data Data to read
22
     */
23
    public function __construct(XmlWriter $xmlWriter, &$data)
24
    {
25
        $this->xmlWriter = $xmlWriter;
26
        $this->readStructure($data);
27
    }
28
    
29
    /**
30
     * Read a php structure and call method to write xml element
31
     * 
32
     * @param mixed $data Datas to read
33
     * 
34
     * @return void
35
     */
36
    protected function readStructure(&$data)
37
    {
38
        //Considering not possible.
39
        //Simple element only in complex structure.
40
        if (!is_array($data) && !is_object($data)) {
41
            return;
42
        }
43
        
44
        foreach ($data as $elementName => &$elementValue) {
45
            if (is_array($elementValue)) {
46
                $this->readArrayElement($elementName, $elementValue);
47
                continue;
48
            }
49
            
50
            if (is_object($elementValue)) {
51
                $this->readObjectElement($elementName, $elementValue);
52
                continue;
53
            }
54
            
55
            $this->readSimpleElement($elementName, $elementValue);
56
        }
57
        
58
        //Only remove the reference on $elementValue.
59
        unset($elementValue);
60
    }
61
    
62
    /**
63
     * Write a xml element for an array structure
64
     * Loop on all datas and call a new instance of this class to read datas
65
     * if data is an array or an object.
66
     * The new instance is to by-pass recursive error.
67
     * If is not an array or object, call readSimpleElement.
68
     * 
69
     * @param string $elementName The name of the element 
70
     * @param mixed $elementValue Datas for this element
71
     * 
72
     * @return void
73
     */
74
    protected function readArrayElement($elementName, &$elementValue)
75
    {
76
        foreach ($elementValue as $elementItem) {
77
            if (!is_array($elementItem) && !is_object($elementItem)) {
78
                $this->readSimpleElement($elementName, $elementItem);
79
                continue;
80
            }
81
                
82
            $this->xmlWriter->startElement($elementName);
83
            new PhpStructReader($this->xmlWriter, $elementItem);
84
            $this->xmlWriter->endElement();
85
        }
86
    }
87
    
88
    /**
89
     * Write a xml element for an object structure
90
     * Call a new instance of this class to read datas in array
91
     * The new instance is to by-pass recursive error.
92
     * 
93
     * @param string $elementName The name of the element 
94
     * @param mixed $elementValue Datas for this element
95
     * 
96
     * @return void
97
     */
98
    protected function readObjectElement($elementName, &$elementValue)
99
    {
100
        $this->xmlWriter->startElement($elementName);
101
        new PhpStructReader($this->xmlWriter, $elementValue);
102
        $this->xmlWriter->endElement();
103
    }
104
    
105
    /**
106
     * Write a simple xml element
107
     * 
108
     * @param string $elementName The name of the element 
109
     * @param mixed $elementValue Datas for this element
110
     * 
111
     * @return void
112
     */
113
    protected function readSimpleElement($elementName, &$elementValue)
114
    {
115
        $this->xmlWriter->writeElement($elementName, $elementValue);
116
    }
117
}
118