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
|
|
|
* @param array $validators An array of callbacks that may perform validations on the content |
25
|
|
|
*/ |
26
|
|
|
public function __construct(string $content, array $validators = []) |
27
|
|
|
{ |
28
|
|
|
$this->setContent($content, $validators); |
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
|
|
|
* @param array $validators An array of callbacks that may perform validations on the content |
37
|
|
|
*/ |
38
|
|
|
protected function setContent(string $content, $validators): void |
39
|
|
|
{ |
40
|
|
|
if (!empty($validators)) { |
41
|
|
|
foreach ($validators as $validator) { |
42
|
|
|
call_user_func($validator, $content); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->content = $content; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the content of the element. |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getContent(): string |
56
|
|
|
{ |
57
|
|
|
return $this->content; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Convert this element to XML. |
63
|
|
|
* |
64
|
|
|
* @param \DOMElement|null $parent The element we should append this element to. |
65
|
|
|
* @return \DOMElement |
66
|
|
|
*/ |
67
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
68
|
|
|
{ |
69
|
|
|
$e = $this->instantiateParentElement($parent); |
|
|
|
|
70
|
|
|
$e->textContent = $this->content; |
71
|
|
|
|
72
|
|
|
return $e; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|