Skip to content

Commit

Permalink
Added JSON Exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
HorstOeko committed Jan 7, 2024
1 parent e719634 commit e16d0e8
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<file>../tests/testcases/OrderDocumentPdfReaderComfortTest.php</file>
<file>../tests/testcases/OrderDocumentPdfReaderExtendedTest.php</file>
</testsuite>
<testsuite name="Exporter">
<file>../tests/testcases/OrderDocumentJsonExporterTest.php</file>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
Expand Down
174 changes: 174 additions & 0 deletions src/OrderDocumentJsonExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

/**
* This file is a part of horstoeko/order-x.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace horstoeko\orderx;

use GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler\BaseTypesHandler;
use GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler\XmlSchemaDateHandler;
use horstoeko\orderx\jms\OrderTypesHandler;
use horstoeko\stringmanagement\PathUtils;
use JMS\Serializer\Exception\RuntimeException as ExceptionRuntimeException;
use JMS\Serializer\Handler\HandlerRegistryInterface;
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\SerializerInterface;
use stdClass;

/**
* Class representing the export of a order-x document
* in JSON format
*
* @category Order-X
* @package Order-X
* @author D. Erling <horstoeko@erling.com.de>
* @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;
}
}
56 changes: 56 additions & 0 deletions tests/testcases/OrderDocumentJsonExporterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace horstoeko\orderx\tests\testcases;

use \horstoeko\orderx\tests\TestCase;
use \horstoeko\orderx\OrderDocumentJsonExporter;
use \horstoeko\orderx\OrderDocumentReader;

class OrderDocumentJsonExporterTest extends TestCase
{
/**
* @var OrderDocumentReader
*/
protected static $document;

public static function setUpBeforeClass(): void
{
self::$document = OrderDocumentReader::readAndGuessFromFile(dirname(__FILE__) . '/../assets/reader-order-x-comfort.xml');
}

/**
* @covers \horstoeko\orderx\OrderDocumentJsonExporter::toJsonString
*/
public function testToJsonString(): void
{
$exporter = new OrderDocumentJsonExporter(static::$document);
$jsonString = $exporter->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));
}
}

0 comments on commit e16d0e8

Please sign in to comment.