Skip to content

Commit

Permalink
Added a json export class for exporting ZugferdDocument as a json str…
Browse files Browse the repository at this point in the history
…ing, a json object or a pretty printed json string, Added an example for this feature
  • Loading branch information
ruff committed Jun 18, 2022
1 parent 2e90762 commit 1fc45b6
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
21 changes: 21 additions & 0 deletions examples/En16931ReaderPdfWithJsonExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use horstoeko\zugferd\ZugferdDocumentJsonExporter;
use horstoeko\zugferd\ZugferdDocumentPdfReader;

require getcwd() . "/../vendor/autoload.php";

$document = ZugferdDocumentPdfReader::readAndGuessFromFile(dirname(__FILE__) . "/invoice_1.pdf");

$document->getDocumentInformation($documentno, $documenttypecode, $documentdate, $invoiceCurrency, $taxCurrency, $documentname, $documentlanguage, $effectiveSpecifiedPeriod);

$documentJsonExporter = new ZugferdDocumentJsonExporter($document);

echo $documentJsonExporter->toPrettyJsonString();
echo "\r\n\r\n";

$jsonObject = $documentJsonExporter->toJsonObject();

var_dump($jsonObject);

echo "\r\n\r\n";
132 changes: 132 additions & 0 deletions src/ZugferdDocumentJsonExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

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

namespace horstoeko\zugferd;

use \GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler\BaseTypesHandler;
use \GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler\XmlSchemaDateHandler;
use \horstoeko\zugferd\jms\ZugferdTypesHandler;
use \JMS\Serializer\Handler\HandlerRegistryInterface;
use \JMS\Serializer\SerializerBuilder;
use \JMS\Serializer\SerializerInterface;
use RuntimeException;

/**
* Class representing the export of a zugferd document
* in JSON format
*
* @category Zugferd
* @package Zugferd
* @author D. Erling <horstoeko@erling.com.de>
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/horstoeko/zugferd
*/
class ZugferdDocumentJsonExporter
{
/**
* The instance to the zugferd document
*
* @var ZugferdDocument
*/
private $document = null;

/**
* @internal
* Serializer builder
* @var SerializerBuilder
*/
private $serializerBuilder;

/**
* @internal
* Serializer
* @var SerializerInterface
*/
private $serializer;

/**
* Constructor
*
* @param ZugferdDocument $document
*
* @codeCoverageIgnore
*/
public function __construct(ZugferdDocument $document)
{
$this->document = $document;
$this->initSerialzer();
}

/**
* Returns the invoice object as a json string
*
* @return string
*/
public function toJsonString(): string
{
return $this->serializer->serialize($this->document->getInvoiceObject(), 'json');
}

/**
* Returns the invoice object as a json object
*
* @return string
* @throws RuntimeException
*/
public function toJsonObject(): object
{
$jsonObject = json_decode($this->toJsonString());

if ($jsonObject === null) {
throw new \RuntimeException("Invalid JSON");
}

return $jsonObject;
}

/**
* Returns the invoice 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 ZugferdDocumentJsonExporter
*
* @codeCoverageIgnore
*/
private function initSerialzer(): ZugferdDocumentJsonExporter
{
$serializerBuilder = SerializerBuilder::create();
$this->serializerBuilder = $serializerBuilder;
$this->serializerBuilder->addMetadataDir(dirname(__FILE__) . '/yaml/' . $this->document->profiledef["name"] . '/qdt', 'horstoeko\zugferd\entities\\' . $this->document->profiledef["name"] . '\qdt');
$this->serializerBuilder->addMetadataDir(dirname(__FILE__) . '/yaml/' . $this->document->profiledef["name"] . '/ram', 'horstoeko\zugferd\entities\\' . $this->document->profiledef["name"] . '\ram');
$this->serializerBuilder->addMetadataDir(dirname(__FILE__) . '/yaml/' . $this->document->profiledef["name"] . '/rsm', 'horstoeko\zugferd\entities\\' . $this->document->profiledef["name"] . '\rsm');
$this->serializerBuilder->addMetadataDir(dirname(__FILE__) . '/yaml/' . $this->document->profiledef["name"] . '/udt', 'horstoeko\zugferd\entities\\' . $this->document->profiledef["name"] . '\udt');
$this->serializerBuilder->addDefaultListeners();
$this->serializerBuilder->configureHandlers(function (HandlerRegistryInterface $handler) use ($serializerBuilder) {
$serializerBuilder->addDefaultHandlers();
$handler->registerSubscribingHandler(new BaseTypesHandler());
$handler->registerSubscribingHandler(new XmlSchemaDateHandler());
$handler->registerSubscribingHandler(new ZugferdTypesHandler());
});

$this->serializer = $this->serializerBuilder->build();

return $this;
}
}

0 comments on commit 1fc45b6

Please sign in to comment.