1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Ivory\Value; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Encapsulation of some XML content, not necessarily an XML document. |
7
|
|
|
* |
8
|
|
|
* The objects are immutable. |
9
|
|
|
*/ |
10
|
|
|
class XmlContent |
11
|
|
|
{ |
12
|
|
|
private $xmlStr; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Creates a new XML content value. If the value forms a document, it creates an {@link XmlDocument} object. |
16
|
|
|
* |
17
|
|
|
* If an object of an unrecognized class is given, its string serialization is used as the XML string. |
18
|
|
|
* |
19
|
|
|
* @param string|XmlContent|\DOMDocument|\DOMNode|\DOMNodeList|\SimpleXMLElement|object $value |
20
|
|
|
* @return XmlContent |
21
|
|
|
*/ |
22
|
|
|
public static function fromValue($value): XmlContent |
23
|
|
|
{ |
24
|
|
|
if (is_string($value)) { |
25
|
|
|
$xmlStr = $value; |
26
|
|
|
$isDoc = self::isXmlDocument($value); |
27
|
|
|
} elseif ($value instanceof XmlContent) { |
28
|
|
|
return $value; |
29
|
|
|
} elseif ($value instanceof \DOMDocument) { |
30
|
|
|
$xmlStr = $value->saveXML(); |
31
|
|
|
if ($xmlStr === false) { |
32
|
|
|
throw new \InvalidArgumentException('value'); |
33
|
|
|
} |
34
|
|
|
$isDoc = true; |
35
|
|
|
} elseif ($value instanceof \DOMNode) { |
36
|
|
|
$d = $value->ownerDocument->saveXML($value); |
|
|
|
|
37
|
|
|
if ($d === false) { |
38
|
|
|
throw new \InvalidArgumentException('value'); |
39
|
|
|
} |
40
|
|
|
$xmlStr = self::getXMLDeclaration($value->ownerDocument) . $d; |
|
|
|
|
41
|
|
|
$isDoc = true; |
42
|
|
|
} elseif ($value instanceof \DOMNodeList) { |
43
|
|
|
$xmlStr = ($value->length > 0 ? self::getXMLDeclaration($value->item(0)->ownerDocument) : ''); |
44
|
|
|
foreach ($value as $i => $node) { |
45
|
|
|
$n = $node->ownerDocument->saveXML($node); |
46
|
|
|
if ($n === false) { |
47
|
|
|
throw new \InvalidArgumentException("value, node $i"); |
48
|
|
|
} |
49
|
|
|
$xmlStr .= $n; |
50
|
|
|
} |
51
|
|
|
$isDoc = ($value->length == 1); |
52
|
|
|
} elseif ($value instanceof \SimpleXMLElement) { |
53
|
|
|
$xmlStr = $value->saveXML(); |
54
|
|
|
if ($xmlStr === false) { |
55
|
|
|
throw new \InvalidArgumentException('value'); |
56
|
|
|
} |
57
|
|
|
$isDoc = true; |
58
|
|
|
} elseif (is_object($value)) { |
59
|
|
|
$xmlStr = (string)$value; |
60
|
|
|
$isDoc = self::isXmlDocument($xmlStr); |
61
|
|
|
} else { |
62
|
|
|
throw new \InvalidArgumentException('value'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($isDoc) { |
66
|
|
|
return new XmlDocument($xmlStr); |
|
|
|
|
67
|
|
|
} else { |
68
|
|
|
return new XmlContent($xmlStr); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private static function isXmlDocument(string $xmlStr): bool |
73
|
|
|
{ |
74
|
|
|
$reader = new \XMLReader(); |
75
|
|
|
/** @noinspection PhpStaticAsDynamicMethodCallInspection it is also a non-static method */ |
76
|
|
|
$reader->XML($xmlStr); |
77
|
|
|
|
78
|
|
|
if (!@$reader->read()) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
do { |
83
|
|
|
$read = @$reader->read(); |
84
|
|
|
} while ($read); |
85
|
|
|
|
86
|
|
|
return ($reader->depth == 0); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private static function getXMLDeclaration(\DOMDocument $doc): string |
90
|
|
|
{ |
91
|
|
|
$out = '<?xml version="' . $doc->xmlVersion . '"'; |
92
|
|
|
if (strlen($doc->xmlEncoding) > 0) { |
93
|
|
|
$out .= ' encoding="' . $doc->xmlEncoding . '"'; |
94
|
|
|
} |
95
|
|
|
if ($doc->xmlStandalone) { |
96
|
|
|
$out .= ' standalone="yes"'; |
97
|
|
|
} |
98
|
|
|
$out .= '?>'; |
99
|
|
|
|
100
|
|
|
return $out; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $xmlStr |
105
|
|
|
*/ |
106
|
|
|
private function __construct(string $xmlStr) |
107
|
|
|
{ |
108
|
|
|
$this->xmlStr = $xmlStr; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string the XML value as a string |
113
|
|
|
*/ |
114
|
|
|
public function toString(): string |
115
|
|
|
{ |
116
|
|
|
return $this->xmlStr; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.