From b2c015a3b2ffd345dc5f414594fd756113c9fead Mon Sep 17 00:00:00 2001 From: Thomas Peterson Date: Thu, 28 May 2020 15:17:40 +0200 Subject: [PATCH] Add CalcValues1-10 --- src/Calc/CalcValues.php | 129 ++++++++++--- src/General/Parser/Edge.php | 54 ++++-- src/General/Type/Edge.php | 198 ++++++++++++++++++-- tests/CalcValue/AddCalcValuesTest.php | 29 +++ tests/TestFiles/CalcValue/addcalcvalues.xml | 30 +++ 5 files changed, 395 insertions(+), 45 deletions(-) create mode 100644 tests/CalcValue/AddCalcValuesTest.php create mode 100644 tests/TestFiles/CalcValue/addcalcvalues.xml diff --git a/src/Calc/CalcValues.php b/src/Calc/CalcValues.php index 1febd14..5088c01 100644 --- a/src/Calc/CalcValues.php +++ b/src/Calc/CalcValues.php @@ -1,4 +1,5 @@ article->getOptions() as $option) { + foreach ($this->article->getOptions() as $option) { - if($option instanceof Select) { + if ($option instanceof Select) { /** @var Select\Opt $opt */ - foreach($option->getOptions() as $opt) { - if($opt->isValid() && $opt->isSelected()) { + foreach ($option->getOptions() as $opt) { + if ($opt->isValid() && $opt->isSelected()) { $price = $this->parseEdgeCollection($price, $option->getId(), $opt->getEdgesCollectionContainer()); } } } $this->parseEdgeCollection($price, $option->getId(), $option->getEdgesCollectionContainer()); - } return $price; @@ -86,30 +86,29 @@ class CalcValues eval($this->engine->getFormulas()); /** @var EdgeCollection $collection */ - foreach($container as $collection) { - if($collection->getName() == "opt") continue; - if($collection->getFormel() != "") { + foreach ($container as $collection) { + if ($collection->getName() == "opt") continue; + if ($collection->getFormel() != "") { $formel = $this->formelCalc->parse($collection->getFormel()); - if(preg_match("/^[a-z](.*)/", $formel)) { + if (preg_match("/^[a-z](.*)/", $formel)) { eval('$var = "' . $formel . '";'); - }else{ + } else { eval('$var = ' . $formel . ';'); } - - }else{ - if(!isset($this->engine->getVariables()[$collection->getName()])) { + } else { + if (!isset($this->engine->getVariables()[$collection->getName()])) { $var = 'XXXXXXXX'; - }else{ + } else { $var = $this->engine->getVariables()[$collection->getName()]; } } /** @var Edge $edge */ - foreach($collection as $edge) { + foreach ($collection as $edge) { - if($edge->isValid($var)) { + if ($edge->isValid($var)) { - if($edge->getCalcValue() != "") { + if ($edge->getCalcValue() != "") { $cv = $this->formelCalc->parse($edge->getCalcValue()); eval('$cv = ' . $cv . ';'); //echo $id . ' '. $this->formelCalc->parse($edge->getCalcValue()) . ' ' . $cv . ' '. PHP_EOL.PHP_EOL; @@ -117,18 +116,104 @@ class CalcValues $this->engine->addCalcVariable($id, $cv); $this->engine->addCalcVariable($id . '_' . $collection->getName(), $cv); - - } - if($edge->getEdgesCollectionContainer()->count() > 0) { + if ($edge->getCalcValue1() != "") { + $cv = $this->formelCalc->parse($edge->getCalcValue1()); + eval('$cv = ' . $cv . ';'); + //echo $id . ' '. $this->formelCalc->parse($edge->getCalcValue()) . ' ' . $cv . ' '. PHP_EOL.PHP_EOL; + + $this->engine->addCalcVariable($id . '_1', $cv); + + $this->engine->addCalcVariable($id . '_' . $collection->getName() . '_1', $cv); + } + if ($edge->getCalcValue2() != "") { + $cv = $this->formelCalc->parse($edge->getCalcValue2()); + eval('$cv = ' . $cv . ';'); + //echo $id . ' '. $this->formelCalc->parse($edge->getCalcValue()) . ' ' . $cv . ' '. PHP_EOL.PHP_EOL; + + $this->engine->addCalcVariable($id . '_2', $cv); + + $this->engine->addCalcVariable($id . '_' . $collection->getName() . '_2', $cv); + } + if ($edge->getCalcValue3() != "") { + $cv = $this->formelCalc->parse($edge->getCalcValue3()); + eval('$cv = ' . $cv . ';'); + //echo $id . ' '. $this->formelCalc->parse($edge->getCalcValue()) . ' ' . $cv . ' '. PHP_EOL.PHP_EOL; + + $this->engine->addCalcVariable($id . '_3', $cv); + + $this->engine->addCalcVariable($id . '_' . $collection->getName() . '_3', $cv); + } + if ($edge->getCalcValue4() != "") { + $cv = $this->formelCalc->parse($edge->getCalcValue4()); + eval('$cv = ' . $cv . ';'); + //echo $id . ' '. $this->formelCalc->parse($edge->getCalcValue()) . ' ' . $cv . ' '. PHP_EOL.PHP_EOL; + + $this->engine->addCalcVariable($id . '_4', $cv); + + $this->engine->addCalcVariable($id . '_' . $collection->getName() . '_4', $cv); + } + if ($edge->getCalcValue5() != "") { + $cv = $this->formelCalc->parse($edge->getCalcValue5()); + eval('$cv = ' . $cv . ';'); + //echo $id . ' '. $this->formelCalc->parse($edge->getCalcValue()) . ' ' . $cv . ' '. PHP_EOL.PHP_EOL; + + $this->engine->addCalcVariable($id . '_5', $cv); + + $this->engine->addCalcVariable($id . '_' . $collection->getName() . '_5', $cv); + } + if ($edge->getCalcValue6() != "") { + $cv = $this->formelCalc->parse($edge->getCalcValue6()); + eval('$cv = ' . $cv . ';'); + //echo $id . ' '. $this->formelCalc->parse($edge->getCalcValue()) . ' ' . $cv . ' '. PHP_EOL.PHP_EOL; + + $this->engine->addCalcVariable($id . '_6', $cv); + + $this->engine->addCalcVariable($id . '_' . $collection->getName() . '_6', $cv); + } + if ($edge->getCalcValue7() != "") { + $cv = $this->formelCalc->parse($edge->getCalcValue7()); + eval('$cv = ' . $cv . ';'); + //echo $id . ' '. $this->formelCalc->parse($edge->getCalcValue()) . ' ' . $cv . ' '. PHP_EOL.PHP_EOL; + + $this->engine->addCalcVariable($id . '_7', $cv); + + $this->engine->addCalcVariable($id . '_' . $collection->getName() . '_7', $cv); + } + if ($edge->getCalcValue8() != "") { + $cv = $this->formelCalc->parse($edge->getCalcValue8()); + eval('$cv = ' . $cv . ';'); + //echo $id . ' '. $this->formelCalc->parse($edge->getCalcValue()) . ' ' . $cv . ' '. PHP_EOL.PHP_EOL; + + $this->engine->addCalcVariable($id . '_8', $cv); + + $this->engine->addCalcVariable($id . '_' . $collection->getName() . '_8', $cv); + } + if ($edge->getCalcValue9() != "") { + $cv = $this->formelCalc->parse($edge->getCalcValue9()); + eval('$cv = ' . $cv . ';'); + //echo $id . ' '. $this->formelCalc->parse($edge->getCalcValue()) . ' ' . $cv . ' '. PHP_EOL.PHP_EOL; + + $this->engine->addCalcVariable($id . '_9', $cv); + + $this->engine->addCalcVariable($id . '_' . $collection->getName() . '_9', $cv); + } + if ($edge->getCalcValue10() != "") { + $cv = $this->formelCalc->parse($edge->getCalcValue10()); + eval('$cv = ' . $cv . ';'); + //echo $id . ' '. $this->formelCalc->parse($edge->getCalcValue()) . ' ' . $cv . ' '. PHP_EOL.PHP_EOL; + + $this->engine->addCalcVariable($id . '_10', $cv); + + $this->engine->addCalcVariable($id . '_' . $collection->getName() . '_10', $cv); + } + if ($edge->getEdgesCollectionContainer()->count() > 0) { $this->parseEdgeCollection($price, $id . '_' . $collection->getName(), $edge->getEdgesCollectionContainer(), true); } - } } } return $price; } - } diff --git a/src/General/Parser/Edge.php b/src/General/Parser/Edge.php index de962f1..06ad162 100644 --- a/src/General/Parser/Edge.php +++ b/src/General/Parser/Edge.php @@ -1,4 +1,5 @@ node->attributes()->formel)) { - $edge->setFormel((string)$this->node->attributes()->formel); + if (isset($this->node->attributes()->formel)) { + $edge->setFormel((string) $this->node->attributes()->formel); } - if(isset($this->node->attributes()->calc_value)) { - $edge->setCalcValue((string)$this->node->attributes()->calc_value); + if (isset($this->node->attributes()->calc_value)) { + $edge->setCalcValue((string) $this->node->attributes()->calc_value); } - if(isset($this->node->attributes()->preis)) { + if (isset($this->node->attributes()->calc_value_1)) { + $edge->setCalcValue1((string) $this->node->attributes()->calc_value_1); + } + if (isset($this->node->attributes()->calc_value_2)) { + $edge->setCalcValue2((string) $this->node->attributes()->calc_value_2); + } + if (isset($this->node->attributes()->calc_value_3)) { + $edge->setCalcValue3((string) $this->node->attributes()->calc_value_3); + } + if (isset($this->node->attributes()->calc_value_4)) { + $edge->setCalcValue4((string) $this->node->attributes()->calc_value_4); + } + if (isset($this->node->attributes()->calc_value_5)) { + $edge->setCalcValue5((string) $this->node->attributes()->calc_value_5); + } + if (isset($this->node->attributes()->calc_value_6)) { + $edge->setCalcValue6((string) $this->node->attributes()->calc_value_6); + } + if (isset($this->node->attributes()->calc_value_7)) { + $edge->setCalcValue7((string) $this->node->attributes()->calc_value_7); + } + if (isset($this->node->attributes()->calc_value_8)) { + $edge->setCalcValue8((string) $this->node->attributes()->calc_value_8); + } + if (isset($this->node->attributes()->calc_value_9)) { + $edge->setCalcValue9((string) $this->node->attributes()->calc_value_9); + } + if (isset($this->node->attributes()->calc_value_10)) { + $edge->setCalcValue10((string) $this->node->attributes()->calc_value_10); + } + if (isset($this->node->attributes()->preis)) { $edge->setPreis(floatval($this->node->attributes()->preis)); } - if(isset($this->node->attributes()->pauschale)) { + if (isset($this->node->attributes()->pauschale)) { $edge->setPauschale(floatval($this->node->attributes()->pauschale)); } - if(isset($this->node->attributes()->value) && $this->node->children()) { - $edge->setValues(explode(",", trim((string)$this->node->attributes()->value))); - if($this->node->children()) { + if (isset($this->node->attributes()->value) && $this->node->children()) { + $edge->setValues(explode(",", trim((string) $this->node->attributes()->value))); + if ($this->node->children()) { $edgeCollectionContainerParser = new EdgeCollectionContainer($this->node); $edge->setEdgesCollectionContainer($edgeCollectionContainerParser->parse()); } - }else { + } else { - $value = (string)$this->node; + $value = (string) $this->node; if (preg_match("/^([0-9a-zA-Z_]+)$/", trim($value), $regs)) { $edge->setValues([$regs[1]]); } elseif (preg_match("/^([0-9]+)-([0-9]+)$/", trim($value), $regs)) { @@ -65,4 +96,3 @@ class Edge return $edge; } } - diff --git a/src/General/Type/Edge.php b/src/General/Type/Edge.php index e02fd44..10e3584 100644 --- a/src/General/Type/Edge.php +++ b/src/General/Type/Edge.php @@ -1,4 +1,5 @@ calcValue = $calcValue; } + /** + * @return string + */ + public function getCalcValue1() + { + return $this->calcValue1; + } + + /** + * @param string $calcValue1 + */ + public function setCalcValue1($calcValue1) + { + $this->calcValue1 = $calcValue1; + } + + /** + * @return string + */ + public function getCalcValue2() + { + return $this->calcValue2; + } + + /** + * @param string $calcValue2 + */ + public function setCalcValue2($calcValue2) + { + $this->calcValue2 = $calcValue2; + } + + /** + * @return string + */ + public function getCalcValue3() + { + return $this->calcValue3; + } + + /** + * @param string $calcValue3 + */ + public function setCalcValue3($calcValue3) + { + $this->calcValue3 = $calcValue3; + } + + /** + * @return string + */ + public function getCalcValue4() + { + return $this->calcValue4; + } + + /** + * @param string $calcValue4 + */ + public function setCalcValue4($calcValue4) + { + $this->calcValue4 = $calcValue4; + } + + /** + * @return string + */ + public function getCalcValue5() + { + return $this->calcValue5; + } + + /** + * @param string $calcValue5 + */ + public function setCalcValue5($calcValue5) + { + $this->calcValue5 = $calcValue5; + } + + /** + * @return string + */ + public function getCalcValue6() + { + return $this->calcValue6; + } + + /** + * @param string $calcValue6 + */ + public function setCalcValue6($calcValue6) + { + $this->calcValue6 = $calcValue6; + } + + /** + * @return string + */ + public function getCalcValue7() + { + return $this->calcValue7; + } + + /** + * @param string $calcValue7 + */ + public function setCalcValue7($calcValue7) + { + $this->calcValue7 = $calcValue7; + } + + /** + * @return string + */ + public function getCalcValue8() + { + return $this->calcValue8; + } + + /** + * @param string $calcValue8 + */ + public function setCalcValue8($calcValue8) + { + $this->calcValue8 = $calcValue8; + } + + /** + * @return string + */ + public function getCalcValue9() + { + return $this->calcValue9; + } + + /** + * @param string $calcValue9 + */ + public function setCalcValue9($calcValue9) + { + $this->calcValue9 = $calcValue9; + } + + /** + * @return string + */ + public function getCalcValue10() + { + return $this->calcValue10; + } + + /** + * @param string $calcValue10 + */ + public function setCalcValue10($calcValue10) + { + $this->calcValue10 = $calcValue10; + } + /** * @return int */ @@ -158,30 +330,33 @@ class Edge public function isValid($value) { - if($this->isRegion() && + if ( + $this->isRegion() && $this->getFrom() <= $value && - $this->getTo() >= $value) { + $this->getTo() >= $value + ) { return true; } - if($this->isRegion() && + if ( + $this->isRegion() && $this->getFrom() <= $value && - $this->getTo() == 0) { + $this->getTo() == 0 + ) { return true; } - if(!$this->isRegion() && in_array($value, $this->getValues())) { + if (!$this->isRegion() && in_array($value, $this->getValues())) { return true; } $values = $this->getValues(); - if(is_array($values)) { - foreach($values as $val) { + if (is_array($values)) { + foreach ($values as $val) { if (( - (preg_match("/^([0-9]+)-([0-9]+)$/", trim($val), $regs) && ($value >= $regs [1] && $value <= $regs [2])) || - (preg_match("/^([0-9]+)-$/", trim($val), $regs) && $value >= $regs [1])) - ) { + (preg_match("/^([0-9]+)-([0-9]+)$/", trim($val), $regs) && ($value >= $regs[1] && $value <= $regs[2])) || + (preg_match("/^([0-9]+)-$/", trim($val), $regs) && $value >= $regs[1]))) { return true; } } @@ -205,4 +380,5 @@ class Edge { $this->edgesCollectionContainer = $edgesCollectionContainer; } -} \ No newline at end of file +} + diff --git a/tests/CalcValue/AddCalcValuesTest.php b/tests/CalcValue/AddCalcValuesTest.php new file mode 100644 index 0000000..c5e7d03 --- /dev/null +++ b/tests/CalcValue/AddCalcValuesTest.php @@ -0,0 +1,29 @@ +engine = new Engine(new Container()); + $this->engine->loadString(file_get_contents(__DIR__ . '/../TestFiles/CalcValue/addcalcvalues.xml')); + } + + public function tearDown() + { + $this->engine = null; + } + + public function testIfParseValue() + { + $this->assertEquals(85, $this->engine->getPrice()); + } +} + diff --git a/tests/TestFiles/CalcValue/addcalcvalues.xml b/tests/TestFiles/CalcValue/addcalcvalues.xml new file mode 100644 index 0000000..7ca5a61 --- /dev/null +++ b/tests/TestFiles/CalcValue/addcalcvalues.xml @@ -0,0 +1,30 @@ + + + + + simple + simple + + + + + + +