Passed
Push — master ( fb0d53...7c0351 )
by Jaime Pérez
03:11
created

Chunk::getXml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SAML2\XML;
4
5
use SAML2\DOMDocumentFactory;
6
use SAML2\Utils;
7
8
/**
9
 * Serializable class used to hold an XML element.
10
 *
11
 * @package SimpleSAMLphp
12
 */
13
class Chunk implements \Serializable
14
{
15
    /**
16
     * The localName of the element.
17
     *
18
     * @var string
19
     */
20
    public $localName;
21
22
    /**
23
     * The namespaceURI of this element.
24
     *
25
     * @var string|null
26
     */
27
    public $namespaceURI;
28
29
    /**
30
     * The \DOMElement we contain.
31
     *
32
     * @var \DOMElement
33
     */
34
    public $xml;
35
36
37
    /**
38
     * Create a XMLChunk from a copy of the given \DOMElement.
39
     *
40
     * @param \DOMElement $xml The element we should copy.
41
     */
42
    public function __construct(\DOMElement $xml)
43
    {
44
        $this->setLocalName($xml->localName);
45
        $this->setNamespaceURI($xml->namespaceURI);
46
47
        $this->xml = Utils::copyElement($xml);
48
    }
49
50
51
    /**
52
     * Append this XML element to a different XML element.
53
     *
54
     * @param  \DOMElement $parent The element we should append this element to.
55
     * @return \DOMElement The new element.
56
     */
57
    public function toXML(\DOMElement $parent)
58
    {
59
        return Utils::copyElement($this->xml, $parent);
60
    }
61
62
63
    /**
64
     * Collect the value of the localName-property
65
     * @return string
66
     */
67
    public function getLocalName()
68
    {
69
        return $this->localName;
70
    }
71
72
73
    /**
74
     * Set the value of the localName-property
75
     * @param string $localName
76
     * @return void
77
     */
78
    public function setLocalName($localName)
79
    {
80
        assert(is_string($localName));
81
        $this->localName = $localName;
82
    }
83
84
85
    /**
86
     * Collect the value of the namespaceURI-property
87
     * @return string|null
88
     */
89
    public function getNamespaceURI()
90
    {
91
        return $this->namespaceURI;
92
    }
93
94
95
    /**
96
     * Set the value of the namespaceURI-property
97
     * @param string|null $namespaceURI
98
     * @return void
99
     */
100
    public function setNamespaceURI($namespaceURI = null)
101
    {
102
        assert(is_string($namespaceURI) || is_null($namespaceURI));
103
        $this->namespaceURI = $namespaceURI;
104
    }
105
106
107
    /**
108
     * Get this \DOMElement.
109
     *
110
     * @return \DOMElement This element.
111
     */
112
    public function getXml()
113
    {
114
        return $this->xml;
115
    }
116
117
    /**
118
     * Set the value of the xml-property
119
     * @param \DOMelement $xml
120
     * @return void
121
     */
122
    private function setXml(\DOMelement $xml)
0 ignored issues
show
Unused Code introduced by
The method setXml() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
123
    {
124
        $this->xml = $xml;
125
    }
126
127
128
    /**
129
     * Serialize this XML chunk.
130
     *
131
     * @return string The serialized chunk.
132
     */
133
    public function serialize()
134
    {
135
        return serialize($this->xml->ownerDocument->saveXML($this->xml));
136
    }
137
138
139
    /**
140
     * Un-serialize this XML chunk.
141
     *
142
     * @param  string          $serialized The serialized chunk.
143
     * @return void
144
     */
145
    public function unserialize($serialized)
146
    {
147
        $doc = DOMDocumentFactory::fromString(unserialize($serialized));
148
        $this->xml = $doc->documentElement;
149
        $this->setLocalName($this->xml->localName);
150
        $this->setNamespaceURI($this->xml->namespaceURI);
151
    }
152
}
153