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