From 89487094af26c3738f1c3eceecdbb0a52601b076 Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Thu, 25 Jan 2024 23:15:09 +0100 Subject: [PATCH] Add wst classes --- src/XML/wst/AbstractClaimsType.php | 133 ++++++++++++++++++++++++ src/XML/wst/Claims.php | 14 +++ tests/WSSecurity/XML/wst/ClaimsTest.php | 78 ++++++++++++++ tests/resources/xml/wst_Claims.xml | 3 + 4 files changed, 228 insertions(+) create mode 100644 src/XML/wst/AbstractClaimsType.php create mode 100644 src/XML/wst/Claims.php create mode 100644 tests/WSSecurity/XML/wst/ClaimsTest.php create mode 100644 tests/resources/xml/wst_Claims.xml diff --git a/src/XML/wst/AbstractClaimsType.php b/src/XML/wst/AbstractClaimsType.php new file mode 100644 index 00000000..f58974cf --- /dev/null +++ b/src/XML/wst/AbstractClaimsType.php @@ -0,0 +1,133 @@ +setElements($children); + $this->setAttributesNS($namespacedAttributes); + } + + + /** + * @return string|null + */ + public function getDialect(): ?string + { + return $this->dialect; + } + + + /** + * Test if an object, at the state it's in, would produce an empty XML-element + * + * @return bool + */ + public function isEmptyElement(): bool + { + return empty($this->getDialect()) + && empty($this->getElements()) + && empty($this->getAttributesNS()); + } + + + /** + * 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; + } + + $children[] = new Chunk($child); + } + + return new static( + self::getOptionalAttribute($xml, 'Dialect', null), + $children, + self::getAttributesNSFromXML($xml), + ); + } + + + /** + * Add this ClaimsType to an XML element. + * + * @param \DOMElement $parent The element we should append this element to. + * @return \DOMElement + */ + public function toXML(DOMElement $parent = null): DOMElement + { + $e = parent::instantiateParentElement($parent); + + if ($this->getDialect() !== null) { + $e->setAttribute('Dialect', $this->getDialect()); + } + + foreach ($this->getElements() as $child) { + if (!$child->isEmptyElement()) { + $child->toXML($e); + } + } + + foreach ($this->getAttributesNS() as $attr) { + $attr->toXML($e); + } + + return $e; + } +} diff --git a/src/XML/wst/Claims.php b/src/XML/wst/Claims.php new file mode 100644 index 00000000..7c53526f --- /dev/null +++ b/src/XML/wst/Claims.php @@ -0,0 +1,14 @@ +assertEquals( + self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), + strval($claims), + ); + } + + + /** + * Test creating an empty Claims object from scratch. + */ + public function testMarshallingEmpty(): void + { + $claims = new Claims(); + + $this->assertTrue($claims->isEmptyElement()); + } +} diff --git a/tests/resources/xml/wst_Claims.xml b/tests/resources/xml/wst_Claims.xml new file mode 100644 index 00000000..e6569885 --- /dev/null +++ b/tests/resources/xml/wst_Claims.xml @@ -0,0 +1,3 @@ + + uuid:d0ccf3cd-2dce-4c1a-a5d6-be8912ecd7de +