Skip to content

Commit

Permalink
Arrayizable IDPList
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jun 12, 2023
1 parent 06402f8 commit 3efbb76
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/SAML2/XML/samlp/IDPList.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML2\Exception\ArrayValidationException;
use SimpleSAML\XML\Constants as C;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\MissingElementException;
use SimpleSAML\XML\Exception\TooManyElementsException;
use SimpleSAML\XML\Utils as XMLUtils;

use function array_change_key_case;
use function array_filter;
use function array_key_exists;
use function array_keys;
use function array_pop;
use function is_null;

Expand Down Expand Up @@ -115,4 +120,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['IDPEntry'],
$data['GetComplete'] ?? 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),
[
'idpentry',
'getcomplete',
],
ArrayValidationException::class,
);

Assert::keyExists($data, 'idpentry', ArrayValidationException::class);
Assert::isArray($data['idpentry'], ArrayValidationException::class);

$retval = ['IDPEntry' => [], 'GetComplete' => null];

foreach ($data['idpentry'] as $entry) {
$retval['IDPEntry'][] = IDPEntry::fromArray($entry);
}

if (array_key_exists('getcomplete', $data)) {
$retval['GetComplete'] = GetComplete::fromArray($data['getcomplete']);
}

return $retval;
}


/**
* Create an array from this class
*
* @return array
*/
public function toArray(): array
{
$data = [
'IDPEntry' => [],
'GetComplete' => $this->getGetComplete()?->toArray(),
];

foreach ($this->getIDPEntry() as $entry) {
$data['IDPEntry'][] = $entry->toArray();
}

return array_filter($data);
}
}
10 changes: 10 additions & 0 deletions tests/SAML2/XML/samlp/IDPListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use SimpleSAML\SAML2\XML\samlp\IDPList;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\Exception\MissingElementException;
use SimpleSAML\XML\TestUtils\ArrayizableElementTestTrait;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

Expand All @@ -28,6 +29,7 @@
*/
final class IDPListTest extends TestCase
{
use ArrayizableElementTestTrait;
use SchemaValidationTestTrait;
use SerializableElementTestTrait;

Expand All @@ -40,6 +42,14 @@ public static function setUpBeforeClass(): void

self::$testedClass = IDPList::class;

self::$arrayRepresentation = [
'IDPEntry' => [
['ProviderID' => 'urn:some:requester1', 'Name' => 'testName1', 'Loc' => 'urn:test:testLoc1'],
['ProviderID' => 'urn:some:requester2', 'Name' => 'testName2', 'Loc' => 'urn:test:testLoc2'],
],
'GetComplete' => ['https://some/location'],
];

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

0 comments on commit 3efbb76

Please sign in to comment.