Skip to content

Commit 6a7a621

Browse files
authored
Merge pull request #4 from h3rj4n/master
Allow XML to be inserted as child
2 parents f668666 + 58a84a5 commit 6a7a621

File tree

2 files changed

+90
-40
lines changed

2 files changed

+90
-40
lines changed

LSS/Array2XML.php

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22
/**
33
* OpenLSS - Lighter Smarter Simpler
44
*
5-
* This file is part of OpenLSS.
5+
* This file is part of OpenLSS.
66
*
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.
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.
1111
*
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.
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.
1616
*
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-
*/
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+
*/
2121
namespace LSS;
22+
2223
use \DomDocument;
2324
use \Exception;
2425

@@ -48,16 +49,20 @@
4849
* - Reverted to version 0.5
4950
* Version: 0.8 (02 May 2012)
5051
* - Removed htmlspecialchars() before adding to text node or attributes.
52+
* Version: 0.11 (28 October 2015)
53+
* - Fixed typos; Added support for plain insertion of XML trough @xml.
5154
*
5255
* Usage:
5356
* $xml = Array2XML::createXML('root_node_name', $php_array);
5457
* echo $xml->saveXML();
5558
*/
56-
5759
class Array2XML {
5860

61+
/**
62+
* @var DOMDocument
63+
*/
5964
private static $xml = null;
60-
private static $encoding = 'UTF-8';
65+
private static $encoding = 'UTF-8';
6166

6267
/**
6368
* Initialize the root XML node [optional]
@@ -68,7 +73,7 @@ class Array2XML {
6873
public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) {
6974
self::$xml = new DomDocument($version, $encoding);
7075
self::$xml->formatOutput = $format_output;
71-
self::$encoding = $encoding;
76+
self::$encoding = $encoding;
7277
}
7378

7479
/**
@@ -77,7 +82,7 @@ public static function init($version = '1.0', $encoding = 'UTF-8', $format_outpu
7782
* @param array $arr - aray to be converterd
7883
* @return DomDocument
7984
*/
80-
public static function &createXML($node_name, $arr=array()) {
85+
public static function &createXML($node_name, $arr = array()) {
8186
$xml = self::getXMLRoot();
8287
$xml->appendChild(self::convert($node_name, $arr));
8388

@@ -86,23 +91,29 @@ public static function &createXML($node_name, $arr=array()) {
8691
}
8792

8893
/**
89-
* Convert an Array to XML
90-
* @param string $node_name - name of the root node to be converted
91-
* @param array $arr - aray to be converterd
92-
* @return DOMNode
94+
* Convert an Array to XML.
95+
*
96+
* @param string $node_name
97+
* Name of the root node to be converted.
98+
* @param array $arr
99+
* Array to be converted.
100+
*
101+
* @throws \Exception
102+
*
103+
* @return \DOMNode
93104
*/
94-
private static function &convert($node_name, $arr=array()) {
105+
private static function &convert($node_name, $arr = array()) {
95106

96107
//print_arr($node_name);
97108
$xml = self::getXMLRoot();
98109
$node = $xml->createElement($node_name);
99110

100-
if(is_array($arr)){
111+
if (is_array($arr)) {
101112
// get the attributes first.;
102-
if(isset($arr['@attributes'])) {
103-
foreach($arr['@attributes'] as $key => $value) {
104-
if(!self::isValidTagName($key)) {
105-
throw new Exception('[Array2XML] Illegal character in attribute name. attribute: '.$key.' in node: '.$node_name);
113+
if (isset($arr['@attributes'])) {
114+
foreach ($arr['@attributes'] as $key => $value) {
115+
if (!self::isValidTagName($key)) {
116+
throw new Exception('[Array2XML] Illegal character in attribute name. attribute: ' . $key . ' in node: ' . $node_name);
106117
}
107118
$node->setAttribute($key, self::bool2str($value));
108119
}
@@ -111,31 +122,38 @@ private static function &convert($node_name, $arr=array()) {
111122

112123
// check if it has a value stored in @value, if yes store the value and return
113124
// else check if its directly stored as string
114-
if(isset($arr['@value'])) {
125+
if (isset($arr['@value'])) {
115126
$node->appendChild($xml->createTextNode(self::bool2str($arr['@value'])));
116127
unset($arr['@value']); //remove the key from the array once done.
117128
//return from recursion, as a note with value cannot have child nodes.
118129
return $node;
119-
} else if(isset($arr['@cdata'])) {
130+
} else if (isset($arr['@cdata'])) {
120131
$node->appendChild($xml->createCDATASection(self::bool2str($arr['@cdata'])));
121132
unset($arr['@cdata']); //remove the key from the array once done.
122133
//return from recursion, as a note with cdata cannot have child nodes.
123134
return $node;
124135
}
136+
else if (isset($arr['@xml'])) {
137+
$fragment = $xml->createDocumentFragment();
138+
$fragment->appendXML($arr['@xml']);
139+
$node->appendChild($fragment);
140+
unset($arr['@xml']);
141+
return $node;
142+
}
125143
}
126144

127145
//create subnodes using recursion
128-
if(is_array($arr)){
146+
if (is_array($arr)) {
129147
// recurse to get the node for that key
130-
foreach($arr as $key=>$value){
131-
if(!self::isValidTagName($key)) {
132-
throw new Exception('[Array2XML] Illegal character in tag name. tag: '.$key.' in node: '.$node_name);
148+
foreach ($arr as $key => $value) {
149+
if (!self::isValidTagName($key)) {
150+
throw new Exception('[Array2XML] Illegal character in tag name. tag: ' . $key . ' in node: ' . $node_name);
133151
}
134-
if(is_array($value) && is_numeric(key($value))) {
152+
if (is_array($value) && is_numeric(key($value))) {
135153
// MORE THAN ONE NODE OF ITS KIND;
136154
// if the new array is numeric index, means it is array of nodes of the same kind
137155
// it should follow the parent key name
138-
foreach($value as $k=>$v){
156+
foreach ($value as $k => $v) {
139157
$node->appendChild(self::convert($key, $v));
140158
}
141159
} else {
@@ -148,7 +166,7 @@ private static function &convert($node_name, $arr=array()) {
148166

149167
// after we are done with all the keys in the array (if it is one)
150168
// we check if it has any text value, if yes, append it.
151-
if(!is_array($arr)) {
169+
if (!is_array($arr)) {
152170
$node->appendChild($xml->createTextNode(self::bool2str($arr)));
153171
}
154172

@@ -158,8 +176,8 @@ private static function &convert($node_name, $arr=array()) {
158176
/*
159177
* Get the root XML node, if there isn't one, create it.
160178
*/
161-
private static function getXMLRoot(){
162-
if(empty(self::$xml)) {
179+
private static function getXMLRoot() {
180+
if (empty(self::$xml)) {
163181
self::init();
164182
}
165183
return self::$xml;
@@ -168,7 +186,7 @@ private static function getXMLRoot(){
168186
/*
169187
* Get string representation of boolean value
170188
*/
171-
private static function bool2str($v){
189+
private static function bool2str($v) {
172190
//convert boolean to text value.
173191
$v = $v === true ? 'true' : $v;
174192
$v = $v === false ? 'false' : $v;
@@ -179,7 +197,7 @@ private static function bool2str($v){
179197
* Check if the tag name or attribute name contains illegal characters
180198
* Ref: http://www.w3.org/TR/xml/#sec-common-syn
181199
*/
182-
private static function isValidTagName($tag){
200+
private static function isValidTagName($tag) {
183201
$pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i';
184202
return preg_match($pattern, $tag, $matches) && $matches[0] == $tag;
185203
}

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,38 @@ $array = XML2Array::createArray($xml);
1515
print_r($array);
1616
```
1717

18+
Array2XML
19+
----
20+
21+
@xml example:
22+
```php
23+
// Build the array that should be transformed into a XML object.
24+
$array = [
25+
'title' => 'A title',
26+
'body' => [
27+
'@xml' => '<html><body><p>The content for the news item</p></body></html>',
28+
],
29+
];
30+
31+
// Use the Array2XML object to transform it.
32+
$xml = Array2XML::createXML('news', $array);
33+
echo $xml->saveXML();
34+
```
35+
This will result in the following.
36+
```xml
37+
<?xml version="1.0" encoding="UTF-8"?>
38+
<news>
39+
<title>A title</title>
40+
<body>
41+
<html>
42+
<body>
43+
<p>The content for the news item</p>
44+
</body>
45+
</html>
46+
</body>
47+
</news>
48+
```
49+
1850
Reference
1951
----
2052
More complete references can be found here

0 commit comments

Comments
 (0)