1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XML; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\XML\Assert\Assert; |
9
|
|
|
use SimpleSAML\XML\Exception\{InvalidDOMElementException, InvalidValueTypeException}; |
10
|
|
|
use SimpleSAML\XML\Type\{QNameValue, StringValue, ValueTypeInterface}; |
11
|
|
|
|
12
|
|
|
use function defined; |
13
|
|
|
use function strval; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Trait for elements that hold a typed textContent value. |
17
|
|
|
* |
18
|
|
|
* @package simplesaml/xml-common |
19
|
|
|
*/ |
20
|
|
|
trait TypedTextContentTrait |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param \SimpleSAML\XML\Type\ValueTypeInterface $content |
24
|
|
|
*/ |
25
|
|
|
public function __construct( |
26
|
|
|
protected ValueTypeInterface $content, |
27
|
|
|
) { |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a class from XML |
33
|
|
|
* |
34
|
|
|
* @param \DOMElement $xml |
35
|
|
|
* @return static |
36
|
|
|
*/ |
37
|
|
|
public static function fromXML(DOMElement $xml): static |
38
|
|
|
{ |
39
|
|
|
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
40
|
|
|
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
|
|
|
|
41
|
|
|
|
42
|
|
|
$type = self::getTextContentType(); |
43
|
|
|
if ($type === QNameValue::class) { |
44
|
|
|
$qName = QNameValue::fromDocument($xml->textContent, $xml); |
45
|
|
|
$text = $qName->getRawValue(); |
46
|
|
|
} else { |
47
|
|
|
$text = $xml->textContent; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return new static($type::fromString($text)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the typed content of the element |
56
|
|
|
* |
57
|
|
|
* @return \SimpleSAML\XML\Type\ValueTypeInterface |
58
|
|
|
*/ |
59
|
|
|
public function getContent(): ValueTypeInterface |
60
|
|
|
{ |
61
|
|
|
return $this->content; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Create XML from this class |
67
|
|
|
* |
68
|
|
|
* @param \DOMElement|null $parent |
69
|
|
|
* @return \DOMElement |
70
|
|
|
*/ |
71
|
|
|
public function toXML(?DOMElement $parent = null): DOMElement |
72
|
|
|
{ |
73
|
|
|
$e = $this->instantiateParentElement($parent); |
|
|
|
|
74
|
|
|
|
75
|
|
|
if ($this->getTextContentType() === QNameValue::class) { |
76
|
|
|
if (!$e->lookupPrefix($this->getContent()->getNamespaceURI()->getValue())) { |
|
|
|
|
77
|
|
|
$e->setAttributeNS( |
78
|
|
|
'http://www.w3.org/2000/xmlns/', |
79
|
|
|
'xmlns:' . $this->getContent()->getNamespacePrefix()->getValue(), |
|
|
|
|
80
|
|
|
$this->getContent()->getNamespaceURI()->getValue(), |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$e->textContent = strval($this->getContent()); |
86
|
|
|
|
87
|
|
|
return $e; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the type the element's textContent value. |
93
|
|
|
* |
94
|
|
|
* @return class-string |
|
|
|
|
95
|
|
|
*/ |
96
|
|
|
public static function getTextContentType(): string |
97
|
|
|
{ |
98
|
|
|
if (defined('static::TEXTCONTENT_TYPE')) { |
99
|
|
|
$type = static::TEXTCONTENT_TYPE; |
|
|
|
|
100
|
|
|
} else { |
101
|
|
|
$type = StringValue::class; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
Assert::isAOf($type, ValueTypeInterface::class, InvalidValueTypeException::class); |
105
|
|
|
return $type; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|