/** * @{inheritdoc} */ public function parse(Formula $formula) { $this->tokenCollector->addParameterBag($formula->getParameterBag()); foreach ($formula->getSubFormulas() as $key => $subformula) { $this->tokenCollector->set($key, $subformula->formula); $this->parse($subformula); } }
public function validateFormula($attribute, $params) { if (!empty($this->{$attribute})) { $formula = new Formula($this->{$attribute}, Formula::CALCULABLE); $formula->setParameter('var_purchase_price', 100); // $formula->setIsCalculable(true); if (!$formula->render()) { $this->addError($attribute, 'Incorrect formula.'); } } }
public function testNotCalculable() { $formula = new FormulaModel('2 + 1 + {{ sf }} - {{ sf2 }}', false); $formula->setRenderer(new StrtrFormulaRenderer()); $formula->setSubFormula('sf', new FormulaModel('3*10', true)); $formula->setSubFormula('sf2', new FormulaModel('10-{{sf3}}', false)); $this->phpString($formula->render())->isEqualTo('2 + 1 + 30 - 10-{{sf3}}'); $formula->setSubFormula('sf3', new FormulaModel('5-3', true)); $formula->getSubFormula('sf2')->setIsCalculable(true); $this->phpString($formula->render())->isEqualTo('2 + 1 + 30 - 2'); $formula->setIsCalculable(true); $this->phpString($formula->render())->isEqualTo('31'); }
/** * @param Formula $formula * * @return array */ public function getReplacements(Formula $formula) { $result = array(); $start = $this->getOption('separator_start'); $end = $this->getOption('separator_end'); if (preg_match_all('/' . $start . '(.+?)' . $end . '/is', $this->prepare($formula->formula), $matches)) { $result = array_merge($result, $matches[1]); } foreach ($formula->getSubFormulas() as $key => $subformula) { $result = array_merge($result, $this->getReplacements($subformula)); unset($result[array_search($key, $result)]); } return array_unique($result); }
public function testMultiLevel() { $formula = new Formula('{{key}} key {{subformula2}} {{subformula1}} {{subformula2}} {{anotherone}}'); $formula->setSubFormula('subformula1', new Formula('{{key}} and {{sf3}}')); $formula->setSubFormula('subformula2', new Formula('{{sf2}}')); $formula->setSubFormula('subformula3', new Formula('UNUSED')); $formula->setParameter('key', 'VIC'); $formula->setParameter('value', 'MCKEY'); $formula->setParameter('sf2', 'valuesf2'); $formula->setParameter('sf3', 'valuesf3'); $renderer = new StrtrFormulaRendererModel(); $this->phpString($renderer->render($formula, $formula->parse()))->isEqualTo('VIC key valuesf2 VIC and valuesf3 valuesf2 {{anotherone}}'); }
protected function calculateFormula($model) { if (!empty($model->store->formula_purchase_p) && !empty($model->purchase_price)) { $formula = new Formula($model->store->formula_purchase_p, Formula::CALCULABLE); $formula->setParameter('var_purchase_price', $model->purchase_price); $formula->setIsCalculable(true); if ($formula->render()) { $model->sale_price = round($formula->render(), $model->store->round_purchase_p_precision, constant($model->store->round_purchase_p_mode)); $model->total_price = $model->sale_price + $model->shipping_price; $model->update(); } } }