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
<?php
namespace Codeception\Util;
class Xml
{
/**
* @static
*
* @param \DOMDocument $xml
* @param \DOMNode $node
* @param array $array
*
* @return \DOMDocument
*/
public static function arrayToXml(\DOMDocument $xml, \DOMNode $node, $array = [])
{
foreach ($array as $el => $val) {
if (is_array($val)) {
self::arrayToXml($xml, $node->$el, $val);
} else {
$node->appendChild($xml->createElement($el, $val));
}
}
return $xml;
}
/**
* @static
*
* @param $xml
*
* @return \DOMDocument|\DOMNode
*/
public static function toXml($xml)
{
if ($xml instanceof XmlBuilder) {
return $xml->getDom();
}
if ($xml instanceof \DOMDocument) {
return $xml;
}
$dom = new \DOMDocument();
$dom->preserveWhiteSpace = false;
if ($xml instanceof \DOMNode) {
$xml = $dom->importNode($xml, true);
$dom->appendChild($xml);
return $dom;
}
if (is_array($xml)) {
return self::arrayToXml($dom, $dom, $xml);
}
if (!empty($xml)) {
$dom->loadXML($xml);
}
return $dom;
}
public static function build()
{
return new XmlBuilder();
}
}