Skip to content

Commit

Permalink
Make NameIDPolicy arrayizable
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jun 11, 2023
1 parent d2025d5 commit e9a2f49
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
82 changes: 81 additions & 1 deletion src/SAML2/XML/samlp/NameIDPolicy.php
Expand Up @@ -6,16 +6,21 @@

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\ArrayizableElementInterface;
use SimpleSAML\XML\Exception\InvalidDOMElementException;

use function array_change_key_case;
use function array_filter;
use function array_key_exists;
use function array_keys;
use function var_export;

/**
* Class for handling SAML2 NameIDPolicy.
*
* @package simplesamlphp/saml2
*/
final class NameIDPolicy extends AbstractSamlpElement
final class NameIDPolicy extends AbstractSamlpElement implements ArrayizableElementInterface
{
/**
* Initialize a NameIDPolicy.
Expand Down Expand Up @@ -124,4 +129,79 @@ public function toXML(DOMElement $parent = null): DOMElement

return $e;
}


/**
* Create a class from an array
*
* @param array $data
* @return static
*/
public static function fromArray(array $data): static
{
$data = self::processArrayContents($data);

return new static(
$data['Format'] ?? null,
$data['SPNameQualifier'] ?? null,
$data['AllowCreate'] ?? null,
);
}


/**
* Validates an array representation of this object and returns the same array with
* rationalized keys (casing) and parsed sub-elements.
*
* @param array $data
* @return array $data
*/
private static function processArrayContents(array $data): array
{
$data = array_change_key_case($data, CASE_LOWER);

// Make sure the array keys are known for this kind of object
Assert::allOneOf(
array_keys($data),
[
'format',
'spnamequalifier',
'allowcreate',
],
ArrayValidationException::class,
);

Assert::string($data['format'], ArrayValidationException::class);

$retval = ['Format' => $data['format']];

if (array_key_exists('spnamequalifier', $data)) {
Assert::string($data['spnamequalifier'], ArrayValidationException::class);
$retval['SPNameQualifier'] = $data['spnamequalifier'];
}

if (array_key_exists('allowcreate', $data)) {
Assert::boolean($data['allowcreate'], ArrayValidationException::class);
$retval['AllowCreate'] = $data['allowcreate'];
}

return $retval;
}


/**
* Create an array from this class
*
* @return array
*/
public function toArray(): array
{
$data = [
'Format' => $this->getFormat(),
'SPNameQualifier' => $this->getSPNameQualifier(),
'AllowCreate' => $this->getAllowCreate(),
];

return array_filter($data);
}
}
8 changes: 8 additions & 0 deletions tests/SAML2/XML/samlp/NameIDPolicyTest.php
Expand Up @@ -9,6 +9,7 @@
use SimpleSAML\SAML2\Constants as C;
use SimpleSAML\SAML2\XML\samlp\NameIDPolicy;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\ArrayizableElementTestTrait;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

Expand All @@ -25,6 +26,7 @@
*/
final class NameIDPolicyTest extends TestCase
{
use ArrayizableElementTestTrait;
use SchemaValidationTestTrait;
use SerializableElementTestTrait;

Expand All @@ -37,6 +39,12 @@ public static function setUpBeforeClass(): void

self::$testedClass = NameIDPolicy::class;

self::$arrayRepresentation = [
'Format' => C::NAMEID_TRANSIENT,
'SPNameQualifier' => 'https://some/qualifier',
'AllowCreate' => true,
];

self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 4) . '/resources/xml/samlp_NameIDPolicy.xml',
);
Expand Down

0 comments on commit e9a2f49

Please sign in to comment.