Skip to content

Commit dd32363

Browse files
committed
add utility class
1 parent 1fa8e37 commit dd32363

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Utils.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace WsdlToPhp\PackageBase;
4+
5+
class Utils
6+
{
7+
/**
8+
* Returns a XML string content as a DOMDocument or as a formated XML string
9+
* @uses \DOMDocument::loadXML()
10+
* @uses \DOMDocument::saveXML()
11+
* @param string $string
12+
* @param bool $asDomDocument
13+
* @return \DOMDocument|string|null
14+
*/
15+
public static function getFormatedXml($string, $asDomDocument = false)
16+
{
17+
if (!empty($string) && class_exists('DOMDocument')) {
18+
$dom = new \DOMDocument('1.0', 'UTF-8');
19+
$dom->formatOutput = true;
20+
$dom->preserveWhiteSpace = false;
21+
$dom->resolveExternals = false;
22+
$dom->substituteEntities = false;
23+
$dom->validateOnParse = false;
24+
try {
25+
if ($dom->loadXML($string)) {
26+
return $asDomDocument ? $dom : $dom->saveXML();
27+
}
28+
} catch (\Exception $exception) {
29+
throw new \InvalidArgumentException(sprintf('XML string is invalid'), null, $exception);
30+
}
31+
}
32+
return $asDomDocument ? null : $string;
33+
}
34+
}

0 commit comments

Comments
 (0)