diff --git a/src/Calc/Calc.php b/src/Calc/Calc.php new file mode 100644 index 0000000..f1ee69d --- /dev/null +++ b/src/Calc/Calc.php @@ -0,0 +1,78 @@ +engine = $engine; + $this->article = $article; + $this->formelCalc = new Formel($engine, $article); + } + + public function calc() + { + $price = 0; + + /** @var Base $option */ + foreach($this->article->getOptions() as $option) { + + /** @var EdgeCollection $collection */ + foreach($option->getEdgesCollectionContainer() as $collection) { + + if($collection->getFormel() != "") { + $formel = $this->formelCalc->parseVariables($collection->getFormel()); + eval('$var = ' . $formel . ';'); + }else{ + $var = $this->engine->getVariables()[$collection->getName()]; + } + + /** @var Edge $edge */ + foreach($collection as $edge) { + + if($edge->isValid($var)) { + $formel = $this->formelCalc->parseVariables($edge->getFormel()); + if ($formel != "") { + eval('$price += ' . $formel . ';'); + + $this->engine->setVariable('price', $price); + } + } + } + } + } + + return $price; + } + +} \ No newline at end of file diff --git a/src/Calc/EvalCommand.php b/src/Calc/EvalCommand.php new file mode 100644 index 0000000..c387ec4 --- /dev/null +++ b/src/Calc/EvalCommand.php @@ -0,0 +1,54 @@ +engine->getVariables(); + + if (!empty($founds [0])) { + foreach ($founds [0] as $key => $found) { + $foundvalue = str_replace('$V', '', $found); + if (isset($variables [$foundvalue])) { + if ($variables [$foundvalue] == 'null') { + $formel = str_replace($found, 0, $formel); + } else { + if ($foundvalue == 'auflage') { + $formel = str_replace($found, str_replace(',', '.', $variables [$foundvalue]), $formel); + } else { + if ($variables [$foundvalue] == '') { + $formel = str_replace($found, 0, $formel); + } else { + $formel = str_replace($found, $variables [$foundvalue], $formel); + } + } + } + } else { + $formel = str_replace($found, 0, $formel); + } + } + } + + return $formel; + } + +} \ No newline at end of file diff --git a/src/Calc/Formel.php b/src/Calc/Formel.php index 5e7ac9c..58b0f64 100644 --- a/src/Calc/Formel.php +++ b/src/Calc/Formel.php @@ -17,40 +17,18 @@ class Formel /** @var Article */ protected $article = null; + /** + * Calc constructor. + * @param Engine $engine + * @param Article $article + */ public function __construct($engine, $article) { $this->engine = $engine; $this->article = $article; } - /** - * @return float - */ - public function calc() - { - $price = 0; - - /** @var Base $option */ - foreach($this->article->getOptions() as $option) { - - /** @var EdgeCollection $collection */ - foreach($option->getEdgesCollectionContainer() as $collection) { - /** @var Edge $edge */ - foreach($collection as $edge) { - if($this->edgeIsValid($collection->getName(), $edge)) { - $formel = $this->parseVar($edge->getFormel()); - if ($formel != "") { - eval('$price += ' . $formel . ';'); - } - } - } - } - } - - return $price; - } - - private function parseVar($formel) + public function parseVariables($formel) { preg_match_all('/\$V\w*\$V/', $formel, $founds); @@ -81,35 +59,4 @@ class Formel return $formel; } - - /** - * @param $section - * @param Edge $edge - * @return bool - */ - private function edgeIsValid($section, $edge) - { - if(!isset($this->engine->getVariables()[$section])) { - return false; - } - - - if($edge->isRegion() && - $edge->getFrom() <= $this->engine->getVariables()[$section] && - $edge->getTo() >= $this->engine->getVariables()[$section]) { - return true; - } - - if($edge->isRegion() && - $edge->getFrom() <= $this->engine->getVariables()[$section] && - $edge->getTo() == 0) { - return true; - } - - if(!$edge->isRegion() && in_array($this->engine->getVariables()[$section], $edge->getValues())) { - return true; - } - - return false; - } } \ No newline at end of file diff --git a/src/Engine.php b/src/Engine.php index 1aff32b..5370526 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -2,7 +2,7 @@ namespace PSC\Library\Calc; use Doctrine\Common\Persistence\ObjectRepository; -use PSC\Library\Calc\Calc\Formel; +use PSC\Library\Calc\Calc\Calc; use PSC\Library\Calc\Calc\Valid; use PSC\Library\Calc\Option\Type\Base; use PSC\Library\Calc\Option\Type\Text; @@ -31,6 +31,9 @@ class Engine /** @var array */ protected $variables = array(); + /** @var array */ + protected $calcVariables = array(); + /** @var float */ protected $price = 0; @@ -85,7 +88,7 @@ class Engine public function getArticleByName($name = false) { if($name === false) { - throw new \Exception('No Name provided'); + $this->articles[0]; } /** @var Article $article */ @@ -95,10 +98,6 @@ class Engine return $article; } } - - if($name === false) { - throw new \Exception('Article not found: ' . $name); - } } /** @@ -207,8 +206,7 @@ class Engine $calcValid->calc(); // CALC Formel - - $calcFormel = new Formel($this, $article); + $calcFormel = new Calc($this, $article); $this->price += $calcFormel->calc(); $this->dirty = false; @@ -234,4 +232,25 @@ class Engine $this->price = $price; } + /** + * @return array + */ + public function getCalcVariables() + { + return $this->calcVariables; + } + + /** + * @param array $calcVariables + */ + public function setCalcVariables($calcVariables) + { + $this->calcVariables = $calcVariables; + } + + public function setVariable($var, $value) + { + $this->variables[$var] = $value; + } + } \ No newline at end of file diff --git a/src/General/Parser/EdgeCollection.php b/src/General/Parser/EdgeCollection.php index 93c6905..0822860 100644 --- a/src/General/Parser/EdgeCollection.php +++ b/src/General/Parser/EdgeCollection.php @@ -27,6 +27,10 @@ class EdgeCollection { $collection = new \PSC\Library\Calc\General\Type\EdgeCollection(); + if(isset($this->node->attributes()->formel)) { + $collection->setFormel((string)$this->node->attributes()->formel); + } + foreach($this->node->grenze as $row) { $edgeParser = new Edge($row); $edge = $edgeParser->parse(); diff --git a/src/General/Type/Edge.php b/src/General/Type/Edge.php index f2a6aa3..677af9a 100644 --- a/src/General/Type/Edge.php +++ b/src/General/Type/Edge.php @@ -143,4 +143,29 @@ class Edge } + /** + * @param $value + * @return bool + */ + public function isValid($value) + { + + if($this->isRegion() && + $this->getFrom() <= $value && + $this->getTo() >= $value) { + return true; + } + + if($this->isRegion() && + $this->getFrom() <= $value && + $this->getTo() == 0) { + return true; + } + + if(!$this->isRegion() && in_array($value, $this->getValues())) { + return true; + } + + return false; + } } \ No newline at end of file diff --git a/src/General/Type/EdgeCollection.php b/src/General/Type/EdgeCollection.php index 0272eb6..c3d72cc 100644 --- a/src/General/Type/EdgeCollection.php +++ b/src/General/Type/EdgeCollection.php @@ -5,7 +5,9 @@ namespace PSC\Library\Calc\General\Type; class EdgeCollection extends \ArrayIterator { - public $name = ''; + protected $name = ''; + + protected $formel = ''; /** * @return string @@ -23,5 +25,21 @@ class EdgeCollection extends \ArrayIterator $this->name = $name; } + /** + * @return string + */ + public function getFormel() + { + return $this->formel; + } + + /** + * @param string $formel + */ + public function setFormel($formel) + { + $this->formel = $formel; + } + } \ No newline at end of file diff --git a/src/Option/Parser.php b/src/Option/Parser.php index 4311899..ec3db1b 100644 --- a/src/Option/Parser.php +++ b/src/Option/Parser.php @@ -1,6 +1,7 @@ element = new \PSC\Library\Calc\Option\Type\Hidden(); + parent::__construct($node); + } + + public function parse() + { + parent::parse(); + + if($this->node->children()) { + $edgeCollectionContainerParser = new EdgeCollectionContainer($this->node); + $this->element->setEdgesCollectionContainer($edgeCollectionContainerParser->parse()); + } + + return $this->element; + } + +} \ No newline at end of file diff --git a/src/Option/Type/Hidden.php b/src/Option/Type/Hidden.php new file mode 100644 index 0000000..83b8be5 --- /dev/null +++ b/src/Option/Type/Hidden.php @@ -0,0 +1,9 @@ +engine = new Engine(new Container()); + $this->engine->loadString(file_get_contents(__DIR__ . '/../TestFiles/Calc/price_min.xml')); + + } + + public function tearDown() + { + $this->engine = null; + } + + public function testIfArticleCountIsCorrect() + { + $this->assertEquals(1, $this->engine->getArticles()->Count()); + } + + public function testIfParserGetMinPrice() + { + $this->engine->calc(); + + $this->assertEquals(2500, $this->engine->getPrice()); + } + + public function testIfCalcReturnsGrenzeWithFormular() + { + $this->engine->setVariables([ + 'gebwert' => 1000000, + 'maschwertjuengerfuenf' => 400000, + 'maschwertaelterfuenf' => 400000, + 'allegereate' => 400000, + 'betriebsunterbrechung' => 400000, + 'umsatzjahrtransport' => 300000, + 'umsatzjahrbh' => 400000]); + $this->engine->calc(); + + $this->assertEquals(2721.40, $this->engine->getPrice()); + } + +} \ No newline at end of file diff --git a/tests/TestFiles/Calc/price_min.xml b/tests/TestFiles/Calc/price_min.xml new file mode 100644 index 0000000..692a097 --- /dev/null +++ b/tests/TestFiles/Calc/price_min.xml @@ -0,0 +1,67 @@ + + + + + test1 + test2 + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file