diff --git a/build/phpunit.xml b/build/phpunit.xml index e73aea9..9e0e12c 100644 --- a/build/phpunit.xml +++ b/build/phpunit.xml @@ -37,6 +37,9 @@ ../tests/testcases/OrderDocumentPdfReaderComfortTest.php ../tests/testcases/OrderDocumentPdfReaderExtendedTest.php + + ../tests/testcases/OrderDocumentJsonExporterTest.php + diff --git a/src/OrderDocumentJsonExporter.php b/src/OrderDocumentJsonExporter.php new file mode 100644 index 0000000..9169e42 --- /dev/null +++ b/src/OrderDocumentJsonExporter.php @@ -0,0 +1,174 @@ + + * @license https://opensource.org/licenses/MIT MIT + * @link https://github.com/horstoeko/orderx + */ +class OrderDocumentJsonExporter +{ + /** + * The instance to the order-x document + * + * @var OrderDocument + */ + private $document = null; + + /** + * @internal + * Serializer builder + * @var SerializerBuilder + */ + private $serializerBuilder; + + /** + * @internal + * Serializer + * @var SerializerInterface + */ + private $serializer; + + /** + * Constructor + * + * @param OrderDocument $document + * + * @codeCoverageIgnore + */ + public function __construct(OrderDocument $document) + { + $this->document = $document; + $this->initSerialzer(); + } + + /** + * Returns the order object as a json string + * + * @return string + */ + public function toJsonString(): string + { + return $this->serializer->serialize($this->document->getOrderObject(), 'json'); + } + + /** + * Returns the order object as a json object + * + * @return null|stdClass + * @throws ExceptionRuntimeException + */ + public function toJsonObject(): ?\stdClass + { + $jsonObject = json_decode($this->toJsonString()); + + return $jsonObject; + } + + /** + * Returns the order object as a pretty printed json string + * + * @return string|boolean + */ + public function toPrettyJsonString() + { + return json_encode($this->toJsonObject(), JSON_PRETTY_PRINT); + } + + /** + * @internal + * + * Build the internal serialzer + * + * @return OrderDocumentJsonExporter + * + * @codeCoverageIgnore + */ + private function initSerialzer(): OrderDocumentJsonExporter + { + $this->serializerBuilder = SerializerBuilder::create(); + + $this->serializerBuilder->addMetadataDir( + PathUtils::combineAllPaths( + OrderSettings::getYamlDirectory(), + $this->document->getProfileDefinition()["name"], + 'qdt' + ), + sprintf( + 'horstoeko\orderx\entities\%s\qdt', + $this->document->getProfileDefinition()["name"] + ) + ); + $this->serializerBuilder->addMetadataDir( + PathUtils::combineAllPaths( + OrderSettings::getYamlDirectory(), + $this->document->getProfileDefinition()["name"], + 'ram' + ), + sprintf( + 'horstoeko\orderx\entities\%s\ram', + $this->document->getProfileDefinition()["name"] + ) + ); + $this->serializerBuilder->addMetadataDir( + PathUtils::combineAllPaths( + OrderSettings::getYamlDirectory(), + $this->document->getProfileDefinition()["name"], + 'rsm' + ), + sprintf( + 'horstoeko\orderx\entities\%s\rsm', + $this->document->getProfileDefinition()["name"] + ) + ); + $this->serializerBuilder->addMetadataDir( + PathUtils::combineAllPaths( + OrderSettings::getYamlDirectory(), + $this->document->getProfileDefinition()["name"], + 'udt' + ), + sprintf( + 'horstoeko\orderx\entities\%s\udt', + $this->document->getProfileDefinition()["name"] + ) + ); + + $this->serializerBuilder->addDefaultListeners(); + $this->serializerBuilder->addDefaultHandlers(); + + $this->serializerBuilder->configureHandlers( + function (HandlerRegistryInterface $handler) { + $handler->registerSubscribingHandler(new BaseTypesHandler()); + $handler->registerSubscribingHandler(new XmlSchemaDateHandler()); + $handler->registerSubscribingHandler(new OrderTypesHandler()); + } + ); + + $this->serializer = $this->serializerBuilder->build(); + + return $this; + } +} diff --git a/tests/testcases/OrderDocumentJsonExporterTest.php b/tests/testcases/OrderDocumentJsonExporterTest.php new file mode 100644 index 0000000..752ac84 --- /dev/null +++ b/tests/testcases/OrderDocumentJsonExporterTest.php @@ -0,0 +1,56 @@ +toJsonString(); + + $this->assertStringStartsWith('{"ExchangedDocumentContext"', $jsonString); + $this->assertStringContainsString('},"GuidelineSpecifiedDocumentContextParameter"', $jsonString); + } + + /** + * @covers \horstoeko\orderx\OrderDocumentJsonExporter::toPrettyJsonString + */ + public function testToPrettyJsonString(): void + { + $exporter = new OrderDocumentJsonExporter(static::$document); + $jsonString = $exporter->toPrettyJsonString(); + + $this->assertStringStartsWith("{\n \"ExchangedDocumentContext\":", $jsonString); + } + + /** + * @covers \horstoeko\orderx\OrderDocumentJsonExporter::toJsonObject + */ + public function testToJsonObject(): void + { + $exporter = new OrderDocumentJsonExporter(static::$document); + $jsonObject = $exporter->toJsonObject(); + + $this->assertInstanceOf("stdClass", $jsonObject); + $this->assertTrue(isset($jsonObject->ExchangedDocumentContext)); + $this->assertTrue(isset($jsonObject->ExchangedDocumentContext->GuidelineSpecifiedDocumentContextParameter)); + } +}