Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * @author Omer Hassan
4
 * @author Ryan Parman
5
 * @license MIT
6
 */
7
class Array2DOM
8
{
9
	const ATTRIBUTES = '__attributes__';
10
	const CONTENT = '__content__';
11
 
12
	/**
13
	 * @param array $source
14
	 * @param string $rootTagName
15
	 * @return DOMDocument
16
	 */
17
	public static function arrayToDOMDocument(array $source, $rootTagName = 'root')
18
	{
19
		$document = new DOMDocument();
20
		$document->appendChild(self::createDOMElement($source, $rootTagName, $document));
21
 
22
		return $document;
23
	}
24
 
25
	/**
26
	 * @param array $source
27
	 * @param string $rootTagName
28
	 * @param bool $formatOutput
29
	 * @return string
30
	 */
31
	public static function arrayToXMLString(array $source, $rootTagName = 'root', $formatOutput = true)
32
	{
33
		$document = self::arrayToDOMDocument($source, $rootTagName);
34
		$document->formatOutput = $formatOutput;
35
 
36
		return $document->saveXML();
37
	}
38
 
39
	/**
40
	 * @param DOMDocument $document
41
	 * @return array
42
	 */
43
	public static function domDocumentToArray(DOMDocument $document)
44
	{
45
		return self::createArray($document->documentElement);
46
	}
47
 
48
	/**
49
	 * @param string $xmlString
50
	 * @return array
51
	 */
52
	public static function xmlStringToArray($xmlString)
53
	{
54
		$document = new DOMDocument();
55
 
56
		return $document->loadXML($xmlString) ? self::domDocumentToArray($document) : array();
57
	}
58
 
59
	/**
60
	 * @param mixed $source
61
	 * @param string $tagName
62
	 * @param DOMDocument $document
63
	 * @return DOMNode
64
	 */
65
	private static function createDOMElement($source, $tagName, DOMDocument $document)
66
	{
67
		if (!is_array($source))
68
		{
69
			$element = $document->createElement($tagName);
70
			$element->appendChild($document->createCDATASection($source));
71
 
72
			return $element;
73
		}
74
 
75
		$element = $document->createElement($tagName);
76
 
77
		foreach ($source as $key => $value)
78
		{
79
			if (is_string($key) && !is_numeric($key))
80
			{
81
				if ($key == self::ATTRIBUTES)
82
				{
83
					foreach ($value as $attributeName => $attributeValue)
84
					{
85
						 $element->setAttribute($attributeName, $attributeValue);
86
					}
87
				}
88
				elseif ($key === self::CONTENT)
89
				{
90
					$element->appendChild($document->createCDATASection($value));
91
				}
92
				elseif (is_string($value) && !is_numeric($value))
93
				{
94
					$element->appendChild(self::createDOMElement($value, $key, $document));
95
				}
96
				elseif (is_array($value) && count($value))
97
				{
98
					$keyNode = $document->createElement($key);
99
 
100
					foreach ($value as $elementKey => $elementValue)
101
					{
102
						if (is_string($elementKey) && !is_numeric($elementKey))
103
						{
104
							$keyNode->appendChild(self::createDOMElement($elementValue, $elementKey, $document));
105
						}
106
						else
107
						{
108
							$element->appendChild(self::createDOMElement($elementValue, $key, $document));
109
						}
110
					}
111
 
112
					if ($keyNode->hasChildNodes())
113
					{
114
						$element->appendChild($keyNode);
115
					}
116
				}
117
				else
118
				{
119
					if (is_bool($value))
120
					{
121
						$value = $value ? 'true' : 'false';
122
					}
123
 
124
					$element->appendChild(self::createDOMElement((string) $value, $key, $document));
125
				}
126
			}
127
			else
128
			{
129
				$element->appendChild(self::createDOMElement($value, $tagName, $document));
130
			}
131
		}
132
 
133
		return $element;
134
	}
135
 
136
	/**
137
	 * @param DOMNode $domNode
138
	 * @return array
139
	 */
140
	private static function createArray(DOMNode $domNode)
141
	{
142
		$array = array();
143
 
144
		for ($i = 0; $i < $domNode->childNodes->length; $i++)
145
		{
146
			$item = $domNode->childNodes->item($i);
147
 
148
			if ($item->nodeType == XML_ELEMENT_NODE)
149
			{
150
				$arrayElement = array();
151
 
152
				for ($attributeIndex = 0; !is_null($attribute = $item->attributes->item($attributeIndex)); $attributeIndex++)
153
				{
154
					if ($attribute->nodeType == XML_ATTRIBUTE_NODE)
155
					{
156
						$arrayElement[self::ATTRIBUTES][$attribute->nodeName] = $attribute->nodeValue;
157
					}
158
				}
159
 
160
				$children = self::createArray($item);
161
 
162
				if (is_array($children))
163
				{
164
					$arrayElement = array_merge($arrayElement, $children);
165
				}
166
				else
167
				{
168
					$arrayElement[self::CONTENT] = $children;
169
				}
170
 
171
				$array[$item->nodeName][] = $arrayElement;
172
			}
173
			elseif ($item->nodeType == XML_CDATA_SECTION_NODE || ($item->nodeType == XML_TEXT_NODE && trim($item->nodeValue) != ''))
174
			{
175
				return $item->nodeValue;
176
			}
177
		}
178
 
179
		return $array;
180
	}
181
}