From 83e54149a5dd12688be1b5f4060e97fbcfdcceb9 Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Wed, 4 Dec 2024 22:56:38 +0100 Subject: [PATCH] Add HexBinaryElementTrait --- src/HexBinaryElementTrait.php | 65 +++++++++++ tests/Utils/HexBinaryElement.php | 33 ++++++ tests/XML/HexBinaryElementTraitTest.php | 112 +++++++++++++++++++ tests/resources/xml/ssp_HexBinaryElement.xml | 1 + 4 files changed, 211 insertions(+) create mode 100644 src/HexBinaryElementTrait.php create mode 100644 tests/Utils/HexBinaryElement.php create mode 100644 tests/XML/HexBinaryElementTraitTest.php create mode 100644 tests/resources/xml/ssp_HexBinaryElement.xml diff --git a/src/HexBinaryElementTrait.php b/src/HexBinaryElementTrait.php new file mode 100644 index 0000000..675eb76 --- /dev/null +++ b/src/HexBinaryElementTrait.php @@ -0,0 +1,65 @@ +sanitizeContent($content), + '/([0-9A-F]{2})*/i', + SchemaViolationException::class, + ); + } + + + /** @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/HexBinaryElement.php b/tests/Utils/HexBinaryElement.php new file mode 100644 index 0000000..70dfcb9 --- /dev/null +++ b/tests/Utils/HexBinaryElement.php @@ -0,0 +1,33 @@ +setContent($content); + } +} diff --git a/tests/XML/HexBinaryElementTraitTest.php b/tests/XML/HexBinaryElementTraitTest.php new file mode 100644 index 0000000..1118a84 --- /dev/null +++ b/tests/XML/HexBinaryElementTraitTest.php @@ -0,0 +1,112 @@ +assertEquals( + XMLDumper::dumpDOMDocumentXMLWithBase64Content(self::$xmlRepresentation), + strval($hexBinaryElement), + ); + } + + + /** + */ + public function testUnmarshalling(): void + { + /** @var \DOMElement $xml */ + $xml = self::$xmlRepresentation->documentElement; + $hexBinaryElement = HexBinaryElement::fromXML($xml); + + $this->assertEquals( + '3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f', + $hexBinaryElement->getContent(), + ); + } + + + /** + * @param non-empty-string $xml + */ + #[DataProvider('provideHexBinaryCases')] + public function testHexBinaryCases(string $xml): void + { + $xmlRepresentation = DOMDocumentFactory::fromString($xml); + /** @var \DOMElement $xmlElement */ + $xmlElement = $xmlRepresentation->documentElement; + + $hexBinary = HexBinaryElement::fromXML($xmlElement); + + $this->assertStringContainsString($hexBinary->getRawContent(), $xml); + } + + /** + * @return array + */ + public static function provideHexBinaryCases(): array + { + return [ + 'inline' => [ + <<3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f +XML + , + ], + 'multiline' => [ + << +3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f + +XML + , + ], + ]; + } +} diff --git a/tests/resources/xml/ssp_HexBinaryElement.xml b/tests/resources/xml/ssp_HexBinaryElement.xml new file mode 100644 index 0000000..72f1379 --- /dev/null +++ b/tests/resources/xml/ssp_HexBinaryElement.xml @@ -0,0 +1 @@ +3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f