|
4 | 4 |
|
5 | 5 | namespace SimpleSAML\SAML2\XML\md; |
6 | 6 |
|
| 7 | +use DOMDocument; |
7 | 8 | use DOMElement; |
8 | 9 | use Exception; |
9 | 10 | use SimpleSAML\Assert\Assert; |
|
14 | 15 | use SimpleSAML\XML\ExtendableAttributesTrait; |
15 | 16 | use SimpleSAML\XML\Utils as XMLUtils; |
16 | 17 |
|
| 18 | +use function array_key_exists; |
| 19 | +use function array_merge; |
| 20 | + |
17 | 21 | /** |
18 | 22 | * Class representing SAML 2 Organization element. |
19 | 23 | * |
@@ -161,4 +165,100 @@ public function toXML(DOMElement $parent = null): DOMElement |
161 | 165 |
|
162 | 166 | return $e; |
163 | 167 | } |
| 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 | + } |
164 | 264 | } |
0 commit comments