From 0ec858a2bf1ef3fc59f5b66d75c3d434af7f2ad4 Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Mon, 22 Jan 2024 21:40:48 +0100 Subject: [PATCH] Introduce new BooleanElementTrait and URIElementTrait --- src/BooleanElementTrait.php | 70 ++++++++++++++++++++++ src/LocalizedStringElementTrait.php | 2 - src/URIElementTrait.php | 64 ++++++++++++++++++++ tests/Utils/BooleanElement.php | 33 ++++++++++ tests/Utils/URIElement.php | 33 ++++++++++ tests/XML/BooleanElementTraitTest.php | 51 ++++++++++++++++ tests/XML/URIElementTraitTest.php | 51 ++++++++++++++++ tests/resources/xml/ssp_BooleanElement.xml | 1 + tests/resources/xml/ssp_URIElement.xml | 1 + 9 files changed, 304 insertions(+), 2 deletions(-) create mode 100644 src/BooleanElementTrait.php create mode 100644 src/URIElementTrait.php create mode 100644 tests/Utils/BooleanElement.php create mode 100644 tests/Utils/URIElement.php create mode 100644 tests/XML/BooleanElementTraitTest.php create mode 100644 tests/XML/URIElementTraitTest.php create mode 100644 tests/resources/xml/ssp_BooleanElement.xml create mode 100644 tests/resources/xml/ssp_URIElement.xml 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