diff --git a/src/BooleanElementTrait.php b/src/BooleanElementTrait.php new file mode 100644 index 0000000..26ad172 --- /dev/null +++ b/src/BooleanElementTrait.php @@ -0,0 +1,70 @@ +getQualifiedName(), $content), + SchemaViolationException::class, + ); + } + + + /** + * Convert XML into a class instance + * + * @param \DOMElement $xml The XML element we should load + * @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); + + return new static($xml->textContent); + } + + + /** + * @param \DOMElement|null $parent + * @return \DOMElement + */ + final public function toXML(DOMElement $parent = null): DOMElement + { + $e = $this->instantiateParentElement($parent); + $e->textContent = $this->getContent(); + + return $e; + } +} diff --git a/src/LocalizedStringElementTrait.php b/src/LocalizedStringElementTrait.php index 97638e7..6fa8613 100644 --- a/src/LocalizedStringElementTrait.php +++ b/src/LocalizedStringElementTrait.php @@ -81,8 +81,6 @@ public static function fromXML(DOMElement $xml): static 'Missing xml:lang from ' . static::getLocalName(), MissingAttributeException::class, ); - Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); - Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); return new static($xml->getAttributeNS(C::NS_XML, 'lang'), $xml->textContent); } diff --git a/src/URIElementTrait.php b/src/URIElementTrait.php new file mode 100644 index 0000000..80ad08a --- /dev/null +++ b/src/URIElementTrait.php @@ -0,0 +1,64 @@ +localName, static::getLocalName(), InvalidDOMElementException::class); + Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); + + return new static($xml->textContent); + } + + + /** + * @param \DOMElement|null $parent + * @return \DOMElement + */ + final public function toXML(DOMElement $parent = null): DOMElement + { + $e = $this->instantiateParentElement($parent); + $e->textContent = $this->getContent(); + + return $e; + } +} diff --git a/tests/Utils/BooleanElement.php b/tests/Utils/BooleanElement.php new file mode 100644 index 0000000..5f6f7dd --- /dev/null +++ b/tests/Utils/BooleanElement.php @@ -0,0 +1,33 @@ +setContent($content); + } +} diff --git a/tests/Utils/URIElement.php b/tests/Utils/URIElement.php new file mode 100644 index 0000000..d0246a8 --- /dev/null +++ b/tests/Utils/URIElement.php @@ -0,0 +1,33 @@ +setContent($content); + } +} diff --git a/tests/XML/BooleanElementTraitTest.php b/tests/XML/BooleanElementTraitTest.php new file mode 100644 index 0000000..9565787 --- /dev/null +++ b/tests/XML/BooleanElementTraitTest.php @@ -0,0 +1,51 @@ +assertEquals( + self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), + strval($booleanElement), + ); + } +} diff --git a/tests/XML/URIElementTraitTest.php b/tests/XML/URIElementTraitTest.php new file mode 100644 index 0000000..be7f4bb --- /dev/null +++ b/tests/XML/URIElementTraitTest.php @@ -0,0 +1,51 @@ +assertEquals( + self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), + strval($URIElement), + ); + } +} diff --git a/tests/resources/xml/ssp_BooleanElement.xml b/tests/resources/xml/ssp_BooleanElement.xml new file mode 100644 index 0000000..bd1facb --- /dev/null +++ b/tests/resources/xml/ssp_BooleanElement.xml @@ -0,0 +1 @@ +true diff --git a/tests/resources/xml/ssp_URIElement.xml b/tests/resources/xml/ssp_URIElement.xml new file mode 100644 index 0000000..ef50d5c --- /dev/null +++ b/tests/resources/xml/ssp_URIElement.xml @@ -0,0 +1 @@ +https://example.org