Passed
Push — master ( 231891...d28a5b )
by Tim
01:32
created

SerializableElementTrait::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XML;
6
7
use DOMElement;
8
use RuntimeException;
9
use SimpleSAML\Assert\Assert;
10
use SimpleSAML\XML\DOMDocumentFactory;
11
12
use function array_pop;
13
use function get_object_vars;
14
15
/**
16
 * Trait grouping common functionality for elements implementing the SerializableElement element.
17
 *
18
 * @package simplesamlphp/xml-common
19
 */
20
trait SerializableElementTrait
21
{
22
    /**
23
     * Whether to format the string output of this element or not.
24
     *
25
     * Defaults to true. Override to disable output formatting.
26
     *
27
     * @var bool
28
     */
29
    protected bool $formatOutput = true;
30
31
32
    /**
33
     * Output the class as an XML-formatted string
34
     *
35
     * @return string
36
     */
37
    public function __toString(): string
38
    {
39
        $xml = $this->toXML();
40
41
        /** @psalm-var \DOMDocument $xml->ownerDocument */
42
        $xmlString = $xml->ownerDocument->saveXML();
0 ignored issues
show
Bug introduced by
The method saveXML() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        /** @scrutinizer ignore-call */ 
43
        $xmlString = $xml->ownerDocument->saveXML();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
44
        /** @psalm-var non-empty-string $xmlString */
45
        $doc = DOMDocumentFactory::fromString($xmlString);
46
        $doc->formatOutput = $this->formatOutput;
47
48
        return $doc->saveXML($doc->firstChild);
49
    }
50
51
52
    /**
53
     * Serialize this XML chunk.
54
     *
55
     * This method will be invoked by any calls to serialize().
56
     *
57
     * @return array The serialized representation of this XML object.
58
     */
59
    public function __serialize(): array
60
    {
61
        $xml = $this->toXML();
62
        /** @psalm-var \DOMDocument $xml->ownerDocument */
63
        return [$xml->ownerDocument->saveXML($xml)];
64
    }
65
66
67
    /**
68
     * Unserialize an XML object and load it..
69
     *
70
     * This method will be invoked by any calls to unserialize(), allowing us to restore any data that might not
71
     * be serializable in its original form (e.g.: DOM objects).
72
     *
73
     * @param array $serialized The XML object that we want to restore.
74
     */
75
    public function __unserialize(array $serialized): void
76
    {
77
        $xml = static::fromXML(
78
            DOMDocumentFactory::fromString(array_pop($serialized))->documentElement,
79
        );
80
81
        $vars = get_object_vars($xml);
82
        foreach ($vars as $k => $v) {
83
            $this->$k = $v;
84
        }
85
    }
86
87
88
    /**
89
     * Create XML from this class
90
     *
91
     * @param \DOMElement|null $parent
92
     * @return \DOMElement
93
     */
94
    abstract public function toXML(DOMElement $parent = null): DOMElement;
95
}
96