Skip to content

Commit f2bc5d4

Browse files
committed
Restore backwards compatibility
1 parent 53caaef commit f2bc5d4

4 files changed

Lines changed: 464 additions & 0 deletions

File tree

src/SAML2/DOMDocumentFactory.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SAML2;
6+
7+
use DOMDocument;
8+
use SimpleSAML\XML\DOMDocumentFactory as BaseDOMDocumentFactory;
9+
10+
/**
11+
* @package SimpleSAMLphp
12+
* @deprecated Use \SimpleSAML\XML\DOMDocumentFactory instead (simplesamlphp/xml-common)
13+
*/
14+
class DOMDocumentFactory
15+
{
16+
/**
17+
* Constructor for DOMDocumentFactory.
18+
* This class should never be instantiated
19+
*/
20+
private function __construct()
21+
{
22+
}
23+
24+
25+
/**
26+
* @param string $xml
27+
*
28+
* @return \DOMDocument
29+
*/
30+
public static function fromString(string $xml): DOMDocument
31+
{
32+
return BaseDOMDocumentFactory::fromString($xml);
33+
}
34+
35+
36+
/**
37+
* @param string $file
38+
*
39+
* @return \DOMDocument
40+
*/
41+
public static function fromFile(string $file): DOMDocument
42+
{
43+
return BaseDOMDocumentFactory::fromFile($file);
44+
}
45+
46+
47+
/**
48+
* @return \DOMDocument
49+
*/
50+
public static function create() : DOMDocument
51+
{
52+
return BaseDOMDocumentFactory::create('1.0', 'UTF-8');
53+
}
54+
}

src/SAML2/XML/Chunk.php

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

0 commit comments

Comments
 (0)