Skip to content

Commit df2722c

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

File tree

4 files changed

+463
-0
lines changed

4 files changed

+463
-0
lines changed

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

0 commit comments

Comments
 (0)