| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: Xml.php 1080 2007-02-10 18:17:08Z jwage $
|
|
|
4 |
*
|
|
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
16 |
*
|
|
|
17 |
* This software consists of voluntary contributions made by many individuals
|
|
|
18 |
* and is licensed under the LGPL. For more information, see
|
|
|
19 |
* <http://www.doctrine-project.org>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Doctrine_Parser_Xml
|
|
|
24 |
*
|
|
|
25 |
* @package Doctrine
|
|
|
26 |
* @subpackage Parser
|
|
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
28 |
* @link www.doctrine-project.org
|
|
|
29 |
* @since 1.0
|
|
|
30 |
* @version $Revision: 1080 $
|
|
|
31 |
* @author Jonathan H. Wage <jwage@mac.com>
|
|
|
32 |
*/
|
|
|
33 |
class Doctrine_Parser_Xml extends Doctrine_Parser
|
|
|
34 |
{
|
|
|
35 |
/**
|
|
|
36 |
* dumpData
|
|
|
37 |
*
|
|
|
38 |
* Convert array to xml and dump to specified path or return the xml
|
|
|
39 |
*
|
|
|
40 |
* @param string $array Array of data to convert to xml
|
|
|
41 |
* @param string $path Path to write xml data to
|
|
|
42 |
* @param string $charset The charset of the data being dumped
|
|
|
43 |
* @return string $xml
|
|
|
44 |
* @return void
|
|
|
45 |
*/
|
|
|
46 |
public function dumpData($array, $path = null, $charset = null)
|
|
|
47 |
{
|
|
|
48 |
$data = self::arrayToXml($array, 'data', null, $charset);
|
|
|
49 |
|
|
|
50 |
return $this->doDump($data, $path);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* arrayToXml
|
|
|
55 |
*
|
|
|
56 |
* @param string $array Array to convert to xml
|
|
|
57 |
* @param string $rootNodeName Name of the root node
|
|
|
58 |
* @param string $xml SimpleXmlElement
|
|
|
59 |
* @return string $asXml String of xml built from array
|
|
|
60 |
*/
|
|
|
61 |
public static function arrayToXml($array, $rootNodeName = 'data', $xml = null, $charset = null)
|
|
|
62 |
{
|
|
|
63 |
if ($xml === null) {
|
|
|
64 |
$xml = new SimpleXmlElement("<?xml version=\"1.0\" encoding=\"utf-8\"?><$rootNodeName/>");
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
foreach($array as $key => $value)
|
|
|
68 |
{
|
|
|
69 |
$key = preg_replace('/[^a-z]/i', '', $key);
|
|
|
70 |
|
|
|
71 |
if (is_array($value) && ! empty($value)) {
|
|
|
72 |
$node = $xml->addChild($key);
|
|
|
73 |
|
|
|
74 |
foreach ($value as $k => $v) {
|
|
|
75 |
if (is_numeric($v)) {
|
|
|
76 |
unset($value[$k]);
|
|
|
77 |
$node->addAttribute($k, $v);
|
|
|
78 |
}
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
self::arrayToXml($value, $rootNodeName, $node, $charset);
|
|
|
82 |
} else if (is_int($key)) {
|
|
|
83 |
$xml->addChild($value, 'true');
|
|
|
84 |
} else {
|
|
|
85 |
$charset = $charset ? $charset : 'utf-8';
|
|
|
86 |
if (strcasecmp($charset, 'utf-8') !== 0 && strcasecmp($charset, 'utf8') !== 0) {
|
|
|
87 |
$value = iconv($charset, 'UTF-8', $value);
|
|
|
88 |
}
|
|
|
89 |
$value = htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
|
|
|
90 |
$xml->addChild($key, $value);
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
return $xml->asXML();
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* loadData
|
|
|
99 |
*
|
|
|
100 |
* Load xml file and return array of data
|
|
|
101 |
*
|
|
|
102 |
* @param string $path Path to load xml data from
|
|
|
103 |
* @return array $array Array of data converted from xml
|
|
|
104 |
*/
|
|
|
105 |
public function loadData($path)
|
|
|
106 |
{
|
|
|
107 |
$contents = $this->doLoad($path);
|
|
|
108 |
|
|
|
109 |
$simpleXml = simplexml_load_string($contents);
|
|
|
110 |
|
|
|
111 |
return $this->prepareData($simpleXml);
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* prepareData
|
|
|
116 |
*
|
|
|
117 |
* Prepare simple xml to array for return
|
|
|
118 |
*
|
|
|
119 |
* @param string $simpleXml
|
|
|
120 |
* @return array $return
|
|
|
121 |
*/
|
|
|
122 |
public function prepareData($simpleXml)
|
|
|
123 |
{
|
|
|
124 |
if ($simpleXml instanceof SimpleXMLElement) {
|
|
|
125 |
$children = $simpleXml->children();
|
|
|
126 |
$return = null;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
foreach ($children as $element => $value) {
|
|
|
130 |
if ($value instanceof SimpleXMLElement) {
|
|
|
131 |
$values = (array) $value->children();
|
|
|
132 |
|
|
|
133 |
if (count($values) > 0) {
|
|
|
134 |
$return[$element] = $this->prepareData($value);
|
|
|
135 |
} else {
|
|
|
136 |
if ( ! isset($return[$element])) {
|
|
|
137 |
$return[$element] = (string) $value;
|
|
|
138 |
} else {
|
|
|
139 |
if ( ! is_array($return[$element])) {
|
|
|
140 |
$return[$element] = array($return[$element], (string) $value);
|
|
|
141 |
} else {
|
|
|
142 |
$return[$element][] = (string) $value;
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
if (is_array($return)) {
|
|
|
150 |
return $return;
|
|
|
151 |
} else {
|
|
|
152 |
return array();
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
}
|