From 40176e229ec02a0cca66a8a304d8cb6b0832541e Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Fri, 29 Nov 2024 22:48:07 +0100 Subject: [PATCH] Add IntegerElementTrait --- src/IntegerElementTrait.php | 140 +++++++++++++++++++++ tests/Utils/IntegerElement.php | 33 +++++ tests/XML/IntegerElementTraitTest.php | 53 ++++++++ tests/resources/xml/ssp_IntegerElement.xml | 1 + 4 files changed, 227 insertions(+) create mode 100644 src/IntegerElementTrait.php create mode 100644 tests/Utils/IntegerElement.php create mode 100644 tests/XML/IntegerElementTraitTest.php create mode 100644 tests/resources/xml/ssp_IntegerElement.xml diff --git a/src/IntegerElementTrait.php b/src/IntegerElementTrait.php new file mode 100644 index 00000000..a5f6d6a4 --- /dev/null +++ b/src/IntegerElementTrait.php @@ -0,0 +1,140 @@ +validateContent($content); + $this->content = $content; + } + + + /** + * Get the content of the element. + * + * @return int + */ + public function getContent(): int + { + return $this->sanitizeContent($this->getRawContent()); + } + + + /** + * Get the raw content of the element. + * + * @return int + */ + public function getRawContent(): int + { + return $this->content; + } + + + /** + * Sanitize the content of the element. + * + * @param int $content The value to go in the XML textContent + * @throws \Exception on failure + * @return int + */ + protected function sanitizeContent(int $content): int + { + /** + * Perform no sanitation by default. + * Override this method on the implementing class to perform content sanitation. + */ + return $content; + } + + + /** + * Validate the content of the element. + * + * @param int $content The value to go in the XML textContent + * @throws \Exception on failure + * @return void + */ + protected function validateContent(/** @scrutinizer ignore-unused */ int $content): void + { + /** + * Perform no validation by default. + * Override this method on the implementing class to perform content validation. + */ + } + + + /** + * 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); + Assert::numeric($xml->textContent, SchemaViolationException::class); + + return new static(intval($xml->textContent)); + } + + + /** + * Convert this element to XML. + * + * @param \DOMElement|null $parent The element we should append this element to. + * @return \DOMElement + */ + public function toXML(DOMElement $parent = null): DOMElement + { + $e = $this->instantiateParentElement($parent); + $e->textContent = strval($this->getContent()); + + return $e; + } + + + /** + * @return string + */ + abstract public static function getLocalName(): string; + + + /** + * Create a document structure for this element + * + * @param \DOMElement|null $parent The element we should append to. + * @return \DOMElement + */ + abstract public function instantiateParentElement(DOMElement $parent = null): DOMElement; +} diff --git a/tests/Utils/IntegerElement.php b/tests/Utils/IntegerElement.php new file mode 100644 index 00000000..5fdf2a08 --- /dev/null +++ b/tests/Utils/IntegerElement.php @@ -0,0 +1,33 @@ +setContent($content); + } +} diff --git a/tests/XML/IntegerElementTraitTest.php b/tests/XML/IntegerElementTraitTest.php new file mode 100644 index 00000000..12a27fdd --- /dev/null +++ b/tests/XML/IntegerElementTraitTest.php @@ -0,0 +1,53 @@ +assertEquals( + self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), + strval($integerElement), + ); + } +} diff --git a/tests/resources/xml/ssp_IntegerElement.xml b/tests/resources/xml/ssp_IntegerElement.xml new file mode 100644 index 00000000..0f731c28 --- /dev/null +++ b/tests/resources/xml/ssp_IntegerElement.xml @@ -0,0 +1 @@ +1