fantasia /core/classes/framework/xmlwrapper/xmlwrapper.php

Language PHP Lines 277
MD5 Hash f33d00afececdaf2bbe09d292f556cc1
Repository https://github.com/GinoPane/fantasia.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
 * 
 * Service class for XML handling, converts xml to arrays and arrays to xml
 * @see XmlWrapper::arrayToXml, XmlWrapper::xmlToArray
 * 
 * 
 * @author Sergey Karavay
 *  
 */
class XmlWrapper {
    
    private $_xml = null;
    private $_encoding = 'UTF-8';
    private $_options = array();
    
    private function __construct($options = array()) {
        $version = "1.0";
        $formatOutput = true;
        
        class_exists('cfg_app') ? $encoding = Cfg_App::DEFAULT_ENCODING : $encoding = 'utf-8';
        
        extract($options, EXTR_IF_EXISTS | EXTR_OVERWRITE);
        
        $this->_xml = new DomDocument($version, $encoding);
        
        $this->_options = $options;
        
        $this->_xml->formatOutput = $formatOutput;
        
        $this->_encoding = $encoding;
    }
    
    /**
     *
     * 
     * @param array $options Options for parser; 
     *  - 'version'
     *  - 'formatOutput' 
     *  - 'encoding'
     *  - 'numericKeysName'
     * 
     * @return XmlWrapper
     */
    public static function getParser($options = array())
    {
        return new XmlWrapper($options);
    }
    
    /**
     * Converts an Array to XML
     * 
     * @param array $data array to be converted
     * @param array $options array of options. Will be merged with existing. 
     * 
     * @see XmlWrapper::getParser() for more
     * @return DomDocument
     */
    public function arrayToXml(array $data, $options = array()) {
        $xml = $this->_getXMLRoot();
        
        $rootNodeName = 'root';
        
        extract($options, EXTR_IF_EXISTS | EXTR_OVERWRITE);
        
        $this->_options = array_merge($this->_options, $options);
        
        $xml->appendChild($this->_convertArrayToXml($rootNodeName, $data));

        return $xml->saveXML();
    }
    
    /**
     * 
     * Converts xml string to array
     * 
     * @param mixed $inputXml DOMDocument instance or a valid xml string
     * @return array converted array
     */
    public function xmlToArray($inputXml) {
        $xml = $this->_getXMLRoot();
        
        $error = false;
        
        if (is_string($inputXml)) {
            if (!$xml->loadXML($inputXml)) {
                trigger_error('Error parsing the XML string.', E_USER_WARNING);
                $error = true;
            }
        } else {
            if (is_a($inputXml, 'DOMDocument')) {
                trigger_error('The input XML object should be descendant of DOMDocument', E_USER_WARNING);
                $error = true;
            }
            
            $xml = $this->_xml = $inputXml;
        }
        
        if (!$error)
        {
            $output = array();
            
            $output[$xml->documentElement->tagName] = 
                $this->_convertXmlToArray($xml->documentElement);
            
            return $output;
        }
        else
        {
            return null;
        }
    }
    
    /**
     * Convert an Array to XML
     * @param string $node_name - name of the root node to be converted
     * @param array $arr - aray to be converterd
     * @return DOMNode
     */
    private function _convertArrayToXml($nodeName, $arr = array()) 
    {
        $node = $this->_xml->createElement($nodeName);

        if (is_array($arr)) {
            if (isset($arr['@attributes'])) {
                foreach ($arr['@attributes'] as $key => $value) {
                    if (!$this->_isValidTagName($key)) {
                        trigger_error("Illegal attribute name: \"{$key}\" in node \"{$nodeName}\"");
                    }
                    $node->setAttribute($key, self::_valueToString($value));
                }
                unset($arr['@attributes']);
            }

            // check if it has a value stored in @value, if yes store the value and return
            // else check if its directly stored as string
            if (isset($arr['@value'])) {
                $node->appendChild($this->_xml->createTextNode($this->_valueToString($arr['@value'])));
                unset($arr['@value']);
                return $node;
            } else if (isset($arr['@cdata'])) {
                $node->appendChild($this->_xml->createCDATASection($this->_valueToString($arr['@cdata'])));
                unset($arr['@cdata']);
                return $node;
            }
        }

        if (is_array($arr)) {
            foreach ($arr as $key => $value) {
                if (!$this->_isValidTagName($key)) {
                    trigger_error("Illegal tag name: \"{$key}\" in node \"{$nodeName}\"");
                }
                if (is_array($value) && is_numeric(key($value))) {
                    $numericKeyName = isset($this->_options['numericKeysName']) ? $this->_options['numericKeysName'] : $key;
                    foreach ($value as $subValue) {
                        $node->appendChild($this->_convertArrayToXml(
                                $numericKeyName, 
                                $subValue
                                ));
                    }
                } else {
                    $node->appendChild($this->_convertArrayToXml($key, $value));
                }
                unset($arr[$key]);
            }
        }

        if (!is_array($arr)) {
            $node->appendChild($this->_xml->createTextNode($this->_valueToString($arr)));
        }

        return $node;
    }
    
    /**
     * Convert an XML to array
     * 
     * @param DOMNode $node
     * @return array
     */
    private function _convertXmlToArray(DOMNode $node) {
        $output = array();

        switch ($node->nodeType) {
            case XML_CDATA_SECTION_NODE:
                $output['@cdata'] = trim($node->textContent);
                break;

            case XML_TEXT_NODE:
                $output = trim($node->textContent);
                break;

            case XML_ELEMENT_NODE:
                // for each child node, call the covert function recursively
                for ($i = 0, $m = $node->childNodes->length; $i < $m; $i++) {
                    $child = $node->childNodes->item($i);
                    $v = $this->_convertXmlToArray($child);
                    if (isset($child->tagName)) {
                        $t = $child->tagName;

                        // assume more nodes of same kind are coming
                        if (!isset($output[$t])) {
                            $output[$t] = array();
                        }
                        $output[$t][] = $v;
                    } else {
                        //check if it is not an empty text node
                        if ($v !== '') {
                            $output = $v;
                        }
                    }
                }

                if (is_array($output)) {
                    // if only one node of its kind, assign it directly instead if array($value);
                    foreach ($output as $t => $v) {
                        if (is_array($v) && count($v) == 1) {
                            $output[$t] = $v[0];
                        }
                    }
                    if (empty($output)) {
                        //for empty nodes
                        $output = '';
                    }
                }

                // loop through the attributes and collect them
                if ($node->attributes->length) {
                    $a = array();
                    foreach ($node->attributes as $attrName => $attrNode) {
                        $a[$attrName] = (string) $attrNode->value;
                    }
                    // if its an leaf node, store the value in @value instead of directly storing it.
                    if (!is_array($output)) {
                        $output = array('@value' => $output);
                    }
                    $output['@attributes'] = $a;
                }
                break;
        }
        
        return $output;
    }
    
    private function _getXMLRoot() {
        if (!$this->_xml) {
            $this->_xml = new DOMDocument();
        }
        
        return $this->_xml;
    }
    
    /*
     * Get string representation of the value
     */
    private function _valueToString($value) {
        if (!is_bool($value)) {
            return (string)$value;
        } else {
            return $value ? 'true' : 'false';
        }
    }

    /**
     * Check if the tag name or attribute name contains illegal characters
     * @link http://www.w3.org/TR/xml/#sec-common-syn
     * 
     * @param string $tag
     * @return bool
     */

    private function _isValidTagName($tag) {
        $matches = array();
        $pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i';
        return preg_match($pattern, $tag, $matches) && $matches[0] == $tag;
    }
};
Back to Top