|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\XML; |
|
6
|
|
|
|
|
7
|
|
|
use DOMElement; |
|
8
|
|
|
use SimpleSAML\Assert\Assert; |
|
9
|
|
|
use SimpleSAML\XML\Constants; |
|
10
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Trait grouping common functionality for simple elements with just some textContent |
|
14
|
|
|
* |
|
15
|
|
|
* @package simplesamlphp/xml-common |
|
16
|
|
|
*/ |
|
17
|
|
|
trait StringElementTrait |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected string $content; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Set the content of the element. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $content The value to go in the XML textContent |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function setContent(string $content): void |
|
29
|
|
|
{ |
|
30
|
|
|
$this->validateContent($content); |
|
31
|
|
|
$this->content = $content; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Get the content of the element. |
|
37
|
|
|
* |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getContent(): string |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->content; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Validate the content of the element. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $content The value to go in the XML textContent |
|
50
|
|
|
* @throws \Exception on failure |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function validateContent(/** @scrutinizer ignore-unused */ string $content): void |
|
54
|
|
|
{ |
|
55
|
|
|
/** |
|
56
|
|
|
* Perform no validation by default. |
|
57
|
|
|
* Override this method on the implementing class to perform content validation. |
|
58
|
|
|
*/ |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Convert XML into a class instance |
|
64
|
|
|
* |
|
65
|
|
|
* @param \DOMElement $xml The XML element we should load |
|
66
|
|
|
* @return self |
|
67
|
|
|
* |
|
68
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
|
69
|
|
|
* If the qualified name of the supplied element is wrong |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function fromXML(DOMElement $xml): object |
|
72
|
|
|
{ |
|
73
|
|
|
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
|
74
|
|
|
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
return new static($xml->textContent); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Convert this element to XML. |
|
82
|
|
|
* |
|
83
|
|
|
* @param \DOMElement|null $parent The element we should append this element to. |
|
84
|
|
|
* @return \DOMElement |
|
85
|
|
|
*/ |
|
86
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
|
87
|
|
|
{ |
|
88
|
|
|
$e = $this->instantiateParentElement($parent); |
|
89
|
|
|
$e->textContent = $this->getContent(); |
|
90
|
|
|
|
|
91
|
|
|
return $e; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
/** @return string */ |
|
96
|
|
|
abstract public static function getLocalName(): string; |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Create a document structure for this element |
|
101
|
|
|
* |
|
102
|
|
|
* @param \DOMElement|null $parent The element we should append to. |
|
103
|
|
|
* @return \DOMElement |
|
104
|
|
|
*/ |
|
105
|
|
|
abstract public function instantiateParentElement(DOMElement $parent = null): DOMElement; |
|
106
|
|
|
} |
|
107
|
|
|
|