diff --git a/src/XML/mex/MetadataSection.php b/src/XML/mex/MetadataSection.php new file mode 100644 index 00000000..acf681f8 --- /dev/null +++ b/src/XML/mex/MetadataSection.php @@ -0,0 +1,155 @@ + $namespacedAttributes + */ + final public function __construct( + protected SerializableElementInterface|MetadataReference|Location $child, + protected string $Dialect, + protected ?string $Identifier = null, + array $namespacedAttributes = [] + ) { + Assert::validURI($Dialect); + Assert::nullOrValidURI($Identifier); + + $this->setAttributesNS($namespacedAttributes); + } + + + /** + * Get the child property. + * + * @return (\SimpleSAML\XML\SerializableElementInterface| + * \SimpleSAML\WSSecurity\XML\mex\MetadataReference| + * \SimpleSAML\WSSecurity\XML\mex\Location) + */ + public function getChild(): SerializableElementInterface|MetadataReference|Location + { + return $this->child; + } + + + /** + * Get the Dialect property. + * + * @return string + */ + public function getDialect(): string + { + return $this->Dialect; + } + + + /** + * Get the Identifier property. + * + * @return string|null + */ + public function getIdentifier(): ?string + { + return $this->Identifier; + } + + + /** + * 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) { + if ($child->localName === 'MetadataReference') { + $children[] = MetadataReference::fromXML($child); + } elseif ($child->localName === 'Location') { + $children[] = Location::fromXML($child); + } + continue; + } + + $children[] = new Chunk($child); + } + + Assert::minCount($children, 1, MissingElementException::class); + Assert::maxCount($children, 1, TooManyElementsException::class); + + return new static( + array_pop($children), + self::getAttribute($xml, 'Dialect'), + self::getOptionalAttribute($xml, 'Identifier', null), + self::getAttributesNSFromXML($xml), + ); + } + + + /** + * Add this MetadataSection to an XML element. + * + * @param \DOMElement $parent The element we should append this MetadataSection to. + * @return \DOMElement + */ + public function toXML(DOMElement $parent = null): DOMElement + { + $e = parent::instantiateParentElement($parent); + $e->setAttribute('Dialect', $this->getDialect()); + + if ($this->getIdentifier() !== null) { + $e->setAttribute('Identifier', $this->getIdentifier()); + } + + $this->getChild()->toXML($e); + + foreach ($this->getAttributesNS() as $attr) { + $attr->toXML($e); + } + + return $e; + } +} diff --git a/src/XML/wst/AbstractBinaryExchangeType.php b/src/XML/wst/AbstractBinaryExchangeType.php index 0c16de65..a49d4570 100644 --- a/src/XML/wst/AbstractBinaryExchangeType.php +++ b/src/XML/wst/AbstractBinaryExchangeType.php @@ -22,7 +22,7 @@ abstract class AbstractBinaryExchangeType extends AbstractWstElement use ExtendableAttributesTrait; use StringElementTrait; - /** @var string|\SimpleSAML\XML\XsNamespace */ + /** The namespace-attribute for the xs:anyAttribute element */ public const XS_ANY_ATTR_NAMESPACE = NS::OTHER; diff --git a/tests/WSSecurity/XML/mex/MetadataSectionTest.php b/tests/WSSecurity/XML/mex/MetadataSectionTest.php new file mode 100644 index 00000000..12d6d22c --- /dev/null +++ b/tests/WSSecurity/XML/mex/MetadataSectionTest.php @@ -0,0 +1,68 @@ +assertEquals( + self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), + strval($metadataSection) + ); + } +} diff --git a/tests/resources/xml/mex_MetadataSection.xml b/tests/resources/xml/mex_MetadataSection.xml new file mode 100644 index 00000000..cb01ef43 --- /dev/null +++ b/tests/resources/xml/mex_MetadataSection.xml @@ -0,0 +1,3 @@ + + urn:x-simplesamlphp:namespace +