diff --git a/src/Calc/Calc.php b/src/Calc/Calc.php index edb9d8c..c325a2d 100644 --- a/src/Calc/Calc.php +++ b/src/Calc/Calc.php @@ -14,6 +14,7 @@ use PSC\Library\Calc\General\Type\Edge; use PSC\Library\Calc\General\Type\EdgeCollection; use PSC\Library\Calc\General\Type\EdgeCollectionContainer; use PSC\Library\Calc\Option\Type\Base; +use PSC\Library\Calc\Option\Type\Checkbox; use PSC\Library\Calc\Option\Type\PaperDbSelect; use PSC\Library\Calc\Option\Type\Select; use PSC\Library\Calc\Tests\Mock\Paper; @@ -51,7 +52,7 @@ class Calc /** @var Base $option */ foreach($this->article->getOptions() as $option) { - if($option instanceof Select) { + if($option instanceof Select || $option instanceof Checkbox) { /** @var Select\Opt $opt */ foreach($option->getOptions() as $opt) { if($opt->isValid() && $opt->isSelected()) { @@ -132,6 +133,20 @@ class Calc $this->engine->setVariable('price', $gesamt); } + if ($formel != "" && $option->getId() != 'weight' && $option->isAjaxExport() && !$option->isDisplayOnly() && $option->isAmount()) { + $p = 0; + $formel = str_replace("tonumber", '$this->toNumber', $formel); + eval('@$p = ' . $formel . ';'); + + $this->engine->addAjaxVariable($option->getId(), $p); + } + if ($formel != "" && $option->getId() != 'weight' && !$option->isAjaxExport() && $option->isDisplayOnly() && $option->isAmount()) { + $p = 0; + $formel = str_replace("tonumber", '$this->toNumber', $formel); + eval('@$p = ' . $formel . ';'); + + $this->engine->addDisplayVariable($option->getId(), $p); + } if(!$option->isAmount()) { $p = 0; $formel = str_replace("tonumber", '$this->toNumber', $formel); diff --git a/src/Calc/CalcValues.php b/src/Calc/CalcValues.php index 6b362c9..35decf1 100644 --- a/src/Calc/CalcValues.php +++ b/src/Calc/CalcValues.php @@ -15,6 +15,7 @@ use PSC\Library\Calc\General\Type\Edge; use PSC\Library\Calc\General\Type\EdgeCollection; use PSC\Library\Calc\General\Type\EdgeCollectionContainer; use PSC\Library\Calc\Option\Type\Base; +use PSC\Library\Calc\Option\Type\Checkbox; use PSC\Library\Calc\Option\Type\PaperDbSelect; use PSC\Library\Calc\Option\Type\Select; use PSC\Library\Calc\Tests\Mock\Paper; @@ -53,7 +54,7 @@ class CalcValues /** @var Base $option */ foreach ($this->article->getOptions() as $option) { - if ($option instanceof Select) { + if ($option instanceof Select || $option instanceof Checkbox) { /** @var Select\Opt $opt */ foreach ($option->getOptions() as $opt) { if ($opt->isValid() && $opt->isSelected()) { diff --git a/src/Calc/Valid.php b/src/Calc/Valid.php index 942268f..96c584e 100644 --- a/src/Calc/Valid.php +++ b/src/Calc/Valid.php @@ -7,6 +7,7 @@ use PSC\Library\Calc\General\Type\Edge; use PSC\Library\Calc\General\Type\EdgeCollection; use PSC\Library\Calc\General\Type\EdgeCollectionContainer; use PSC\Library\Calc\Option\Type\Base; +use PSC\Library\Calc\Option\Type\Checkbox; use PSC\Library\Calc\Option\Type\Select; class Valid @@ -37,7 +38,7 @@ class Valid /** @var Base $option */ foreach($this->article->getOptions() as $option) { - if($option instanceof Select) { + if($option instanceof Select|| $option instanceof Checkbox) { $valid = false; @@ -70,10 +71,19 @@ class Valid } } - if($this->engine->getVariables()[$option->getId()] == $opt->getId()) { - $opt->setIsSelected(true); - }else{ - $opt->setIsSelected(false); + if($option instanceof Checkbox) { + if (in_array($opt->getId(), $this->engine->getVariables()[$option->getId()])) { + $opt->setIsSelected(true); + } else { + $opt->setIsSelected(false); + } + + }else { + if ($this->engine->getVariables()[$option->getId()] == $opt->getId()) { + $opt->setIsSelected(true); + } else { + $opt->setIsSelected(false); + } } if($opt->isValid() && !$valid) { diff --git a/src/Engine.php b/src/Engine.php index b2c8255..b9cfe1f 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -6,6 +6,7 @@ use PSC\Library\Calc\Calc\Calc; use PSC\Library\Calc\Calc\CalcValues; use PSC\Library\Calc\Calc\Valid; use PSC\Library\Calc\Option\Type\Base; +use PSC\Library\Calc\Option\Type\Checkbox; use PSC\Library\Calc\Option\Type\PaperDbSelect; use PSC\Library\Calc\Option\Type\Select\Opt; use PSC\Library\Calc\Option\Type\Text; @@ -37,6 +38,12 @@ class Engine /** @var array */ protected $calcVariables = array(); + /** @var array */ + protected $ajaxVariables = array(); + + /** @var array */ + protected $displayVariables = array(); + /** @var float */ protected $price = 0; @@ -230,7 +237,11 @@ class Engine $option->setSavedCalcValues($this->savedCalcValues); if(!isset($this->variables[$option->getId()]) && $option->getDefault() !== null && !$option instanceof Text) { - $this->variables[$option->getId()] = $option->getDefault(); + if($option instanceof Checkbox) { + $this->variables[$option->getId()] = explode(",", $option->getDefault()); + }else{ + $this->variables[$option->getId()] = $option->getDefault(); + } } if(isset($this->variables[$option->getId()])) { @@ -412,4 +423,46 @@ class Engine { $this->savedCalcValues = $values; } + + /** + * @return array + */ + public function getAjaxVariables() + { + return $this->ajaxVariables; + } + + /** + * @param array $ajaxVariables + */ + public function setAjaxVariables($ajaxVariables) + { + $this->ajaxVariables = $ajaxVariables; + } + + public function addAjaxVariable($key, $value) + { + $this->ajaxVariables[$key] = $value; + } + + /** + * @return array + */ + public function getDisplayVariables() + { + return $this->displayVariables; + } + + /** + * @param array $displayVariables + */ + public function setDisplayVariables($displayVariables) + { + $this->displayVariables = $displayVariables; + } + + public function addDisplayVariable($key, $value) + { + $this->displayVariables[$key] = $value; + } } \ No newline at end of file diff --git a/src/General/Type/Edge.php b/src/General/Type/Edge.php index 10e3584..6dd8390 100644 --- a/src/General/Type/Edge.php +++ b/src/General/Type/Edge.php @@ -359,6 +359,9 @@ class Edge (preg_match("/^([0-9]+)-$/", trim($val), $regs) && $value >= $regs[1]))) { return true; } + if(is_array($value) && in_array($val, $value)) { + return true; + } } } diff --git a/src/Option/Parser.php b/src/Option/Parser.php index 8206efa..a157207 100644 --- a/src/Option/Parser.php +++ b/src/Option/Parser.php @@ -1,6 +1,7 @@ element = new \PSC\Library\Calc\Option\Type\Checkbox(); + + parent::__construct($node); + } + + public function parse() + { + parent::parse(); + + if(isset($this->node->grenzen) && $this->node->grenzen->children()) { + $edgeCollectionContainerParser = new EdgeCollectionContainer($this->node->grenzen); + $this->element->setEdgesCollectionContainer($edgeCollectionContainerParser->parse()); + } + + $this->parseModeNormal(); + + return $this->element; + } + + private function parseModeNormal() + { + foreach ($this->node->opt as $opt) { + $optParser = new Opt($opt); + $this->element->addOption($optParser->parse()); + } + } + +} \ No newline at end of file diff --git a/src/Option/Parser/Checkbox/Opt.php b/src/Option/Parser/Checkbox/Opt.php new file mode 100644 index 0000000..822fec3 --- /dev/null +++ b/src/Option/Parser/Checkbox/Opt.php @@ -0,0 +1,30 @@ +element = new \PSC\Library\Calc\Option\Type\Checkbox\Opt(); + $this->node = $node; + } + + public function parse() + { + $this->element->setId((string)$this->node['id']); + $this->element->setLabel((string)$this->node['name']); + + 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/Checkbox.php b/src/Option/Type/Checkbox.php new file mode 100644 index 0000000..2f2754b --- /dev/null +++ b/src/Option/Type/Checkbox.php @@ -0,0 +1,49 @@ +options = new \ArrayIterator(); + } + + public function addOption($option) + { + $this->options->append($option); + } + + /** + * Verarbeitet das Value + */ + public function processValue() + { + + } + + /** + * @return \ArrayIterator + */ + public function getOptions() + { + return $this->options; + } + + public function getSelectedOptions() + { + /** @var Opt $opt */ + foreach($this->getOptions() as $opt) { + if($opt->isSelected()) return $opt; + } + } + +} \ No newline at end of file diff --git a/src/Option/Type/Checkbox/Opt.php b/src/Option/Type/Checkbox/Opt.php new file mode 100644 index 0000000..f8d7306 --- /dev/null +++ b/src/Option/Type/Checkbox/Opt.php @@ -0,0 +1,112 @@ +edgesCollectionContainer = new EdgeCollectionContainer(); + } + + + /** + * @return string + */ + public function getId() + { + return $this->id; + } + + /** + * @param string $id + */ + public function setId($id) + { + $this->id = $id; + } + + /** + * @return string + */ + public function getLabel() + { + return $this->label; + } + + /** + * @param string $label + */ + public function setLabel($label) + { + $this->label = $label; + } + + /** + * @return EdgeCollectionContainer + */ + public function getEdgesCollectionContainer() + { + return $this->edgesCollectionContainer; + } + + /** + * @param EdgeCollectionContainer $edgesCollectionContainer + */ + public function setEdgesCollectionContainer($edgesCollectionContainer) + { + $this->edgesCollectionContainer = $edgesCollectionContainer; + } + + /** + * @return bool + */ + public function isValid() + { + return $this->isValid; + } + + /** + * @param bool $isValid + */ + public function setIsValid($isValid) + { + $this->isValid = $isValid; + } + + /** + * @return bool + */ + public function isSelected() + { + return $this->isSelected; + } + + /** + * @param bool $isSelected + */ + public function setIsSelected($isSelected) + { + $this->isSelected = $isSelected; + } +} \ No newline at end of file diff --git a/tests/Complex/SixTest.php b/tests/Complex/SixTest.php new file mode 100644 index 0000000..43407aa --- /dev/null +++ b/tests/Complex/SixTest.php @@ -0,0 +1,54 @@ +parse(simplexml_load_string(file_get_contents(__DIR__ . '/../TestFiles/Complex2/papierContainer.xml'))); + + $this->engine = new Engine(); + $this->engine->setPaperContainer($paperContainer); + $this->engine->setPaperRepository($repository); + $this->engine->setFormulas(file_get_contents(__DIR__.'/../TestFiles/Complex2/formels.txt')); + $this->engine->setParameters(file_get_contents(__DIR__.'/../TestFiles/Complex2/parameters.txt')); + + $this->engine->loadString(file_get_contents(__DIR__ . '/../TestFiles/Complex2/6.xml')); + + } + + public function tearDown() + { + $this->engine = null; + } + + public function testIfArticleCountIsCorrect() + { + $this->assertEquals(1, $this->engine->getArticles()->Count()); + } + + public function testIfSelectedOptionsCorrect() + { + $this->engine->getPrice(); + $this->assertEquals(37500 , $this->engine->getPrice()); + } + + public function testIfCalcCorrect() + { + $this->engine->setVariable('size', ['a3q','a4q']); + $this->engine->getPrice(); + $this->assertEquals(42000 , $this->engine->getPrice()); + } +} \ No newline at end of file diff --git a/tests/Customer/G/CalcTest.php b/tests/Customer/G/CalcTest.php index dd8f77d..b656652 100644 --- a/tests/Customer/G/CalcTest.php +++ b/tests/Customer/G/CalcTest.php @@ -39,4 +39,16 @@ class CalcTest extends \PHPUnit_Framework_TestCase { $this->assertEquals(10.49 , $this->engine->getPrice()); } + + public function testIfDisplayValues() + { + $this->assertEquals(10.49 , $this->engine->getPrice()); + $this->assertCount(1 , $this->engine->getDisplayVariables()); + } + + public function testIfAjaxValues() + { + $this->assertEquals(10.49 , $this->engine->getPrice()); + $this->assertCount(1 , $this->engine->getDisplayVariables()); + } } \ No newline at end of file diff --git a/tests/Customer/G/calc.xml b/tests/Customer/G/calc.xml index 3e06b62..2a54a34 100644 --- a/tests/Customer/G/calc.xml +++ b/tests/Customer/G/calc.xml @@ -39,7 +39,14 @@ + + + diff --git a/tests/Parse/Option/CheckboxTest.php b/tests/Parse/Option/CheckboxTest.php new file mode 100644 index 0000000..2660bea --- /dev/null +++ b/tests/Parse/Option/CheckboxTest.php @@ -0,0 +1,23 @@ +getOptByType(simplexml_load_string(file_get_contents(__DIR__ . '/../../TestFiles/Option/checkbox.xml'))); + + /** @var Checkbox $element */ + $element = $obj->parse(); + + $this->assertInstanceOf('PSC\Library\Calc\Option\Type\Checkbox', $element); + } + +} \ No newline at end of file diff --git a/tests/TestFiles/Complex2/6.xml b/tests/TestFiles/Complex2/6.xml new file mode 100644 index 0000000..20468bc --- /dev/null +++ b/tests/TestFiles/Complex2/6.xml @@ -0,0 +1,52 @@ + + + + TEST Checkbox + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/TestFiles/Option/checkbox.xml b/tests/TestFiles/Option/checkbox.xml new file mode 100644 index 0000000..9d04254 --- /dev/null +++ b/tests/TestFiles/Option/checkbox.xml @@ -0,0 +1,9 @@ + +