This commit is contained in:
Thomas Peterson 2025-07-25 10:09:18 +02:00
parent 872706857b
commit 3e559ecfae
10 changed files with 5809 additions and 5296 deletions

File diff suppressed because it is too large Load Diff

View File

@ -39,7 +39,7 @@ class Headline extends Base
return $this->element;
}
public function parseJSON(): PSCHeadline
public function parseJson(): PSCHeadline
{
parent::parseJson();

View File

@ -1,26 +1,43 @@
<?php
namespace PSC\Library\Calc\Option\Parser;
use PSC\Library\Calc\General\Parser\EdgeCollectionContainer;
use PSC\Library\Calc\Option\Parser\Row\Column;
use PSC\Library\Calc\Option\Parser\Base;
use PSC\Library\Calc\Option\Parser\Row\Column;
use PSC\Library\Calc\Option\Type\Base as PSCBase;
class Row extends Base
{
protected $element;
public function __construct(private $paperContainer, private $paperRepository, private $templates)
{
public function __construct(
private $paperContainer,
private $paperRepository,
private $templates,
) {
$this->element = new \PSC\Library\Calc\Option\Type\Row();
}
public function parseJson(): PSCBase
{
parent::parseJson();
foreach ($this->json['columns'] as $col) {
$p = new Column($this->paperContainer, $this->paperRepository, $this->templates);
$p->fromJson($col);
$element = $p->parseJson();
$this->element->addColumn($element);
}
return $this->element;
}
public function parseXML(): PSCBase
{
parent::parseXML();
foreach($this->node->children() as $key => $node) {
foreach ($this->node->children() as $key => $node) {
$p = new Column($this->paperContainer, $this->paperRepository, $this->templates);
$p->fromXML($node);
$element = $p->parseXML();
@ -29,5 +46,4 @@ class Row extends Base
return $this->element;
}
}

View File

@ -1,4 +1,5 @@
<?php
namespace PSC\Library\Calc\Option\Parser\Row;
use PSC\Library\Calc\Option\Parser;
@ -12,43 +13,77 @@ class Column extends Base
{
private Parser $optionParser;
public function __construct(private $paperContainer, private $paperRepository, private $templates)
{
public function __construct(
private $paperContainer,
private $paperRepository,
private $templates,
) {
$this->element = new \PSC\Library\Calc\Option\Type\Row\Column();
$this->optionParser = new \PSC\Library\Calc\Option\Parser($paperContainer, $paperRepository, $templates);
}
public function parseJson(): PSCColumn
{
parent::parseJson();
foreach ($this->json['options'] as $option) {
$obj = $this->optionParser->getOptByJsonType($option['type'], $option['mode'] ?? null);
if ($obj) {
$obj->fromJson($option);
if ($obj instanceof Select) {
$obj->setPaperContainer($this->getPaperContainer());
$obj->setPaperRepository($this->getPaperRepository());
}
if ($obj instanceof Template) {
$element = $obj->parseJson();
$default = $element->getDefault();
$node = $this->templates->xpath('//option[@id="' . $element->getSelect() . '"]');
$obj = $this->optionParser->getOptByType($node[0]);
$element = $obj->parseJson();
if ($default != '') {
$element->setDefault($default);
}
} else {
$element = $obj->parseJson();
}
$this->element->addOption($element);
}
}
return $this->element;
}
public function parseXML(): PSCColumn
{
parent::parseXML();
foreach ($this->node->children() as $key => $option) {
if($key == 'option') {
$obj = $this->optionParser->getOptByType($option['type'], $option['mode']?? null);
if($obj) {
foreach ($this->node->children() as $key => $option) {
if ($key == 'option') {
$obj = $this->optionParser->getOptByType($option['type'], $option['mode'] ?? null);
if ($obj) {
$obj->fromXML($option);
if($obj instanceof Select) {
if ($obj instanceof Select) {
$obj->setPaperContainer($this->paperContainer);
$obj->setPaperRepository($this->paperRepository);
}
if($obj instanceof Template) {
if ($obj instanceof Template) {
$element = $obj->parseXML();
$default = $element->getDefault();
$node = $this->templates->xpath('//option[@id="' . $element->getSelect() . '"]');
$obj = $this->optionParser->getOptByType($node[0]['type'], $node[0]['mode']?? null);
$obj = $this->optionParser->getOptByType($node[0]['type'], $node[0]['mode'] ?? null);
$obj->fromXML($node[0]);
$element = $obj->parseXML();
if($default != "") {
if ($default != '') {
$element->setDefault($default);
}
}else{
} else {
$element = $obj->parseXML();
}
$this->element->addOption($element);
}
}elseif($key == 'row') {
} elseif ($key == 'row') {
$obj = new Row($this->paperContainer, $this->paperRepository, $this->templates);
$obj->fromXML($option);
$element = $obj->parseXML();
@ -58,6 +93,4 @@ class Column extends Base
return $this->element;
}
}

View File

@ -1,4 +1,5 @@
<?php
namespace PSC\Library\Calc\Option\Type;
use PSC\Library\Calc\Option\Type\Row\Column;
@ -25,10 +26,44 @@ class Row extends Base
return $this->columns;
}
public function getColumnById(string $id): ?Column
public function getColumnById(string $id): null|Column
{
return array_find((array)$this->columns, function(Column $c) use ($id) {
return array_find((array) $this->columns, function (Column $c) use ($id) {
return $c->getId() == $id;
});
}
public function generateXML(): array
{
$tmp = [];
$columns = $this->columns;
if (count($columns) > 0) {
$tmp = ['column' => function () use ($columns) {
$xml_columns = [];
foreach ($columns as $col) {
$xml_columns[] = $col->generateXML();
}
return $xml_columns;
}];
}
return array_merge_recursive($tmp, parent::generateXML());
}
public function generateJson(): \stdClass
{
$obj = new \stdClass();
$obj->id = $this->getId();
$obj->dependencys = [];
$obj->columns = [];
$obj->type = 7;
foreach ($this->edgesCollectionContainer as $col) {
$obj->dependencys[] = $col->generateJson();
}
foreach ($this->getColumns() as $col) {
$obj->columns[] = $col->generateJson();
}
return $obj;
}
}

View File

@ -1,4 +1,5 @@
<?php
namespace PSC\Library\Calc\Option\Type\Row;
use PSC\Library\Calc\Option\Type\Base;
@ -13,7 +14,7 @@ class Column extends Base
$this->options = new \ArrayIterator();
}
public function addOption(Base $option):void
public function addOption(Base $option): void
{
$this->options->append($option);
}
@ -23,10 +24,44 @@ class Column extends Base
return $this->options;
}
public function getOptionById(string $id): ?Base
public function getOptionById(string $id): null|Base
{
return array_find((array)$this->options, function(Base $c) use ($id) {
return array_find((array) $this->options, function (Base $c) use ($id) {
return $c->getId() == $id;
});
}
public function generateXML(): array
{
$tmp = [];
$options = $this->options;
if (count($options) > 0) {
$tmp = ['option' => function () use ($options) {
$xml_options = [];
foreach ($options as $option) {
$xml_options[] = $option->generateXML();
}
return $xml_options;
}];
}
return array_merge_recursive($tmp, ['_attributes' => ['id' => $this->id]]);
}
public function generateJson(): \stdClass
{
$obj = new \stdClass();
$obj->id = $this->getId();
$obj->dependencys = [];
$obj->options = [];
$obj->type = 8;
foreach ($this->edgesCollectionContainer as $col) {
$obj->dependencys[] = $col->generateJson();
}
foreach ($this->getOptions() as $opt) {
$obj->options[] = $opt->generateJson();
}
return $obj;
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace PSC\Library\Calc\Tests\Json\Type;
use PHPUnit\Framework\TestCase;
use PSC\Library\Calc\Option\Parser\Headline;
use Spatie\ArrayToXml\ArrayToXml;
class HeadlineTest extends TestCase
{
private array $json;
private string $xml;
public function setUp(): void
{
$this->json = json_decode('{
"id": "c09e76a5-d9be-4a27-92cc-f9462e79b4e5",
"type": 6,
"dependencys": [],
"default": "Kalkulation",
"name": "",
"variant": 3
}', true);
$this->xml = '<option default="Kalkulation" variant="3" id="c09e76a5-d9be-4a27-92cc-f9462e79b4e5" type="Headline"/>';
}
public function testFromJson()
{
$parser = new Headline();
$parser->fromJson($this->json);
$obj = $parser->parseJson();
$xml = ArrayToXml::convert($obj->generateXML(), 'option');
self::assertXmlStringEqualsXmlString($xml, $this->xml);
}
public function testFromXML()
{
$parser = new Headline();
$parser->fromXML(simplexml_load_string($this->xml));
$obj = $parser->parseXML();
self::assertJsonStringEqualsJsonString(json_encode($obj->generateJson()), json_encode($this->json));
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace PSC\Library\Calc\Tests\Json\Type;
use PHPUnit\Framework\TestCase;
use PSC\Library\Calc\Option\Parser\Row;
use Spatie\ArrayToXml\ArrayToXml;
class RowTest extends TestCase
{
private array $json;
private string $xml;
public function setUp(): void
{
$this->json = json_decode('{
"id": "dee2007d-1ffe-430e-b75f-4f32a30b6a6d",
"type": 7,
"dependencys": [],
"columns": []
}', true);
$this->xml = '<row id="dee2007d-1ffe-430e-b75f-4f32a30b6a6d" type="Row"/>';
}
public function testFromJson()
{
$parser = new Row(null, null, null);
$parser->fromJson($this->json);
$obj = $parser->parseJson();
$xml = ArrayToXml::convert($obj->generateXML(), 'row');
self::assertXmlStringEqualsXmlString($xml, $this->xml);
}
public function testFromXML()
{
$parser = new Row(null, null, null);
$parser->fromXML(simplexml_load_string($this->xml));
$obj = $parser->parseXML();
self::assertJsonStringEqualsJsonString(json_encode($obj->generateJson()), json_encode($this->json));
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace PSC\Library\Calc\Tests\Json\Type;
use PHPUnit\Framework\TestCase;
use PSC\Library\Calc\Option\Parser\Row;
use Spatie\ArrayToXml\ArrayToXml;
class RowWithColumnTest extends TestCase
{
private array $json;
private string $xml;
public function setUp(): void
{
$this->json = json_decode('{
"id": "dee2007d-1ffe-430e-b75f-4f32a30b6a6d",
"type": 7,
"dependencys": [],
"columns": [
{
"id": "1",
"dependencys": [],
"type": 8,
"options": []
},
{
"id": "2",
"type": 8,
"dependencys": [],
"options": []
}
]
}', true);
$this->xml = '<row id="dee2007d-1ffe-430e-b75f-4f32a30b6a6d" type="Row"><column id="1"/><column id="2"/></row>';
}
public function testFromJson()
{
$parser = new Row(null, null, null);
$parser->fromJson($this->json);
$obj = $parser->parseJson();
$xml = ArrayToXml::convert($obj->generateXML(), 'row');
self::assertXmlStringEqualsXmlString($xml, $this->xml);
}
public function testFromXML()
{
$parser = new Row(null, null, null);
$parser->fromXML(simplexml_load_string($this->xml));
$obj = $parser->parseXML();
self::assertJsonStringEqualsJsonString(json_encode($obj->generateJson()), json_encode($this->json));
}
}

View File

@ -0,0 +1,93 @@
<?php
namespace PSC\Library\Calc\Tests\Json\Type;
use PHPUnit\Framework\TestCase;
use PSC\Library\Calc\Option\Parser\Row;
use Spatie\ArrayToXml\ArrayToXml;
class RowWithColumnWithOptionsTest extends TestCase
{
private array $json;
private string $xml;
public function setUp(): void
{
$this->json = json_decode('{
"id": "dee2007d-1ffe-430e-b75f-4f32a30b6a6d",
"type": 7,
"dependencys": [],
"columns": [
{
"id": "1",
"dependencys": [],
"type": 8,
"options": [
{
"id": "auflage",
"name": "Auflage",
"default": "100",
"dependencys": [],
"placeHolder": "Placeholder",
"required": false,
"type": 2
},
{
"id": "seiten_umschlag",
"name": "Seiten Umschlag",
"default": "2",
"dependencys": [],
"placeHolder": "Placeholder",
"required": false,
"type": 2
}
]
},
{
"id": "2",
"type": 8,
"dependencys": [],
"options": [
{
"id": "farbe1",
"name": "Farbe1",
"default": "142",
"container": "pantone+-solid-uncoated",
"mode": "colordb",
"dependencys": [],
"required": true,
"options": [
{
"dependencys": [],
"id": "142",
"name": "142"
}
],
"type": 3
}
]
}
]
}', true);
$this->xml = '<row id="dee2007d-1ffe-430e-b75f-4f32a30b6a6d" type="Row"><column id="1"><option default="100" id="auflage" name="Auflage" placeholder="Placeholder" type="Input"/><option default="2" id="seiten_umschlag" name="Seiten Umschlag" placeholder="Placeholder" type="Input"/></column><column id="2"><option container="pantone+-solid-uncoated" default="142" id="farbe1" mode="colordb" name="Farbe1" require="true" type="Select"/></column></row>';
}
public function testFromJson()
{
$parser = new Row(null, null, null);
$parser->fromJson($this->json);
$obj = $parser->parseJson();
$xml = ArrayToXml::convert($obj->generateXML(), 'row');
self::assertXmlStringEqualsXmlString($xml, $this->xml);
}
public function testFromXML()
{
$parser = new Row(null, null, null);
$parser->fromXML(simplexml_load_string($this->xml));
$obj = $parser->parseXML();
self::assertJsonStringEqualsJsonString(json_encode($obj->generateJson()), json_encode($this->json));
}
}