Repository navigation
Expand file tree
/
Copy pathXml.php
More file actions
57 lines (50 loc) · 1.3 KB
/
Copy pathXml.php
File metadata and controls
57 lines (50 loc) · 1.3 KB
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
<?php
declare(strict_types=1);
namespace Codeception\Util;
use DOMDocument;
use DOMNode;
use function is_array;
class Xml
{
public static function arrayToXml(DOMDocument $xml, DOMNode $domNode, array $array = []): DOMDocument
{
foreach ($array as $el => $val) {
if (is_array($val)) {
self::arrayToXml($xml, $domNode->$el, $val);
} else {
$domNode->appendChild($xml->createElement($el, $val));
}
}
return $xml;
}
/**
* @param XmlBuilder|DOMDocument $xml
*/
public static function toXml($xml): DOMDocument
{
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(): XmlBuilder
{
return new XmlBuilder();
}
}