Skip to content

Commit 19c658c

Browse files
committed
Arrayizable IDPEntry
1 parent 64543f0 commit 19c658c

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

src/SAML2/XML/samlp/IDPEntry.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66

77
use DOMElement;
88
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\SAML2\Exception\ArrayValidationException;
910
use SimpleSAML\XML\Exception\InvalidDOMElementException;
1011
use SimpleSAML\XML\Exception\SchemaViolationException;
1112

13+
use function array_change_key_case;
14+
use function array_filter;
15+
use function array_key_exists;
16+
use function array_keys;
17+
1218
/**
1319
* Class for handling SAML2 IDPEntry.
1420
*
@@ -106,4 +112,80 @@ public function toXML(DOMElement $parent = null): DOMElement
106112

107113
return $e;
108114
}
115+
116+
117+
/**
118+
* Create a class from an array
119+
*
120+
* @param array $data
121+
* @return static
122+
*/
123+
public static function fromArray(array $data): static
124+
{
125+
$data = self::processArrayContents($data);
126+
127+
return new static(
128+
$data['ProviderID'],
129+
$data['Name'] ?? null,
130+
$data['Loc'] ?? null,
131+
);
132+
}
133+
134+
135+
/**
136+
* Validates an array representation of this object and returns the same array with
137+
* rationalized keys (casing) and parsed sub-elements.
138+
*
139+
* @param array $data
140+
* @return array $data
141+
*/
142+
private static function processArrayContents(array $data): array
143+
{
144+
$data = array_change_key_case($data, CASE_LOWER);
145+
146+
// Make sure the array keys are known for this kind of object
147+
Assert::allOneOf(
148+
array_keys($data),
149+
[
150+
'providerid',
151+
'name',
152+
'loc',
153+
],
154+
ArrayValidationException::class,
155+
);
156+
157+
Assert::keyExists($data, 'providerid', ArrayValidationException::class);
158+
Assert::string($data['providerid'], ArrayValidationException::class);
159+
160+
$retval = ['ProviderID' => $data['providerid']];
161+
162+
if (array_key_exists('name', $data)) {
163+
Assert::string($data['name'], ArrayValidationException::class);
164+
$retval['Name'] = $data['name'];
165+
}
166+
167+
if (array_key_exists('loc', $data)) {
168+
Assert::string($data['loc'], ArrayValidationException::class);
169+
$retval['Loc'] = $data['loc'];
170+
}
171+
172+
return $retval;
173+
}
174+
175+
176+
/**
177+
* Create an array from this class
178+
*
179+
* @return array
180+
*/
181+
public function toArray(): array
182+
{
183+
$data = [
184+
'ProviderID' => $this->getProviderID(),
185+
'Name' => $this->getName(),
186+
'Loc' => $this->getLoc(),
187+
];
188+
189+
return array_filter($data);
190+
}
109191
}

tests/SAML2/XML/samlp/IDPEntryTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPUnit\Framework\TestCase;
99
use SimpleSAML\SAML2\XML\samlp\IDPEntry;
1010
use SimpleSAML\XML\DOMDocumentFactory;
11+
use SimpleSAML\XML\TestUtils\ArrayizableElementTestTrait;
1112
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
1213
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
1314

@@ -24,6 +25,7 @@
2425
*/
2526
final class IDPEntryTest extends TestCase
2627
{
28+
use ArrayizableElementTestTrait;
2729
use SchemaValidationTestTrait;
2830
use SerializableElementTestTrait;
2931

@@ -36,6 +38,12 @@ public static function setUpBeforeClass(): void
3638

3739
self::$testedClass = IDPentry::class;
3840

41+
self::$arrayRepresentation = [
42+
'ProviderID' => 'urn:some:requester',
43+
'Name' => 'testName',
44+
'Loc' => 'urn:test:testLoc',
45+
];
46+
3947
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
4048
dirname(__FILE__, 4) . '/resources/xml/samlp_IDPEntry.xml',
4149
);

0 commit comments

Comments
 (0)