Passed
Pull Request — master (#12)
by Tim
05:32 queued 03:44
created

AbstractSerializableXML::__serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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