1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XML; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use SimpleSAML\XML\DOMDocumentFactory; |
10
|
|
|
|
11
|
|
|
use function array_pop; |
12
|
|
|
use function get_object_vars; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait grouping common functionality for elements implementing the SerializableElement element. |
16
|
|
|
* |
17
|
|
|
* @package simplesamlphp/xml-common |
18
|
|
|
*/ |
19
|
|
|
trait SerializableElementTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Whether to format the string output of this element or not. |
23
|
|
|
* |
24
|
|
|
* Defaults to true. Override to disable output formatting. |
25
|
|
|
* |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
protected bool $formatOutput = true; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Output the class as an XML-formatted string |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function __toString(): string |
37
|
|
|
{ |
38
|
|
|
$xml = $this->toXML(); |
39
|
|
|
/** @psalm-var \DOMDocument $xml->ownerDocument */ |
40
|
|
|
$xml->ownerDocument->formatOutput = $this->formatOutput; |
41
|
|
|
|
42
|
|
|
return $xml->ownerDocument->saveXML($xml); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Serialize this XML chunk. |
48
|
|
|
* |
49
|
|
|
* This method will be invoked by any calls to serialize(). |
50
|
|
|
* |
51
|
|
|
* @return array The serialized representation of this XML object. |
52
|
|
|
*/ |
53
|
|
|
public function __serialize(): array |
54
|
|
|
{ |
55
|
|
|
$xml = $this->toXML(); |
56
|
|
|
/** @psalm-var \DOMDocument $xml->ownerDocument */ |
57
|
|
|
return [$xml->ownerDocument->saveXML($xml)]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Unserialize an XML object and load it.. |
63
|
|
|
* |
64
|
|
|
* This method will be invoked by any calls to unserialize(), allowing us to restore any data that might not |
65
|
|
|
* be serializable in its original form (e.g.: DOM objects). |
66
|
|
|
* |
67
|
|
|
* @param array $serialized The XML object that we want to restore. |
68
|
|
|
*/ |
69
|
|
|
public function __unserialize(array $serialized): void |
70
|
|
|
{ |
71
|
|
|
$xml = static::fromXML( |
72
|
|
|
DOMDocumentFactory::fromString(array_pop($serialized))->documentElement, |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$vars = get_object_vars($xml); |
76
|
|
|
foreach ($vars as $k => $v) { |
77
|
|
|
$this->$k = $v; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Create a class from an array |
84
|
|
|
* |
85
|
|
|
* @param array $data |
86
|
|
|
* @return static |
87
|
|
|
*/ |
88
|
|
|
public static function fromArray(/** @scrutinizer ignore-unused */array $data): static |
89
|
|
|
{ |
90
|
|
|
throw new RuntimeException('Not implemented.'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Create an array from this class |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public function toArray(): array |
100
|
|
|
{ |
101
|
|
|
throw new RuntimeException('Not implemented'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Create XML from this class |
107
|
|
|
* |
108
|
|
|
* @param \DOMElement|null $parent |
109
|
|
|
* @return \DOMElement |
110
|
|
|
*/ |
111
|
|
|
abstract public function toXML(DOMElement $parent = null): DOMElement; |
112
|
|
|
} |
113
|
|
|
|