-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
HorstOeko
committed
Jan 7, 2024
1 parent
e719634
commit e16d0e8
Showing
3 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |