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