summaryrefslogtreecommitdiffstats
path: root/lib/SAML2/XML/Chunk.php
blob: 9f614d3dc180547ddcb2f0b7b00fd71115336fce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php

/**
 * Serializable class used to hold an XML element.
 *
 * @package simpleSAMLphp
 * @version $Id$
 */
class SAML2_XML_Chunk {

	/**
	 * The localName of the element.
	 *
	 * @var string
	 */
	public $localName;


	/**
	 * The namespaceURI of this element.
	 *
	 * @var string
	 */
	public $namespaceURI;


	/**
	 * The DOMElement we contain.
	 *
	 * @var DOMElement
	 */
	private $xml;


	/**
	 * The DOMElement as a text string. Used during serialization.
	 *
	 * @var string|NULL
	 */
	private $xmlString;


	/**
	 * Create a XMLChunk from a copy of the given DOMElement.
	 *
	 * @param DOMElement $xml  The element we should copy.
	 */
	public function __construct(DOMElement $xml) {

		$this->localName = $xml->localName;
		$this->namespaceURI = $xml->namespaceURI;

		$this->xml = SAML2_Utils::copyElement($xml);
	}


	/**
	 * Get this DOMElement.
	 *
	 * @return DOMElement  This element.
	 */
	public function getXML() {
		assert('$this->xml instanceof DOMElement || is_string($this->xmlString)');

		if ($this->xml === NULL) {
			$doc = new DOMDocument();
			$doc->loadXML($this->xmlString);
			$this->xml = $doc->firstChild;
		}

		return $this->xml;
	}


	/**
	 * Append this XML element to a different XML element.
	 *
	 * @param DOMElement $parent  The element we should append this element to.
	 * @return DOMElement  The new element.
	 */
	public function toXML(DOMElement $parent) {

		return SAML2_Utils::copyElement($this->getXML(), $parent);
	}


	/**
	 * Serialization handler.
	 *
	 * Converts the XML data to a string that can be serialized
	 *
	 * @return array  List of properties that should be serialized.
	 */
	public function __sleep() {
		assert('$this->xml instanceof DOMElement || is_string($this->xmlString)');

		if ($this->xmlString === NULL) {
			$this->xmlString = $this->xml->ownerDocument->saveXML($this->xml);
		}

		return array('xmlString', 'localName', 'namespaceURI');
	}

}