1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\SOAP\XML; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\XML\Constants; |
10
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
11
|
|
|
use SimpleSAML\XML\Exception\SchemaViolationException; |
12
|
|
|
|
13
|
|
|
use function preg_split; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Trait grouping common functionality for simple elements with QName textContent |
17
|
|
|
* |
18
|
|
|
* @package simplesamlphp/xml-soap |
19
|
|
|
*/ |
20
|
|
|
trait QNameStringElementTrait |
21
|
|
|
{ |
22
|
|
|
/** @var string */ |
23
|
|
|
protected string $content; |
24
|
|
|
|
25
|
|
|
/** @var string|null */ |
26
|
|
|
protected ?string $namespaceUri; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Set the content of the element. |
31
|
|
|
* |
32
|
|
|
* @param string $content The value to go in the XML textContent |
33
|
|
|
*/ |
34
|
|
|
protected function setContent(string $content): void |
35
|
|
|
{ |
36
|
|
|
Assert::validQName($content, SchemaViolationException::class); |
37
|
|
|
$this->content = $content; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the content of the element. |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function getContent(): string |
47
|
|
|
{ |
48
|
|
|
return $this->content; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set the namespaceUri. |
54
|
|
|
* |
55
|
|
|
* @param string|null $namespaceUri |
56
|
|
|
*/ |
57
|
|
|
protected function setContentNamespaceUri(?string $namespaceUri): void |
58
|
|
|
{ |
59
|
|
|
Assert::nullOrValidURI($namespaceUri, SchemaViolationException::class); |
60
|
|
|
$this->namespaceUri = $namespaceUri; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the namespace URI. |
66
|
|
|
* |
67
|
|
|
* @return string|null |
68
|
|
|
*/ |
69
|
|
|
public function getContentNamespaceUri(): ?string |
70
|
|
|
{ |
71
|
|
|
return $this->namespaceUri; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Splits a QName into an array holding the prefix (or null if no prefix is available) and the localName |
77
|
|
|
* |
78
|
|
|
* @param string $qName The qualified name |
79
|
|
|
* @return string[] |
80
|
|
|
*/ |
81
|
|
|
private static function parseQName(string $qName): array |
82
|
|
|
{ |
83
|
|
|
Assert::validQName($qName); |
84
|
|
|
|
85
|
|
|
@list($prefix, $localName) = preg_split('/:/', $qName, 2); |
86
|
|
|
if ($localName === null) { |
87
|
|
|
$prefix = null; |
88
|
|
|
$localName = $qName; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
Assert::nullOrValidNCName($prefix); |
92
|
|
|
Assert::validNCName($localName); |
93
|
|
|
|
94
|
|
|
return [$prefix, $localName]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Convert XML into a class instance |
100
|
|
|
* |
101
|
|
|
* @param \DOMElement $xml The XML element we should load |
102
|
|
|
* @return static |
103
|
|
|
* |
104
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
105
|
|
|
* If the qualified name of the supplied element is wrong |
106
|
|
|
*/ |
107
|
|
|
public static function fromXML(DOMElement $xml): static |
108
|
|
|
{ |
109
|
|
|
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
110
|
|
|
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
|
|
|
|
111
|
|
|
|
112
|
|
|
list($prefix, $localName) = self::parseQName($xml->textContent); |
113
|
|
|
if ($localName === null) { |
|
|
|
|
114
|
|
|
// We don't have a prefixed value here; use target namespace |
115
|
|
|
$namespace = $xml->lookupNamespaceUri(null); |
116
|
|
|
} else { |
117
|
|
|
$namespace = $xml->lookupNamespaceUri($prefix); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return new static($xml->textContent, $namespace); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Convert this element to XML. |
126
|
|
|
* |
127
|
|
|
* @param \DOMElement|null $parent The element we should append this element to. |
128
|
|
|
* @return \DOMElement |
129
|
|
|
*/ |
130
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
131
|
|
|
{ |
132
|
|
|
$e = $this->instantiateParentElement($parent); |
133
|
|
|
|
134
|
|
|
list($prefix, $localName) = self::parseQName($this->content); |
135
|
|
|
if ($this->namespaceUri !== null && $prefix !== null) { |
136
|
|
|
if ($e->lookupNamespaceUri($this->namespaceUri) === null && $e->lookupPrefix($prefix) === null) { |
137
|
|
|
// The namespace is not yet available in the document - insert it |
138
|
|
|
$e->setAttribute('xmlns:' . $prefix, $this->namespaceUri); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$e->textContent = ($prefix === null) ? $localName : ($prefix . ':' . $localName); |
143
|
|
|
|
144
|
|
|
return $e; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** @return string */ |
149
|
|
|
abstract public static function getLocalName(): string; |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Create a document structure for this element |
154
|
|
|
* |
155
|
|
|
* @param \DOMElement|null $parent The element we should append to. |
156
|
|
|
* @return \DOMElement |
157
|
|
|
*/ |
158
|
|
|
abstract public function instantiateParentElement(DOMElement $parent = null): DOMElement; |
159
|
|
|
} |
160
|
|
|
|