diff --git a/src/XML/fed/AbstractSecurityTokenType.php b/src/XML/fed/AbstractSecurityTokenType.php new file mode 100644 index 00000000..ed826b24 --- /dev/null +++ b/src/XML/fed/AbstractSecurityTokenType.php @@ -0,0 +1,107 @@ +setElements([$child]); + $this->setAttributesNS($namespacedAttributes); + } + + + /** + * Create an instance of this object from its XML representation. + * + * @param \DOMElement $xml + * @return static + * + * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException + * if the qualified name of the supplied element is wrong + */ + public static function fromXML(DOMElement $xml): static + { + Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); + Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); + + $children = []; + foreach ($xml->childNodes as $child) { + if (!($child instanceof DOMElement)) { + continue; + } elseif ($child->namespaceURI === static::NS) { + continue; + } + + $children[] = new Chunk($child); + } + + Assert::minCount($children, 1, SchemaViolationException::class); + Assert::maxCount($children, 1, SchemaViolationException::class); + + return new static( + array_pop($children), + self::getAttributesNSFromXML($xml), + ); + } + + + /** + * Add this AbstractSecurityTokenType to an XML element. + * + * @param \DOMElement $parent The element we should append this username token to. + * @return \DOMElement + */ + public function toXML(DOMElement $parent = null): DOMElement + { + $e = parent::instantiateParentElement($parent); + + foreach ($this->getAttributesNS() as $attr) { + $attr->toXML($e); + } + + /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $child */ + foreach ($this->getElements() as $child) { + if (!$child->isEmptyElement()) { + $child->toXML($e); + } + } + + return $e; + } +} diff --git a/src/XML/fed/SecurityToken.php b/src/XML/fed/SecurityToken.php new file mode 100644 index 00000000..26233181 --- /dev/null +++ b/src/XML/fed/SecurityToken.php @@ -0,0 +1,14 @@ +SomeChunk', + ); + + $securityToken = new SecurityToken( + new Chunk($child->documentElement), + [$attr1], + ); + + $this->assertEquals( + self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), + strval($securityToken), + ); + } +} diff --git a/tests/resources/xml/fed_SecurityToken.xml b/tests/resources/xml/fed_SecurityToken.xml new file mode 100644 index 00000000..574e5909 --- /dev/null +++ b/tests/resources/xml/fed_SecurityToken.xml @@ -0,0 +1,3 @@ + + SomeChunk +