Skip to content

Commit

Permalink
Fix serialization for the NameID element
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 25, 2024
1 parent 49e4264 commit e91bf4c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
59 changes: 58 additions & 1 deletion src/SAML2/XML/saml/NameIDType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
use DOMElement;
use SAML2\Constants;
use SAML2\DOMDocumentFactory;
use Serializable;

abstract class NameIDType
abstract class NameIDType implements Serializable
{
use IDNameQualifiersTrait;

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


/**
* Serialize this NameID.
*
* @return string The NameID serialized.
*/
public function serialize() : string
{
return serialize([
'NameQualifier' => $this->NameQualifier,
'SPNameQualifier' => $this->SPNameQualifier,
'nodeName' => $this->nodeName,
'Format' => $this->Format,
'SPProvidedID' => $this->SPProvidedID,
'value' => $this->value
]);
}


/**
* Un-serialize this NameID.
*
* @param string $serialized The serialized NameID.
* @return void
*
* Type hint not possible due to upstream method signature
*/
public function unserialize($serialized) : void
{
$unserialized = unserialize($serialized);
foreach ($unserialized as $k => $v) {
$this->$k = $v;
}
}


public function __serialize(): array
{
return [
'NameQualifier' => $this->getNameQualifier(),
'SPNameQualifier' => $this->getSPNameQualifier(),
'nodeName' => $this->nodeName,
'Format' => $this->Format,
'SPProvidedID' => $this->SPProvidedID,
'value' => $this->value
];
}


public function __unserialize($serialized): void
{
foreach ($serialized as $k => $v) {
$this->$k = $v;
}
}


/**
* Get a string representation of this BaseIDType object.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/SAML2/XML/saml/NameIDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,23 @@ public function testToString() : void

$this->assertXmlStringEqualsXmlString($output, $nameId->__toString());
}



/**
* Serialize a NameID and unserialize that again.
* @return void
*/
public function testSerialize() : void
{
$nid1 = new NameID();
$nid1->setValue('aap:noot:mies');
$ser = $nid1->serialize();

$nid2 = new NameID();
$nid2->setValue('Wim');
$nid2->unserialize($ser);

$this->assertEquals($nid1, $nid2);
}
}

0 comments on commit e91bf4c

Please sign in to comment.