Skip to content

Commit fb9da09

Browse files
committed
added autoloading
1 parent e510507 commit fb9da09

File tree

4 files changed

+168
-139
lines changed

4 files changed

+168
-139
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
/composer.lock

lib/array2xml.php renamed to LSS/Array2XML.php

Lines changed: 0 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -183,142 +183,3 @@ private static function isValidTagName($tag){
183183
}
184184
}
185185

186-
/**
187-
* XML2Array: A class to convert XML to array in PHP
188-
* It returns the array which can be converted back to XML using the Array2XML script
189-
* It takes an XML string or a DOMDocument object as an input.
190-
*
191-
* See Array2XML: http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes
192-
*
193-
* Author : Lalit Patel
194-
* Website: http://www.lalit.org/lab/convert-xml-to-array-in-php-xml2array
195-
* License: Apache License 2.0
196-
* http://www.apache.org/licenses/LICENSE-2.0
197-
* Version: 0.1 (07 Dec 2011)
198-
* Version: 0.2 (04 Mar 2012)
199-
* Fixed typo 'DomDocument' to 'DOMDocument'
200-
*
201-
* Usage:
202-
* $array = XML2Array::createArray($xml);
203-
*/
204-
205-
class XML2Array {
206-
207-
private static $xml = null;
208-
private static $encoding = 'UTF-8';
209-
210-
/**
211-
* Initialize the root XML node [optional]
212-
* @param $version
213-
* @param $encoding
214-
* @param $format_output
215-
*/
216-
public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) {
217-
self::$xml = new DOMDocument($version, $encoding);
218-
self::$xml->formatOutput = $format_output;
219-
self::$encoding = $encoding;
220-
}
221-
222-
/**
223-
* Convert an XML to Array
224-
* @param string $node_name - name of the root node to be converted
225-
* @param array $arr - aray to be converterd
226-
* @return DOMDocument
227-
*/
228-
public static function &createArray($input_xml) {
229-
$xml = self::getXMLRoot();
230-
if(is_string($input_xml)) {
231-
$parsed = $xml->loadXML($input_xml);
232-
if(!$parsed) {
233-
throw new Exception('[XML2Array] Error parsing the XML string.');
234-
}
235-
} else {
236-
if(get_class($input_xml) != 'DOMDocument') {
237-
throw new Exception('[XML2Array] The input XML object should be of type: DOMDocument.');
238-
}
239-
$xml = self::$xml = $input_xml;
240-
}
241-
$array[$xml->documentElement->tagName] = self::convert($xml->documentElement);
242-
self::$xml = null; // clear the xml node in the class for 2nd time use.
243-
return $array;
244-
}
245-
246-
/**
247-
* Convert an Array to XML
248-
* @param mixed $node - XML as a string or as an object of DOMDocument
249-
* @return mixed
250-
*/
251-
private static function &convert($node) {
252-
$output = array();
253-
254-
switch ($node->nodeType) {
255-
case XML_CDATA_SECTION_NODE:
256-
$output['@cdata'] = trim($node->textContent);
257-
break;
258-
259-
case XML_TEXT_NODE:
260-
$output = trim($node->textContent);
261-
break;
262-
263-
case XML_ELEMENT_NODE:
264-
265-
// for each child node, call the covert function recursively
266-
for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
267-
$child = $node->childNodes->item($i);
268-
$v = self::convert($child);
269-
if(isset($child->tagName)) {
270-
$t = $child->tagName;
271-
272-
// assume more nodes of same kind are coming
273-
if(!isset($output[$t])) {
274-
$output[$t] = array();
275-
}
276-
$output[$t][] = $v;
277-
} else {
278-
//check if it is not an empty text node
279-
if($v !== '') {
280-
$output = $v;
281-
}
282-
}
283-
}
284-
285-
if(is_array($output)) {
286-
// if only one node of its kind, assign it directly instead if array($value);
287-
foreach ($output as $t => $v) {
288-
if(is_array($v) && count($v)==1) {
289-
$output[$t] = $v[0];
290-
}
291-
}
292-
if(empty($output)) {
293-
//for empty nodes
294-
$output = '';
295-
}
296-
}
297-
298-
// loop through the attributes and collect them
299-
if($node->attributes->length) {
300-
$a = array();
301-
foreach($node->attributes as $attrName => $attrNode) {
302-
$a[$attrName] = (string) $attrNode->value;
303-
}
304-
// if its an leaf node, store the value in @value instead of directly storing it.
305-
if(!is_array($output)) {
306-
$output = array('@value' => $output);
307-
}
308-
$output['@attributes'] = $a;
309-
}
310-
break;
311-
}
312-
return $output;
313-
}
314-
315-
/*
316-
* Get the root XML node, if there isn't one, create it.
317-
*/
318-
private static function getXMLRoot(){
319-
if(empty(self::$xml)) {
320-
self::init();
321-
}
322-
return self::$xml;
323-
}
324-
}

LSS/XML2Array.php

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
/**
3+
* OpenLSS - Lighter Smarter Simpler
4+
*
5+
* This file is part of OpenLSS.
6+
*
7+
* OpenLSS is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 3 of
10+
* the License, or (at your option) any later version.
11+
*
12+
* OpenLSS is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the
18+
* GNU Lesser General Public License along with OpenLSS.
19+
* If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
namespace LSS;
22+
23+
/**
24+
* XML2Array: A class to convert XML to array in PHP
25+
* It returns the array which can be converted back to XML using the Array2XML script
26+
* It takes an XML string or a DOMDocument object as an input.
27+
*
28+
* See Array2XML: http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes
29+
*
30+
* Author : Lalit Patel
31+
* Website: http://www.lalit.org/lab/convert-xml-to-array-in-php-xml2array
32+
* License: Apache License 2.0
33+
* http://www.apache.org/licenses/LICENSE-2.0
34+
* Version: 0.1 (07 Dec 2011)
35+
* Version: 0.2 (04 Mar 2012)
36+
* Fixed typo 'DomDocument' to 'DOMDocument'
37+
*
38+
* Usage:
39+
* $array = XML2Array::createArray($xml);
40+
*/
41+
42+
class XML2Array {
43+
44+
private static $xml = null;
45+
private static $encoding = 'UTF-8';
46+
47+
/**
48+
* Initialize the root XML node [optional]
49+
* @param $version
50+
* @param $encoding
51+
* @param $format_output
52+
*/
53+
public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) {
54+
self::$xml = new DOMDocument($version, $encoding);
55+
self::$xml->formatOutput = $format_output;
56+
self::$encoding = $encoding;
57+
}
58+
59+
/**
60+
* Convert an XML to Array
61+
* @param string $node_name - name of the root node to be converted
62+
* @param array $arr - aray to be converterd
63+
* @return DOMDocument
64+
*/
65+
public static function &createArray($input_xml) {
66+
$xml = self::getXMLRoot();
67+
if(is_string($input_xml)) {
68+
$parsed = $xml->loadXML($input_xml);
69+
if(!$parsed) {
70+
throw new Exception('[XML2Array] Error parsing the XML string.');
71+
}
72+
} else {
73+
if(get_class($input_xml) != 'DOMDocument') {
74+
throw new Exception('[XML2Array] The input XML object should be of type: DOMDocument.');
75+
}
76+
$xml = self::$xml = $input_xml;
77+
}
78+
$array[$xml->documentElement->tagName] = self::convert($xml->documentElement);
79+
self::$xml = null; // clear the xml node in the class for 2nd time use.
80+
return $array;
81+
}
82+
83+
/**
84+
* Convert an Array to XML
85+
* @param mixed $node - XML as a string or as an object of DOMDocument
86+
* @return mixed
87+
*/
88+
private static function &convert($node) {
89+
$output = array();
90+
91+
switch ($node->nodeType) {
92+
case XML_CDATA_SECTION_NODE:
93+
$output['@cdata'] = trim($node->textContent);
94+
break;
95+
96+
case XML_TEXT_NODE:
97+
$output = trim($node->textContent);
98+
break;
99+
100+
case XML_ELEMENT_NODE:
101+
102+
// for each child node, call the covert function recursively
103+
for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
104+
$child = $node->childNodes->item($i);
105+
$v = self::convert($child);
106+
if(isset($child->tagName)) {
107+
$t = $child->tagName;
108+
109+
// assume more nodes of same kind are coming
110+
if(!isset($output[$t])) {
111+
$output[$t] = array();
112+
}
113+
$output[$t][] = $v;
114+
} else {
115+
//check if it is not an empty text node
116+
if($v !== '') {
117+
$output = $v;
118+
}
119+
}
120+
}
121+
122+
if(is_array($output)) {
123+
// if only one node of its kind, assign it directly instead if array($value);
124+
foreach ($output as $t => $v) {
125+
if(is_array($v) && count($v)==1) {
126+
$output[$t] = $v[0];
127+
}
128+
}
129+
if(empty($output)) {
130+
//for empty nodes
131+
$output = '';
132+
}
133+
}
134+
135+
// loop through the attributes and collect them
136+
if($node->attributes->length) {
137+
$a = array();
138+
foreach($node->attributes as $attrName => $attrNode) {
139+
$a[$attrName] = (string) $attrNode->value;
140+
}
141+
// if its an leaf node, store the value in @value instead of directly storing it.
142+
if(!is_array($output)) {
143+
$output = array('@value' => $output);
144+
}
145+
$output['@attributes'] = $a;
146+
}
147+
break;
148+
}
149+
return $output;
150+
}
151+
152+
/*
153+
* Get the root XML node, if there isn't one, create it.
154+
*/
155+
private static function getXMLRoot(){
156+
if(empty(self::$xml)) {
157+
self::init();
158+
}
159+
return self::$xml;
160+
}
161+
}

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@
2525
,"require": {
2626
"php": ">=5.3.2"
2727
}
28+
,"autoload": {
29+
"psr-0": {
30+
"LSS": ""
31+
}
32+
}
2833
}

0 commit comments

Comments
 (0)