Skip to content

Commit 95b17cd

Browse files
committed
Add toArray/fromArray to md:Organization
1 parent 10ad29c commit 95b17cd

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

src/SAML2/XML/md/Organization.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace SimpleSAML\SAML2\XML\md;
66

7+
use DOMDocument;
78
use DOMElement;
89
use Exception;
910
use SimpleSAML\Assert\Assert;
@@ -14,6 +15,9 @@
1415
use SimpleSAML\XML\ExtendableAttributesTrait;
1516
use SimpleSAML\XML\Utils as XMLUtils;
1617

18+
use function array_key_exists;
19+
use function array_merge;
20+
1721
/**
1822
* Class representing SAML 2 Organization element.
1923
*
@@ -161,4 +165,100 @@ public function toXML(DOMElement $parent = null): DOMElement
161165

162166
return $e;
163167
}
168+
169+
170+
/**
171+
* Create a class from an array
172+
*
173+
* @param array $data
174+
* @return self
175+
*/
176+
public static function fromArray(array $data): static
177+
{
178+
$orgNames = [];
179+
if (array_key_exists('OrganizationName', $data)) {
180+
Assert::count($data['OrganizationName'], 1);
181+
$orgNames[] = OrganizationName::fromArray($data['OrganizationName']);
182+
}
183+
184+
$orgDisplayNames = [];
185+
if (array_key_exists('OrganizationDisplayName', $data)) {
186+
Assert::count($data['OrganizationDisplayName'], 1);
187+
$orgDisplayNames[] = OrganizationDisplayName::fromArray($data['OrganizationDisplayName']);
188+
}
189+
190+
$orgURLs = [];
191+
if (array_key_exists('OrganizationURL', $data)) {
192+
Assert::count($data['OrganizationURL'], 1);
193+
$orgURLs[] = OrganizationURL::fromArray($data['OrganizationURL']);
194+
}
195+
196+
$Extensions = $data['Extensions'] ?? null;
197+
198+
// Anything after this should be (namespaced) attributes
199+
unset(
200+
$data['OrganizationName'],
201+
$data['OrganizationDisplayName'],
202+
$data['OrganizationURL'],
203+
$data['Extensions'],
204+
);
205+
206+
$attributes = [];
207+
foreach ($data as $ns => $attribute) {
208+
$name = array_key_first($attribute);
209+
$value = $attribute[$name];
210+
211+
$doc = new DOMDocument('1.0', 'UTF-8');
212+
$elt = $doc->createElement("placeholder");
213+
$elt->setAttributeNS($ns, $name, $value);
214+
215+
$attributes[] = $elt->getAttributeNode($name);
216+
}
217+
218+
return new static(
219+
$orgNames,
220+
$orgDisplayNames,
221+
$orgURLs,
222+
$Extensions,
223+
$attributes,
224+
);
225+
}
226+
227+
228+
/**
229+
* Create an array from this class
230+
*
231+
* @return array
232+
*/
233+
public function toArray(): array
234+
{
235+
$data = [
236+
'OrganizationName' => [],
237+
'OrganizationDisplayName' => [],
238+
'OrganizationURL' => [],
239+
'Extensions' => $this->getExtensions(),
240+
];
241+
242+
foreach ($this->getOrganizationName() as $orgName) {
243+
$data['OrganizationName'] = array_merge($data['OrganizationName'], $orgName->toArray());
244+
}
245+
246+
foreach ($this->getOrganizationDisplayName() as $orgDisplayName) {
247+
$data['OrganizationDisplayName'] = array_merge(
248+
$data['OrganizationDisplayName'],
249+
$orgDisplayName->toArray(),
250+
);
251+
}
252+
253+
foreach ($this->getOrganizationURL() as $orgURL) {
254+
$data['OrganizationURL'] = array_merge($data['OrganizationURL'], $orgURL->toArray());
255+
}
256+
257+
/** @psalm-suppress PossiblyNullReference */
258+
foreach ($this->getAttributesNS() as $a) {
259+
$data[$a['namespaceURI']] = [$a['qualifiedName'] => $a['value']];
260+
}
261+
262+
return $data;
263+
}
164264
}

tests/SAML2/XML/md/OrganizationTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use SimpleSAML\XML\Chunk;
1515
use SimpleSAML\XML\DOMDocumentFactory;
1616
use SimpleSAML\XML\Exception\MissingElementException;
17+
use SimpleSAML\XML\TestUtils\ArrayizableElementTestTrait;
1718
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
1819
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
1920

@@ -29,6 +30,7 @@
2930
*/
3031
final class OrganizationTest extends TestCase
3132
{
33+
use ArrayizableElementTestTrait;
3234
use SchemaValidationTestTrait;
3335
use SerializableElementTestTrait;
3436

@@ -41,6 +43,14 @@ protected function setUp(): void
4143

4244
$this->testedClass = Organization::class;
4345

46+
$this->arrayRepresentation = [
47+
'OrganizationName' => ['en' => 'SSP'],
48+
'OrganizationDisplayName' => ['en' => 'SimpleSAMLphp'],
49+
'OrganizationURL' => ['en' => 'https://simplesamlphp.org'],
50+
'Extensions' => null,
51+
'urn:test:something' => ['test:attr' => 'value'],
52+
];
53+
4454
$this->xmlRepresentation = DOMDocumentFactory::fromFile(
4555
dirname(__FILE__, 4) . '/resources/xml/md_Organization.xml',
4656
);

0 commit comments

Comments
 (0)