From 4a2fed26dee7719af1a7c853dcae8f4e2e73d8f7 Mon Sep 17 00:00:00 2001 From: Thomas Peterson Date: Fri, 18 Jul 2025 20:30:41 +0200 Subject: [PATCH] Fixes --- cobertura.xml | 2394 ++++++++++++++++---------------- src/Calc/Valid.php | 162 ++- src/Option/Parser/Select.php | 6 + src/Option/Type/Base.php | 46 +- src/Option/Type/Select.php | 23 +- tests/Calc/ColorDBTest.php | 14 + tests/Customer/GG/CalcTest.php | 21 +- tests/Json/FromJsonTest.php | 18 +- tests/Json/from.json | 298 +++- tests/Json/from.xml | 8 +- tests/Json/from1.xml | 3 + 11 files changed, 1714 insertions(+), 1279 deletions(-) diff --git a/cobertura.xml b/cobertura.xml index e64e903..0bb78e2 100644 --- a/cobertura.xml +++ b/cobertura.xml @@ -1,6 +1,6 @@ - + /home/thomas/projekte/calc/src @@ -11,11 +11,11 @@ - - - - - + + + + + @@ -30,7 +30,7 @@ - + @@ -53,7 +53,7 @@ - + @@ -63,12 +63,12 @@ - + - + @@ -78,33 +78,33 @@ - + - + - + - - + + - + - - + + - + @@ -114,7 +114,7 @@ - + @@ -162,42 +162,42 @@ - - - - - + + + + + - + - + - - + + - - + + - - - + + + - + - - + + - + - + @@ -273,32 +273,32 @@ - - - + + + - + - - + + - - - - - - - - - - + + + + + + + + + + @@ -307,22 +307,22 @@ - - - - - - - + + + + + + + - - - - - + + + + + @@ -443,7 +443,7 @@ - + @@ -469,24 +469,24 @@ - - - - - - + + + + + + - - - - - - - - - - + + + + + + + + + + @@ -495,18 +495,18 @@ - - - - - - - - - - - - + + + + + + + + + + + + @@ -627,7 +627,7 @@ - + @@ -651,44 +651,44 @@ - - - + + + - + - - + + - - - - - - - - - - + + + + + + + + + + - - - - - - + + + + + + - + @@ -1018,37 +1018,37 @@ - + - - - - - - + + + + + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - + @@ -1378,7 +1378,7 @@ - + @@ -1443,8 +1443,8 @@ - - + + @@ -1545,8 +1545,8 @@ - - + + @@ -1627,44 +1627,45 @@ - + - + - - - + + + - + - + - - + + - + - + + - - - - - - - + + + + + + + @@ -1677,133 +1678,143 @@ - - - - - - - - - - - + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - + + - - - + + - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - + + + + + + + - - - - - - + + + + + + - + - + + - - - - - - - + + + + + + + @@ -1816,97 +1827,106 @@ - - - - - - - - - - - + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -2061,30 +2081,30 @@ - - - - - - + + + + + + - - - - - + + + + + - - - + + + - - + + @@ -2094,9 +2114,9 @@ - - - + + + @@ -2129,7 +2149,7 @@ - + @@ -2140,7 +2160,7 @@ - + @@ -2151,38 +2171,38 @@ - + - - + + - - - - - - - - - - - + + + + + + + + + + + - - - + + + - - - - + + + + @@ -2192,54 +2212,54 @@ - - + + - - - - - - - - - + + + + + + + + + - - - - - - - - + + + + + + + + - - + + - - - - - + + + + + - - - - + + + + @@ -2342,13 +2362,13 @@ - + - - + + @@ -2368,9 +2388,9 @@ - + - + @@ -2460,11 +2480,11 @@ - - - - - + + + + + @@ -2474,28 +2494,28 @@ - - + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + @@ -2525,30 +2545,30 @@ - - - - - - - - - - - + + + + + + + + + + + - - - + + + - - + + - - - + + + @@ -2561,36 +2581,36 @@ - + - + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - - + + + - - - - + + + + @@ -2600,50 +2620,50 @@ - - + + - - - - - - - - - + + + + + + + + + - - - - - - - - - - + + + + + + + + + + - - - - - + + + + + - - - - + + + + @@ -2678,15 +2698,15 @@ - - - + + + - + - + @@ -2712,11 +2732,11 @@ - - - - - + + + + + @@ -2726,24 +2746,24 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + @@ -3788,8 +3808,8 @@ - - + + @@ -3863,8 +3883,8 @@ - - + + @@ -3920,8 +3940,8 @@ - - + + @@ -4166,8 +4186,8 @@ - - + + @@ -4424,9 +4444,9 @@ - - - + + + @@ -4436,12 +4456,12 @@ - + - + @@ -4518,12 +4538,12 @@ - - - + + + - - + + @@ -4586,7 +4606,7 @@ - + @@ -4728,17 +4748,17 @@ - + - + - + @@ -4852,8 +4872,8 @@ - - + + @@ -4894,7 +4914,7 @@ - + @@ -5054,7 +5074,7 @@ - + @@ -5158,10 +5178,10 @@ - - - - + + + + @@ -5176,15 +5196,15 @@ - - + + - + - - - - + + + + @@ -5233,21 +5253,21 @@ - - - - + + + + - - + + - + - - - - + + + + @@ -5283,7 +5303,7 @@ - + @@ -5293,15 +5313,15 @@ - + - + - + @@ -5320,7 +5340,7 @@ - + @@ -5332,9 +5352,9 @@ - - - + + + @@ -5358,14 +5378,14 @@ - - - + + + - - - + + + @@ -5390,12 +5410,12 @@ - + - + @@ -5403,9 +5423,9 @@ - - - + + + @@ -5425,14 +5445,14 @@ - - - + + + - - - + + + @@ -5457,7 +5477,7 @@ - + @@ -5468,12 +5488,12 @@ - + - + @@ -5508,42 +5528,42 @@ - - - - - - - + + + + + + + - + - + - + - + - + - + - + - + - - + + - - + + @@ -5558,31 +5578,31 @@ - - - - - - - + + + + + + + - + - + - + - + - + - + - + - - - + + + @@ -6116,60 +6136,60 @@ - + - + - - + + - + - - + + - + - - + + - - + + - - + + - - - + + + - + - - + + - - + + - - + + - - - + + + - + @@ -6226,11 +6246,11 @@ - + - - - + + + @@ -6250,25 +6270,24 @@ - - - + + + + + - + - - - - - - + + + + + - - @@ -6278,90 +6297,95 @@ - - + + + - + + + + + - - - - + + + + - - - - + + + + - + - + - + - + - - + + - + - - + + - - + + - - + + - - + + - - - + + + - - - + + + - - + + - - + + - - - + + + - + @@ -6400,9 +6424,9 @@ - - - + + + @@ -6422,21 +6446,20 @@ - - - - - - - - - + + + + + + + + + + - - @@ -6446,22 +6469,27 @@ - - + + + - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + @@ -6716,231 +6744,229 @@ - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + - - - - - - - + + + + + + + + - - @@ -6949,82 +6975,82 @@ + - - + - - - + + + - - - + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + - - - - - - - - - + + + + + + + + @@ -7033,17 +7059,19 @@ + - - + - - - + + + - - - + + + + + @@ -7160,46 +7188,52 @@ - + - + - + - + - + - - - - + + + + + + - + - - - + + + + - - - - - - - - - + + + + + + + + + + + + @@ -7482,21 +7516,19 @@ - + - + - - - - + + + - @@ -7506,27 +7538,28 @@ + - - - - + + + + + + - - - - + + + - @@ -7540,8 +7573,8 @@ + - @@ -7556,13 +7589,15 @@ + - + + @@ -7612,16 +7647,17 @@ - + - + + @@ -7629,9 +7665,11 @@ + + @@ -7639,13 +7677,11 @@ - - - - + + + - @@ -7655,23 +7691,24 @@ + - - - - - - - - + + + + + + + + + - @@ -7685,8 +7722,8 @@ + - @@ -7701,13 +7738,15 @@ + - + + @@ -7757,14 +7796,17 @@ - - + + + + + @@ -7948,15 +7990,15 @@ - + - + - - - + + + @@ -7964,192 +8006,214 @@ + + + + + - + - - - - + + + + + + + + + + + - - + + - + - + - + - + - + + + + + + - + - + - + - - - - - - - + + + + + + + - - - - - + + + + + - + - - - - - - - - - - - - - + + + + + - - - - - - + + + + + + + + + + + + + + - + - - - - - - - - - - - + + + + + + + + + + + - - - + + + - - - - - - - - - + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + @@ -8357,7 +8421,7 @@ - + @@ -8367,7 +8431,7 @@ - + @@ -8377,7 +8441,7 @@ - + @@ -8387,7 +8451,7 @@ - + @@ -8415,25 +8479,25 @@ - - - - - + + + + + - + - + - + - + - + @@ -8445,13 +8509,13 @@ - - - - - + + + + + - + @@ -8622,7 +8686,7 @@ - + @@ -8647,7 +8711,7 @@ - + @@ -8714,7 +8778,7 @@ - + @@ -8726,8 +8790,8 @@ - - + + @@ -8743,11 +8807,11 @@ - - - - - + + + + + @@ -8755,18 +8819,18 @@ - + - - - - - - - - - + + + + + + + + + @@ -8775,34 +8839,34 @@ - - - + + + - + - + - + - + - + @@ -8812,7 +8876,7 @@ - + @@ -8820,8 +8884,8 @@ - - + + @@ -8833,11 +8897,11 @@ - - - - - + + + + + @@ -8845,18 +8909,18 @@ - + - - - - - - - - - + + + + + + + + + @@ -8865,18 +8929,18 @@ - - - + + + - - - - - + + + + + @@ -9062,7 +9126,7 @@ - + @@ -9077,7 +9141,7 @@ - + diff --git a/src/Calc/Valid.php b/src/Calc/Valid.php index 1355a1a..093ab76 100644 --- a/src/Calc/Valid.php +++ b/src/Calc/Valid.php @@ -1,4 +1,5 @@ getColumns() as $col) { + foreach ($options as $option) { + if ($option instanceof Row) { + foreach ($option->getColumns() as $col) { $this->validRec($col->getOptions(), $withFormel); - } + } } - if($option instanceof Input) { - if($option->getMaxValue() && $option->getRawValue() > $option->getMaxValue()) { + if ($option instanceof Input) { + if ($option->getMaxValue() && $option->getRawValue() > $option->getMaxValue()) { $option->addValidationError(new Max(intval($option->getRawValue()), $option->getMaxValue())); } - if($option->getMinValue() && $option->getRawValue() < $option->getMinValue()) { + if ($option->getMinValue() && $option->getRawValue() < $option->getMinValue()) { $option->addValidationError(new Min(intval($option->getRawValue()), $option->getMinValue())); } } - if(($option instanceof Select || $option instanceof Checkbox) && !($option instanceOf ColorDBSelect)) { - + if (($option instanceof Select || $option instanceof Checkbox) && !($option instanceof ColorDBSelect)) { $valid = false; $nextShouldBeValid = false; $isDefaultValid = true; $option->clearSelected(); /** @var Select\Opt $opt */ foreach ($option->getOptions() as $opt) { - if(count($opt->getEdgesCollectionContainer()) > 0) { + if (count($opt->getEdgesCollectionContainer()) > 0) { $opt->setIsValid(false); } $groupsValid = []; /** @var EdgeCollection $collection */ - foreach($opt->getEdgesCollectionContainer() as $collection) { - if(!$withFormel && trim($collection->getFormel()) != "") continue; + foreach ($opt->getEdgesCollectionContainer() as $collection) { + if (!$withFormel && trim($collection->getFormel()) != '') + continue; $groupsValid[$collection->getName()] = false; - if(trim($collection->getFormel()) != "") { + if (trim($collection->getFormel()) != '') { $value = $this->formelCalc->parse($collection->getFormel(), true); eval('$value = ' . $value . ';'); /** @var Edge $edge */ @@ -85,7 +85,7 @@ class Valid $groupsValid[$collection->getName()] = true; } } - }else { + } else { /** @var Edge $edge */ foreach ($collection as $edge) { if ($this->edgeIsValid($collection->getName(), $edge)) { @@ -96,44 +96,57 @@ class Valid } } - if(!in_array(false, $groupsValid)) { + if (!in_array(false, $groupsValid)) { $opt->setIsValid(true); } - if(!$opt->isValid() && $opt->getId() == $option->getDefault() && $option->getSelectedOptions()->count() == 0) { + if ( + !$opt->isValid() && + $opt->getId() == $option->getDefault() && + $option->getSelectedOptions()->count() == 0 + ) { $isDefaultValid = false; } - if($option->getDefault() === null && $opt->isValid()) { + if ($option->getDefault() === null && $opt->isValid()) { $option->setDefault($opt->getId()); - if(!$this->engine->hasVariable($option->getId())) { + if (!$this->engine->hasVariable($option->getId())) { $this->engine->setVariable($option->getId(), $opt->getId()); } } - if($option->getDefault() !== null && $opt->isValid() && !$isDefaultValid) { + if ($option->getDefault() !== null && $opt->isValid() && !$isDefaultValid) { $option->setDefault($opt->getId()); $this->engine->setVariable($option->getId(), $opt->getId()); - if(isset($this->engine->validVars[$option->getId()]) && $opt->getId() != $this->engine->validVars[$option->getId()]) { + if ( + isset($this->engine->validVars[$option->getId()]) && + $opt->getId() != $this->engine->validVars[$option->getId()] + ) { $this->engine->validDirty = true; } $isDefaultValid = true; } - if($option instanceof Checkbox) { - if (isset($this->engine->getVariables()[$option->getId()]) && is_array($this->engine->getVariables()[$option->getId()]) && in_array($opt->getId(), $this->engine->getVariables()[$option->getId()])) { + if ($option instanceof Checkbox) { + if ( + isset($this->engine->getVariables()[$option->getId()]) && + is_array($this->engine->getVariables()[$option->getId()]) && + in_array($opt->getId(), $this->engine->getVariables()[$option->getId()]) + ) { $option->addSelectedOption($opt); } - }else { - if (isset($this->engine->getVariables()[$option->getId()]) && $this->engine->getVariables()[$option->getId()] == $opt->getId()) { - if(!$opt->isValid()) { + } else { + if ( + isset($this->engine->getVariables()[$option->getId()]) && + $this->engine->getVariables()[$option->getId()] == $opt->getId() + ) { + if (!$opt->isValid()) { $nextShouldBeValid = true; } else { $option->addSelectedOption($opt); } - } else { - if($nextShouldBeValid && $opt->isValid()) { + if ($nextShouldBeValid && $opt->isValid()) { $this->engine->getVariables()[$option->getId()] = $opt->getId(); $option->addSelectedOption($opt); $nextShouldBeValid = false; @@ -141,14 +154,14 @@ class Valid } } - if($opt->isValid() && !$valid) { + if ($opt->isValid() && !$valid) { $valid = true; } } - if($nextShouldBeValid && $option->getSelectedOptions()->count() === 0) { - foreach($option->getOptions() as $opt) { - if($opt->isValid()) { + if ($nextShouldBeValid && $option->getSelectedOptions()->count() === 0) { + foreach ($option->getOptions() as $opt) { + if ($opt->isValid()) { $option->addSelectedOption($opt); break; } @@ -157,54 +170,51 @@ class Valid $option->processValue(); $option->setIsValid($valid); - } - if($option instanceof ColorDBSelect) { + if ($option instanceof ColorDBSelect) { if (isset($this->engine->getVariables()[$option->getId()])) { - $element = array_find((array)$option->getOptions(), function(ColorOpt $o1) use ($option) { + $element = array_find((array) $option->getOptions(), function (ColorOpt $o1) use ($option) { return $o1->getId() === $this->engine->getVariables()[$option->getId()]; }); - if($element) { + if ($element) { $option->addSelectedOption($element); - }else{ + } else { $option->addSelectedOption($option->getOptions()[0]); } - }elseif($option->getDefault() != null) { - $element = array_find((array)$option->getOptions(), function(ColorOpt $o1) use ($option) { + } elseif ($option->getDefault() != null) { + $element = array_find((array) $option->getOptions(), function (ColorOpt $o1) use ($option) { return $o1->getId() === $option->getDefault(); }); $option->addSelectedOption($element); - }else{ + } else { $option->addSelectedOption($option->getOptions()[0]); } } $mainEdges = true; - foreach($option->getEdgesCollectionContainer() as $collection) { + foreach ($option->getEdgesCollectionContainer() as $collection) { /** @var Edge $edge */ $collValid = false; - foreach($collection as $edge) { - if(!$collValid && $this->edgeIsValid($collection->getName(), $edge)) { - $collValid = true; + foreach ($collection as $edge) { + if (!$collValid && $this->edgeIsValid($collection->getName(), $edge)) { + $collValid = true; } } - if(!$collValid) { + if (!$collValid) { $mainEdges = false; break; - } + } } - if(!$mainEdges && $option->isValid()) { + if (!$mainEdges && $option->isValid()) { $option->setIsValid(false); } $this->engine->setVariables($option->parseAdditionalValues($this->engine->getVariables())); - $this->engine->addCalcVariable($option->getId() . '_valid', (int)$option->isValid()); + $this->engine->addCalcVariable($option->getId() . '_valid', (int) $option->isValid()); } - - } private function edgeIsValid(mixed $section, Edge $edge): bool @@ -212,37 +222,49 @@ class Valid $valid = false; $skipTests = false; - if(!isset($this->engine->getVariables()[$section])) { + if (!isset($this->engine->getVariables()[$section])) { $skipTests = true; } - if(!$skipTests && !$valid && $edge->isRegion() && - $edge->getFrom() <= $this->engine->getVariables()[$section] && - $edge->getTo() >= $this->engine->getVariables()[$section]) { + if ( + !$skipTests && + !$valid && + $edge->isRegion() && + $edge->getFrom() <= $this->engine->getVariables()[$section] && + $edge->getTo() >= $this->engine->getVariables()[$section] + ) { $valid = true; } - if(!$skipTests && !$valid && $edge->isRegion() && - $edge->getFrom() <= $this->engine->getVariables()[$section] && - $edge->getTo() == 0) { + if ( + !$skipTests && + !$valid && + $edge->isRegion() && + $edge->getFrom() <= $this->engine->getVariables()[$section] && + $edge->getTo() == 0 + ) { $valid = true; } - if(!$skipTests && !$valid && !$edge->isRegion() && in_array($this->engine->getVariables()[$section], $edge->getValues())) { - $valid = true; + if ( + !$skipTests && + !$valid && + !$edge->isRegion() && + in_array($this->engine->getVariables()[$section], $edge->getValues()) + ) { + $valid = true; } - if(!$skipTests && $valid && $edge->getEdgesCollectionContainer()->count()) { + if (!$skipTests && $valid && $edge->getEdgesCollectionContainer()->count()) { $valid = false; /** @var EdgeCollection $collection */ - foreach($edge->getEdgesCollectionContainer() as $collection) { - foreach($collection as $subEdge) { - if(!$valid) { + foreach ($edge->getEdgesCollectionContainer() as $collection) { + foreach ($collection as $subEdge) { + if (!$valid) { $valid = $this->edgeIsValid($collection->getName(), $subEdge); } } } - } return $valid; @@ -250,19 +272,15 @@ class Valid private function edgeIsValidWithValue(mixed $value, Edge $edge): bool { - if($edge->isRegion() && - $edge->getFrom() <= $value && - $edge->getTo() >= $value) { + if ($edge->isRegion() && $edge->getFrom() <= $value && $edge->getTo() >= $value) { return true; } - if($edge->isRegion() && - $edge->getFrom() <= $value && - $edge->getTo() == 0) { + if ($edge->isRegion() && $edge->getFrom() <= $value && $edge->getTo() == 0) { return true; } - if(!$edge->isRegion() && in_array($value, $edge->getValues())) { + if (!$edge->isRegion() && in_array($value, $edge->getValues())) { return true; } diff --git a/src/Option/Parser/Select.php b/src/Option/Parser/Select.php index e923d67..2125eea 100644 --- a/src/Option/Parser/Select.php +++ b/src/Option/Parser/Select.php @@ -215,6 +215,9 @@ class Select extends Base $this->element->setColorSystem($colorSystem); $this->element->addOptions($value); + if ($this->element->getDefault() != '') { + $this->element->addSelectedOption($this->element->getOptionById($this->element->getDefault())); + } } private function parseModeColorDbJson(): void @@ -256,6 +259,9 @@ class Select extends Base $this->element->setColorSystem($colorSystem); $this->element->addOptions($value); + if ($this->element->getDefault() != '') { + $this->element->addSelectedOption($this->element->getOptionById($this->element->getDefault())); + } } private function parseModeNormalXML() diff --git a/src/Option/Type/Base.php b/src/Option/Type/Base.php index 1b43d6a..868441d 100644 --- a/src/Option/Type/Base.php +++ b/src/Option/Type/Base.php @@ -1,4 +1,5 @@ default = $default; } - public function getHelp(): ?string + public function getHelp(): null|string { return $this->help; } @@ -175,7 +176,7 @@ class Base $this->helpId = $var; } - public function getHelpId(): ?string + public function getHelpId(): null|string { return $this->helpId; } @@ -185,7 +186,7 @@ class Base $this->helpLink = $var; } - public function getHelpLink(): ?string + public function getHelpLink(): null|string { return $this->helpLink; } @@ -201,7 +202,7 @@ class Base /** * @param string $rawValue */ - public function setRawValue($rawValue) + public function setRawValue(mixed $rawValue): void { $this->rawValue = $rawValue; } @@ -331,12 +332,13 @@ class Base $this->amount = $amount; } - public function toArray() : array { + public function toArray(): array + { return [ 'id' => $this->getId(), 'name' => $this->getName(), 'type' => $this->getType(), - 'valid' => $this->isValid() + 'valid' => $this->isValid(), ]; } @@ -345,18 +347,18 @@ class Base $obj = new \stdClass(); $obj->id = $this->getId(); $obj->name = $this->getName(); - if($this->default !== null) { + if ($this->default !== null) { $obj->default = $this->default; } $obj->dependencys = []; - if($this->isAjaxExport()) { + if ($this->isAjaxExport()) { $obj->exportAjax = $this->isAjaxExport(); } - if($this->isDisplayOnly()) { + if ($this->isDisplayOnly()) { $obj->displayOnly = $this->isDisplayOnly(); } - foreach($this->edgesCollectionContainer as $col) { + foreach ($this->edgesCollectionContainer as $col) { $obj->dependencys[] = $col->generateJson(); } @@ -369,24 +371,24 @@ class Base '_attributes' => [ 'id' => $this->getId(), 'type' => ucfirst($this->getType()), - ] + ], ]; - if($this->name != "") { + if ($this->name != '') { $tmp = array_merge_recursive($tmp, ['_attributes' => [ - 'name' => $this->name + 'name' => $this->name, ]]); } - if($this->isDisplayOnly()) { + if ($this->isDisplayOnly()) { $tmp = array_merge_recursive($tmp, ['_attributes' => [ - 'displayOnly' => $this->isDisplayOnly()?'1':'0' + 'displayOnly' => $this->isDisplayOnly() ? '1' : '0', ]]); } - if($this->isAjaxExport()) { + if ($this->isAjaxExport()) { $tmp = array_merge_recursive($tmp, ['_attributes' => [ - 'exportAjax' => $this->isAjaxExport()?'1':'0' + 'exportAjax' => $this->isAjaxExport() ? '1' : '0', ]]); } - foreach($this->edgesCollectionContainer as $col) { + foreach ($this->edgesCollectionContainer as $col) { $tmp[$col->getName()] = $col->generateXML(); } diff --git a/src/Option/Type/Select.php b/src/Option/Type/Select.php index 956c3cb..390e298 100644 --- a/src/Option/Type/Select.php +++ b/src/Option/Type/Select.php @@ -39,6 +39,11 @@ class Select extends Base $this->options->append($option); } + public function getOptionById(string $id): null|Opt + { + return array_find((array) $this->options, fn($item) => $item->getId() == $id); + } + public function addOptions(array $options) { $this->options = new \ArrayIterator($options); @@ -56,6 +61,14 @@ class Select extends Base } } + public function setRawValue(mixed $value): void + { + parent::setRawValue($value); + if ($this->getSelectedOption() === null || $this->getSelectedOption()->getId() != $value) { + $this->addSelectedOption($this->getOptionById($value)); + } + } + public function getValue() { $this->processValue(); @@ -88,14 +101,16 @@ class Select extends Base $this->container = $container; } - /** - * @return \ArrayIterator - */ - public function getOptions() + public function getOptions(): \ArrayIterator { return $this->options; } + public function clearOptions(): void + { + $this->options = []; + } + public function clearSelected(): void { $this->selectedOptions = new \ArrayIterator(); diff --git a/tests/Calc/ColorDBTest.php b/tests/Calc/ColorDBTest.php index ec8463f..17e8659 100644 --- a/tests/Calc/ColorDBTest.php +++ b/tests/Calc/ColorDBTest.php @@ -1,4 +1,5 @@ assertSame('78', $option->getSelectedOption()->getId()); } + public function testSetVar() + { + $this->engine->setVariable('colorpantone', '128'); + $this->engine->calc(); + + $this->assertSame( + '128', + $this->engine + ->getArticle() + ->getOptionById('colorpantone') + ->getRawValue(), + ); + } } diff --git a/tests/Customer/GG/CalcTest.php b/tests/Customer/GG/CalcTest.php index d154496..8d5f88c 100644 --- a/tests/Customer/GG/CalcTest.php +++ b/tests/Customer/GG/CalcTest.php @@ -1,4 +1,5 @@ engine->setTemplates(file_get_contents(__DIR__ . '/calcTemplates.xml')); $this->engine->loadString(file_get_contents(__DIR__ . '/calc.xml')); - - } public function tearDown(): void @@ -52,8 +50,19 @@ class CalcTest extends TestCase $this->engine->setVariable('farbe_2_v_wert_pantone', '7555'); $this->engine->calc(); $this->assertSame(175.75, $this->engine->getPrice()); - $this->assertSame($this->engine->getArticle()->getOptionById('farbe_1_v_wert_pantone')->getRawValue(), '128'); - $this->assertSame($this->engine->getArticle()->getOptionById('farbe_2_v_wert_pantone')->getRawValue(), '7555'); + $this->assertSame( + $this->engine + ->getArticle() + ->getOptionById('farbe_1_v_wert_pantone') + ->getRawValue(), + '128', + ); + $this->assertSame( + $this->engine + ->getArticle() + ->getOptionById('farbe_2_v_wert_pantone') + ->getRawValue(), + '7555', + ); } - } diff --git a/tests/Json/FromJsonTest.php b/tests/Json/FromJsonTest.php index 45ce4bd..78db7d0 100644 --- a/tests/Json/FromJsonTest.php +++ b/tests/Json/FromJsonTest.php @@ -1,4 +1,5 @@ loadJson(file_get_contents(__DIR__ .'/from.json'))); + self::assertTrue($engine1->loadJson(file_get_contents(__DIR__ . '/from.json'))); self::assertCount(1, $engine1->getArticles()); self::assertInstanceOf(Input::class, $engine1->getArticle()->getOptionById('auflage')); $engine2 = new Engine(); - self::assertTrue($engine2->loadString(file_get_contents(__DIR__ .'/from.xml'))); + self::assertTrue($engine2->loadString(file_get_contents(__DIR__ . '/from.xml'))); self::assertCount(1, $engine2->getArticles()); self::assertInstanceOf(Input::class, $engine2->getArticle()->getOptionById('auflage')); self::assertSame(1011.42, $engine1->getPrice()); self::assertSame($engine1->getPrice(), $engine2->getPrice()); - + self::assertEqualsCanonicalizing( + $engine1->getArticle()->getOptionById('farbe1')->getSelectedOption(), + $engine2->getArticle()->getOptionById('farbe1')->getSelectedOption(), + ); self::assertEqualsCanonicalizing($engine1->getArticle(), $engine2->getArticle()); $tempXML = tempnam(sys_get_temp_dir(), 'calc'); $tempJson = tempnam(sys_get_temp_dir(), 'calc'); file_put_contents($tempXML, $engine1->generateXML()); file_put_contents($tempJson, $engine2->generateJson()); - self::assertJsonFileEqualsJsonFile(__DIR__.'/from.json', $tempJson); - self::assertXmlFileEqualsXmlFile(__DIR__.'/from.xml', $tempXML); + self::assertJsonFileEqualsJsonFile(__DIR__ . '/from.json', $tempJson); + self::assertXmlFileEqualsXmlFile(__DIR__ . '/from.xml', $tempXML); self::assertCount(5, $engine1->getArticle()->getOptionById('farbigkeit')->getOptions()); self::assertCount(5, $engine2->getArticle()->getOptionById('farbigkeit')->getOptions()); - @unlink($tempXML); - @unlink($tempJson); + unlink($tempXML); + unlink($tempJson); } } diff --git a/tests/Json/from.json b/tests/Json/from.json index 7a80898..f8084a9 100644 --- a/tests/Json/from.json +++ b/tests/Json/from.json @@ -1 +1,297 @@ -[{"uuid":"df2df718-b28e-482d-bf0c-67d246f05d32","name":"Test Artikel","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":"seiten_anzahl_inhalt","name":"Seiten Anzahl Inhalt","default":"10","dependencys":[{"relation":"auflage","formula":"","borders":[{"calcValue":"","calcValue1":"","calcValue2":"","calcValue3":"","calcValue4":"","calcValue5":"","calcValue6":"","calcValue7":"","calcValue8":"","calcValue9":"","calcValue10":"","flatRate":"","formula":"$Vauflage$V*0.12","price":0,"value":"1-10","dependencys":[{"relation":"seiten_umschlag","formula":"","borders":[{"calcValue":"","calcValue1":"","calcValue2":"","calcValue3":"","calcValue4":"","calcValue5":"","calcValue6":"","calcValue7":"","calcValue8":"","calcValue9":"","calcValue10":"","flatRate":"","formula":"$Vseiten_umschlag$V*0.24","price":0,"value":"1-2","dependencys":[]},{"calcValue":"","calcValue1":"","calcValue2":"","calcValue3":"","calcValue4":"","calcValue5":"","calcValue6":"","calcValue7":"","calcValue8":"","calcValue9":"","calcValue10":"","flatRate":"","formula":"$Vseiten_umschlag$V*0.23","price":0,"value":"3-","dependencys":[]}]}]},{"calcValue":"","calcValue1":"","calcValue2":"","calcValue3":"","calcValue4":"","calcValue5":"","calcValue6":"","calcValue7":"","calcValue8":"","calcValue9":"","calcValue10":"","flatRate":"","formula":"$Vauflage$V*0.11","price":0,"value":"11-","dependencys":[{"relation":"seiten_umschlag","formula":"","borders":[{"calcValue":"","calcValue1":"","calcValue2":"","calcValue3":"","calcValue4":"","calcValue5":"","calcValue6":"","calcValue7":"","calcValue8":"","calcValue9":"","calcValue10":"","flatRate":"","formula":"$Vseiten_umschlag$V*0.21","price":0,"value":"1-2","dependencys":[]},{"calcValue":"","calcValue1":"","calcValue2":"","calcValue3":"","calcValue4":"","calcValue5":"","calcValue6":"","calcValue7":"","calcValue8":"","calcValue9":"","calcValue10":"","flatRate":"","formula":"$Vseiten_umschlag$V*0.20","price":0,"value":"3-","dependencys":[]}]}]}]}],"placeHolder":"Placeholder","required":true,"type":2},{"id":"farbigkeit","name":"Farbigkeit","default":"10","dependencys":[],"type":3,"options":[{"id":"10","name":"1\/0 farbig","dependencys":[{"relation":"auflage","formula":"","borders":[{"calcValue":"","calcValue1":"","calcValue2":"","calcValue3":"","calcValue4":"","calcValue5":"","calcValue6":"","calcValue7":"","calcValue8":"","calcValue9":"","calcValue10":"","flatRate":"","formula":"","price":0,"value":"1-101","dependencys":[]}]}]},{"id":"11","name":"1\/1 farbig","dependencys":[]},{"id":"20","name":"2\/0 farbig","dependencys":[]},{"id":"21","name":"2\/1 farbig","dependencys":[]},{"id":"22","name":"2\/2 farbig","dependencys":[{"relation":"auflage","formula":"","borders":[{"calcValue":"","calcValue1":"","calcValue2":"","calcValue3":"","calcValue4":"","calcValue5":"","calcValue6":"","calcValue7":"","calcValue8":"","calcValue9":"","calcValue10":"","flatRate":"","formula":"","price":0,"value":"11-50","dependencys":[]}]}]}],"mode":"normal"},{"id":"calc","name":"calc","dependencys":[{"relation":"auflage","formula":"","borders":[{"calcValue":"","calcValue1":"","calcValue2":"","calcValue3":"","calcValue4":"","calcValue5":"","calcValue6":"","calcValue7":"","calcValue8":"","calcValue9":"","calcValue10":"","flatRate":"","formula":"$Vauflage$V*$Vseiten_anzahl_inhalt$V","price":0,"value":"1-","dependencys":[]}]}],"type":1}]}] \ No newline at end of file +[ + { + "uuid": "df2df718-b28e-482d-bf0c-67d246f05d32", + "name": "Test Artikel", + "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": "farbe1", + "name": "Farbe1", + "default": "142", + "container": "pantone+-solid-uncoated", + "mode": "colordb", + "dependencys": [], + "required": true, + "options": [ + { + "dependencys": [], + "id": "142", + "name": "142" + } + ], + "type": 3 + }, + { + "id": "seiten_anzahl_inhalt", + "name": "Seiten Anzahl Inhalt", + "default": "10", + "dependencys": [ + { + "relation": "auflage", + "formula": "", + "borders": [ + { + "calcValue": "", + "calcValue1": "", + "calcValue2": "", + "calcValue3": "", + "calcValue4": "", + "calcValue5": "", + "calcValue6": "", + "calcValue7": "", + "calcValue8": "", + "calcValue9": "", + "calcValue10": "", + "flatRate": "", + "formula": "$Vauflage$V*0.12", + "price": 0, + "value": "1-10", + "dependencys": [ + { + "relation": "seiten_umschlag", + "formula": "", + "borders": [ + { + "calcValue": "", + "calcValue1": "", + "calcValue2": "", + "calcValue3": "", + "calcValue4": "", + "calcValue5": "", + "calcValue6": "", + "calcValue7": "", + "calcValue8": "", + "calcValue9": "", + "calcValue10": "", + "flatRate": "", + "formula": "$Vseiten_umschlag$V*0.24", + "price": 0, + "value": "1-2", + "dependencys": [] + }, + { + "calcValue": "", + "calcValue1": "", + "calcValue2": "", + "calcValue3": "", + "calcValue4": "", + "calcValue5": "", + "calcValue6": "", + "calcValue7": "", + "calcValue8": "", + "calcValue9": "", + "calcValue10": "", + "flatRate": "", + "formula": "$Vseiten_umschlag$V*0.23", + "price": 0, + "value": "3-", + "dependencys": [] + } + ] + } + ] + }, + { + "calcValue": "", + "calcValue1": "", + "calcValue2": "", + "calcValue3": "", + "calcValue4": "", + "calcValue5": "", + "calcValue6": "", + "calcValue7": "", + "calcValue8": "", + "calcValue9": "", + "calcValue10": "", + "flatRate": "", + "formula": "$Vauflage$V*0.11", + "price": 0, + "value": "11-", + "dependencys": [ + { + "relation": "seiten_umschlag", + "formula": "", + "borders": [ + { + "calcValue": "", + "calcValue1": "", + "calcValue2": "", + "calcValue3": "", + "calcValue4": "", + "calcValue5": "", + "calcValue6": "", + "calcValue7": "", + "calcValue8": "", + "calcValue9": "", + "calcValue10": "", + "flatRate": "", + "formula": "$Vseiten_umschlag$V*0.21", + "price": 0, + "value": "1-2", + "dependencys": [] + }, + { + "calcValue": "", + "calcValue1": "", + "calcValue2": "", + "calcValue3": "", + "calcValue4": "", + "calcValue5": "", + "calcValue6": "", + "calcValue7": "", + "calcValue8": "", + "calcValue9": "", + "calcValue10": "", + "flatRate": "", + "formula": "$Vseiten_umschlag$V*0.20", + "price": 0, + "value": "3-", + "dependencys": [] + } + ] + } + ] + } + ] + } + ], + "placeHolder": "Placeholder", + "required": true, + "type": 2 + }, + { + "id": "farbigkeit", + "name": "Farbigkeit", + "default": "10", + "dependencys": [], + "type": 3, + "options": [ + { + "id": "10", + "name": "1\/0 farbig", + "dependencys": [ + { + "relation": "auflage", + "formula": "", + "borders": [ + { + "calcValue": "", + "calcValue1": "", + "calcValue2": "", + "calcValue3": "", + "calcValue4": "", + "calcValue5": "", + "calcValue6": "", + "calcValue7": "", + "calcValue8": "", + "calcValue9": "", + "calcValue10": "", + "flatRate": "", + "formula": "", + "price": 0, + "value": "1-101", + "dependencys": [] + } + ] + } + ] + }, + { + "id": "11", + "name": "1\/1 farbig", + "dependencys": [] + }, + { + "id": "20", + "name": "2\/0 farbig", + "dependencys": [] + }, + { + "id": "21", + "name": "2\/1 farbig", + "dependencys": [] + }, + { + "id": "22", + "name": "2\/2 farbig", + "dependencys": [ + { + "relation": "auflage", + "formula": "", + "borders": [ + { + "calcValue": "", + "calcValue1": "", + "calcValue2": "", + "calcValue3": "", + "calcValue4": "", + "calcValue5": "", + "calcValue6": "", + "calcValue7": "", + "calcValue8": "", + "calcValue9": "", + "calcValue10": "", + "flatRate": "", + "formula": "", + "price": 0, + "value": "11-50", + "dependencys": [] + } + ] + } + ] + } + ], + "mode": "normal" + }, + { + "id": "calc", + "name": "calc", + "dependencys": [ + { + "relation": "auflage", + "formula": "", + "borders": [ + { + "calcValue": "", + "calcValue1": "", + "calcValue2": "", + "calcValue3": "", + "calcValue4": "", + "calcValue5": "", + "calcValue6": "", + "calcValue7": "", + "calcValue8": "", + "calcValue9": "", + "calcValue10": "", + "flatRate": "", + "formula": "$Vauflage$V*$Vseiten_anzahl_inhalt$V", + "price": 0, + "value": "1-", + "dependencys": [] + } + ] + } + ], + "type": 1 + } + ] + } +] diff --git a/tests/Json/from.xml b/tests/Json/from.xml index 24ca40f..0157153 100644 --- a/tests/Json/from.xml +++ b/tests/Json/from.xml @@ -7,10 +7,14 @@ - + +