Fixes
This commit is contained in:
parent
ff608a598e
commit
3f82f1d009
11362
cobertura.xml
11362
cobertura.xml
File diff suppressed because it is too large
Load Diff
@ -3,6 +3,7 @@
|
|||||||
namespace PSC\Library\Calc\Option;
|
namespace PSC\Library\Calc\Option;
|
||||||
|
|
||||||
use PSC\Library\Calc\Option\Parser\Checkbox;
|
use PSC\Library\Calc\Option\Parser\Checkbox;
|
||||||
|
use PSC\Library\Calc\Option\Parser\Fieldset;
|
||||||
use PSC\Library\Calc\Option\Parser\Headline;
|
use PSC\Library\Calc\Option\Parser\Headline;
|
||||||
use PSC\Library\Calc\Option\Parser\Hidden;
|
use PSC\Library\Calc\Option\Parser\Hidden;
|
||||||
use PSC\Library\Calc\Option\Parser\Input;
|
use PSC\Library\Calc\Option\Parser\Input;
|
||||||
@ -60,6 +61,9 @@ class Parser
|
|||||||
case 11:
|
case 11:
|
||||||
$obj = new Radio();
|
$obj = new Radio();
|
||||||
break;
|
break;
|
||||||
|
case 12:
|
||||||
|
$obj = new Fieldset($this->paperContainer, $this->paperRepository, $this->templates);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $obj;
|
return $obj;
|
||||||
@ -105,6 +109,9 @@ class Parser
|
|||||||
case 'media':
|
case 'media':
|
||||||
$obj = new Media();
|
$obj = new Media();
|
||||||
break;
|
break;
|
||||||
|
case 'fieldset':
|
||||||
|
$obj = new Fieldset($this->paperContainer, $this->paperRepository, $this->templates);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $obj;
|
return $obj;
|
||||||
|
|||||||
90
src/Option/Parser/Fieldset.php
Normal file
90
src/Option/Parser/Fieldset.php
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PSC\Library\Calc\Option\Parser;
|
||||||
|
|
||||||
|
use PSC\Library\Calc\Option\Parser;
|
||||||
|
use PSC\Library\Calc\Option\Parser\Base;
|
||||||
|
use PSC\Library\Calc\Option\Type\Fieldset as PSCFieldset;
|
||||||
|
use PSC\Library\Calc\Option\Type\Select;
|
||||||
|
use PSC\Library\Calc\Option\Type\Template;
|
||||||
|
|
||||||
|
class Fieldset extends Base
|
||||||
|
{
|
||||||
|
private Parser $optionParser;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private $paperContainer,
|
||||||
|
private $paperRepository,
|
||||||
|
private $templates,
|
||||||
|
) {
|
||||||
|
$this->element = new \PSC\Library\Calc\Option\Type\Fieldset();
|
||||||
|
$this->optionParser = new \PSC\Library\Calc\Option\Parser($paperContainer, $paperRepository, $templates);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function parseJson(): PSCFieldset
|
||||||
|
{
|
||||||
|
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(): PSCFieldset
|
||||||
|
{
|
||||||
|
parent::parseXML();
|
||||||
|
|
||||||
|
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) {
|
||||||
|
$obj->setPaperContainer($this->paperContainer);
|
||||||
|
$obj->setPaperRepository($this->paperRepository);
|
||||||
|
}
|
||||||
|
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->fromXML($node[0]);
|
||||||
|
$element = $obj->parseXML();
|
||||||
|
if ($default != '') {
|
||||||
|
$element->setDefault($default);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$element = $obj->parseXML();
|
||||||
|
}
|
||||||
|
$this->element->addOption($element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->element;
|
||||||
|
}
|
||||||
|
}
|
||||||
69
src/Option/Type/Fieldset.php
Normal file
69
src/Option/Type/Fieldset.php
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PSC\Library\Calc\Option\Type;
|
||||||
|
|
||||||
|
use PSC\Library\Calc\Option\Type\Base;
|
||||||
|
|
||||||
|
class Fieldset extends Base
|
||||||
|
{
|
||||||
|
private \ArrayIterator $options;
|
||||||
|
|
||||||
|
public $type = 'fieldset';
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
$this->options = new \ArrayIterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addOption(Base $option): void
|
||||||
|
{
|
||||||
|
$this->options->append($option);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getOptions(): \ArrayIterator
|
||||||
|
{
|
||||||
|
return $this->options;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getOptionById(string $id): null|Base
|
||||||
|
{
|
||||||
|
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, parent::generateXML());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generateJson(): \stdClass
|
||||||
|
{
|
||||||
|
$obj = new \stdClass();
|
||||||
|
$obj->id = $this->getId();
|
||||||
|
$obj->dependencys = [];
|
||||||
|
$obj->options = [];
|
||||||
|
$obj->type = 12;
|
||||||
|
foreach ($this->edgesCollectionContainer as $col) {
|
||||||
|
$obj->dependencys[] = $col->generateJson();
|
||||||
|
}
|
||||||
|
foreach ($this->getOptions() as $opt) {
|
||||||
|
$obj->options[] = $opt->generateJson();
|
||||||
|
}
|
||||||
|
return $obj;
|
||||||
|
}
|
||||||
|
}
|
||||||
62
tests/Json/Type/FieldsetTest.php
Normal file
62
tests/Json/Type/FieldsetTest.php
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PSC\Library\Calc\Tests\Json\Type;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PSC\Library\Calc\Option\Parser\Fieldset;
|
||||||
|
use Spatie\ArrayToXml\ArrayToXml;
|
||||||
|
|
||||||
|
class FieldsetTest extends TestCase
|
||||||
|
{
|
||||||
|
private array $json;
|
||||||
|
private string $xml;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
$this->json = json_decode('{
|
||||||
|
"id": "1",
|
||||||
|
"dependencys": [],
|
||||||
|
"type": 12,
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
|
||||||
|
]
|
||||||
|
}', true);
|
||||||
|
|
||||||
|
$this->xml = '<option type="Fieldset" 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"/></option>';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFromJson()
|
||||||
|
{
|
||||||
|
$parser = new Fieldset(null, null, null);
|
||||||
|
$parser->fromJson($this->json);
|
||||||
|
$obj = $parser->parseJson();
|
||||||
|
$xml = ArrayToXml::convert($obj->generateXML(), 'option');
|
||||||
|
self::assertXmlStringEqualsXmlString($xml, $this->xml);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFromXML()
|
||||||
|
{
|
||||||
|
$parser = new Fieldset(null, null, null);
|
||||||
|
$parser->fromXML(simplexml_load_string($this->xml));
|
||||||
|
$obj = $parser->parseXML();
|
||||||
|
self::assertJsonStringEqualsJsonString(json_encode($obj->generateJson()), json_encode($this->json));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user