Skip to content

Commit e91bf4c

Browse files
committed
Fix serialization for the NameID element
1 parent 49e4264 commit e91bf4c

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

src/SAML2/XML/saml/NameIDType.php

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
use DOMElement;
1414
use SAML2\Constants;
1515
use SAML2\DOMDocumentFactory;
16+
use Serializable;
1617

17-
abstract class NameIDType
18+
abstract class NameIDType implements Serializable
1819
{
1920
use IDNameQualifiersTrait;
2021

@@ -197,6 +198,62 @@ public function toXML(DOMElement $parent = null) : DOMElement
197198
}
198199

199200

201+
/**
202+
* Serialize this NameID.
203+
*
204+
* @return string The NameID serialized.
205+
*/
206+
public function serialize() : string
207+
{
208+
return serialize([
209+
'NameQualifier' => $this->NameQualifier,
210+
'SPNameQualifier' => $this->SPNameQualifier,
211+
'nodeName' => $this->nodeName,
212+
'Format' => $this->Format,
213+
'SPProvidedID' => $this->SPProvidedID,
214+
'value' => $this->value
215+
]);
216+
}
217+
218+
219+
/**
220+
* Un-serialize this NameID.
221+
*
222+
* @param string $serialized The serialized NameID.
223+
* @return void
224+
*
225+
* Type hint not possible due to upstream method signature
226+
*/
227+
public function unserialize($serialized) : void
228+
{
229+
$unserialized = unserialize($serialized);
230+
foreach ($unserialized as $k => $v) {
231+
$this->$k = $v;
232+
}
233+
}
234+
235+
236+
public function __serialize(): array
237+
{
238+
return [
239+
'NameQualifier' => $this->getNameQualifier(),
240+
'SPNameQualifier' => $this->getSPNameQualifier(),
241+
'nodeName' => $this->nodeName,
242+
'Format' => $this->Format,
243+
'SPProvidedID' => $this->SPProvidedID,
244+
'value' => $this->value
245+
];
246+
}
247+
248+
249+
public function __unserialize($serialized): void
250+
{
251+
foreach ($serialized as $k => $v) {
252+
$this->$k = $v;
253+
}
254+
}
255+
256+
200257
/**
201258
* Get a string representation of this BaseIDType object.
202259
*

tests/SAML2/XML/saml/NameIDTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,23 @@ public function testToString() : void
7777

7878
$this->assertXmlStringEqualsXmlString($output, $nameId->__toString());
7979
}
80+
81+
82+
83+
/**
84+
* Serialize a NameID and unserialize that again.
85+
* @return void
86+
*/
87+
public function testSerialize() : void
88+
{
89+
$nid1 = new NameID();
90+
$nid1->setValue('aap:noot:mies');
91+
$ser = $nid1->serialize();
92+
93+
$nid2 = new NameID();
94+
$nid2->setValue('Wim');
95+
$nid2->unserialize($ser);
96+
97+
$this->assertEquals($nid1, $nid2);
98+
}
8099
}

0 commit comments

Comments
 (0)