|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SAML2\XML; |
| 6 | + |
| 7 | +use DOMElement; |
| 8 | +use SimpleSAML\XML\Assert\Assert; |
| 9 | +use SimpleSAML\XML\DOMDocumentFactory; |
| 10 | +use SimpleSAML\XML\SerializableElementTrait; |
| 11 | +use SimpleSAML\XMLSchema\Exception\MissingAttributeException; |
| 12 | +use SimpleSAML\XMLSchema\Exception\SchemaViolationException; |
| 13 | +use SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface; |
| 14 | +use SimpleSAML\XMLSchema\Type\StringValue; |
| 15 | + |
| 16 | +/** |
| 17 | + * Serializable class used to hold an XML element. |
| 18 | + * |
| 19 | + * @package simplesamlphp/xml-common |
| 20 | + * @deprecated Use \SimpleSAML\XML\Chunk instead (simplesamlphp/xml-common) |
| 21 | + */ |
| 22 | +final class Chunk implements |
| 23 | + SerializableElementInterface, |
| 24 | + ElementInterface |
| 25 | +{ |
| 26 | + use SerializableElementTrait; |
| 27 | + |
| 28 | + |
| 29 | + /** |
| 30 | + * Whether the element may be normalized |
| 31 | + * |
| 32 | + * @var bool $normalization |
| 33 | + */ |
| 34 | + protected bool $normalization = true; |
| 35 | + |
| 36 | + /** |
| 37 | + * The localName of the element. |
| 38 | + * |
| 39 | + * @var string |
| 40 | + */ |
| 41 | + protected string $localName; |
| 42 | + |
| 43 | + /** |
| 44 | + * The namespaceURI of this element. |
| 45 | + * |
| 46 | + * @var string|null |
| 47 | + */ |
| 48 | + protected ?string $namespaceURI; |
| 49 | + |
| 50 | + /** |
| 51 | + * The prefix of this element. |
| 52 | + * |
| 53 | + * @var string |
| 54 | + */ |
| 55 | + protected string $prefix; |
| 56 | + |
| 57 | + |
| 58 | + /** |
| 59 | + * Create an XML Chunk from a copy of the given \DOMElement. |
| 60 | + * |
| 61 | + * @param \DOMElement $xml The element we should copy. |
| 62 | + */ |
| 63 | + public function __construct( |
| 64 | + protected DOMElement $xml, |
| 65 | + ) { |
| 66 | + $this->setLocalName($xml->localName); |
| 67 | + $this->setNamespaceURI($xml->namespaceURI); |
| 68 | + $this->setPrefix($xml->prefix); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * Get this \DOMElement. |
| 74 | + * |
| 75 | + * @return \DOMElement This element. |
| 76 | + */ |
| 77 | + public function getXML() : DOMElement |
| 78 | + { |
| 79 | + return $this->xml; |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + /** |
| 84 | + * Append this XML element to a different XML element. |
| 85 | + * |
| 86 | + * @param \DOMElement $parent The element we should append this element to. |
| 87 | + * @return \DOMElement The new element. |
| 88 | + */ |
| 89 | + public function toXML(DOMElement $parent) : DOMElement |
| 90 | + { |
| 91 | + return Utils::copyElement($this->xml, $parent); |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + /** |
| 96 | + * Collect the value of the localName-property |
| 97 | + * |
| 98 | + * @return string |
| 99 | + */ |
| 100 | + public function getLocalName() : string |
| 101 | + { |
| 102 | + return $this->localName; |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + /** |
| 107 | + * Set the value of the localName-property |
| 108 | + * |
| 109 | + * @param string $localName |
| 110 | + * @return void |
| 111 | + */ |
| 112 | + public function setLocalName(string $localName) : void |
| 113 | + { |
| 114 | + $this->localName = $localName; |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + /** |
| 119 | + * Collect the value of the namespaceURI-property |
| 120 | + * |
| 121 | + * @return string|null |
| 122 | + */ |
| 123 | + public function getNamespaceURI() : ?string |
| 124 | + { |
| 125 | + return $this->namespaceURI; |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + /** |
| 130 | + * Set the value of the namespaceURI-property |
| 131 | + * |
| 132 | + * @param string|null $namespaceURI |
| 133 | + * @return void |
| 134 | + */ |
| 135 | + public function setNamespaceURI(?string $namespaceURI = null) : void |
| 136 | + { |
| 137 | + $this->namespaceURI = $namespaceURI; |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + /** |
| 142 | + * Serialize this XML chunk. |
| 143 | + * |
| 144 | + * @return string The serialized chunk. |
| 145 | + */ |
| 146 | + public function serialize() : string |
| 147 | + { |
| 148 | + return serialize($this->xml->ownerDocument->saveXML($this->xml)); |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + /** |
| 153 | + * Un-serialize this XML chunk. |
| 154 | + * |
| 155 | + * @param string $serialized The serialized chunk. |
| 156 | + * @return void |
| 157 | + * |
| 158 | + * Type hint not possible due to upstream method signature |
| 159 | + */ |
| 160 | + public function unserialize($serialized) : void |
| 161 | + { |
| 162 | + $doc = DOMDocumentFactory::fromString(unserialize($serialized)); |
| 163 | + $this->xml = $doc->documentElement; |
| 164 | + $this->setLocalName($this->xml->localName); |
| 165 | + $this->setNamespaceURI($this->xml->namespaceURI); |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | + /** |
| 171 | + * Serialize this XML chunk. |
| 172 | + * |
| 173 | + * This method will be invoked by any calls to serialize(). |
| 174 | + * |
| 175 | + * @return array The serialized representation of this XML object. |
| 176 | + */ |
| 177 | + public function __serialize(): array |
| 178 | + { |
| 179 | + $xml = $this->getXML(); |
| 180 | + /** @psalm-var \DOMDocument $xml->ownerDocument */ |
| 181 | + return [$xml->ownerDocument->saveXML($xml)]; |
| 182 | + } |
| 183 | + |
| 184 | + |
| 185 | + /** |
| 186 | + * Unserialize an XML object and load it.. |
| 187 | + * |
| 188 | + * This method will be invoked by any calls to unserialize(), allowing us to restore any data that might not |
| 189 | + * be serializable in its original form (e.g.: DOM objects). |
| 190 | + * |
| 191 | + * @param array $vars The XML object that we want to restore. |
| 192 | + */ |
| 193 | + public function __unserialize(array $serialized): void |
| 194 | + { |
| 195 | + $xml = new self( |
| 196 | + DOMDocumentFactory::fromString(array_pop($serialized))->documentElement |
| 197 | + ); |
| 198 | + |
| 199 | + $vars = get_object_vars($xml); |
| 200 | + foreach ($vars as $k => $v) { |
| 201 | + $this->$k = $v; |
| 202 | + } |
| 203 | + } |
| 204 | +} |
0 commit comments