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
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait grouping common functionality for simple elements with just some textContent |
13
|
|
|
* |
14
|
|
|
* @package simplesamlphp/xml-common |
15
|
|
|
*/ |
16
|
|
|
trait XMLStringElementTrait |
17
|
|
|
{ |
18
|
|
|
/** @var string */ |
19
|
|
|
protected string $content; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $content |
24
|
|
|
*/ |
25
|
|
|
public function __construct(string $content) |
26
|
|
|
{ |
27
|
|
|
$this->setContent($content); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Set the content of the element. |
33
|
|
|
* |
34
|
|
|
* @param string $content The value to go in the XML textContent |
35
|
|
|
*/ |
36
|
|
|
protected function setContent(string $content): void |
37
|
|
|
{ |
38
|
|
|
$this->validateContent($content); |
39
|
|
|
$this->content = $content; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the content of the element. |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function getContent(): string |
49
|
|
|
{ |
50
|
|
|
return $this->content; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Validate the content of the element. |
56
|
|
|
* |
57
|
|
|
* @param string $content The value to go in the XML textContent |
58
|
|
|
* @throws \Exception on failure |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
private function validateContent(string $content): void |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
/** |
64
|
|
|
* Perform no validation by default. |
65
|
|
|
* Override this method on the implementing class to perform content validation. |
66
|
|
|
*/ |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Convert this element to XML. |
72
|
|
|
* |
73
|
|
|
* @param \DOMElement|null $parent The element we should append this element to. |
74
|
|
|
* @return \DOMElement |
75
|
|
|
*/ |
76
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
77
|
|
|
{ |
78
|
|
|
$e = $this->instantiateParentElement($parent); |
|
|
|
|
79
|
|
|
$e->textContent = $this->content; |
80
|
|
|
|
81
|
|
|
return $e; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.