diff --git a/src/Calc.php b/src/Calc.php index 98cd58e..09efa38 100644 --- a/src/Calc.php +++ b/src/Calc.php @@ -33,8 +33,6 @@ final class Calc private $variables = []; - //protected $allowedPhpFunctions = false; - /** * @param integer $scale */ @@ -54,7 +52,7 @@ public function evaluate(array $tokens, array $variables = []) { $stack = new SplStack; - //var_dump($tokens); + $this->defineVars($variables); foreach ($tokens as $token) { if ($token->isNumber()) { @@ -170,6 +168,23 @@ public function defineVar($name, $value) return $this; } + /** + * Define new variables. + *
+ * Array format ['varName' => 'varValue', ...] + * + * @param array $variables + * @return \Sufir\Calc\Calc + */ + public function defineVars(array $variables = []) + { + foreach ($variables as $name => $value) { + $this->defineVar($name, $value); + } + + return $this; + } + /** * @param string $operator * @param integer|float $firstOperand diff --git a/src/Token/OperatorToken.php b/src/Token/OperatorToken.php index b035eb1..ccefe71 100644 --- a/src/Token/OperatorToken.php +++ b/src/Token/OperatorToken.php @@ -44,8 +44,6 @@ public function getPriority($assoc = 'left') return 2; case '+': return 1; - default: - return 0; } } else { switch ($this->value) { @@ -57,8 +55,6 @@ public function getPriority($assoc = 'left') case '+': case '-': return 1; - default: - return 0; } } } diff --git a/tests/CalcTest.php b/tests/CalcTest.php index bc4cf2e..be96736 100644 --- a/tests/CalcTest.php +++ b/tests/CalcTest.php @@ -51,9 +51,7 @@ public function testEvaluate( $this->calc->registerFunction($functionName, $functionBody); } - foreach ($variables as $name => $value) { - $this->calc->defineVar($name, $value); - } + $this->calc->defineVars($variables); $result = $this->calc->evaluate($converted); diff --git a/tests/TokenTest.php b/tests/TokenTest.php index 6579311..bf13f9b 100644 --- a/tests/TokenTest.php +++ b/tests/TokenTest.php @@ -177,7 +177,7 @@ public function testOperatorToken($expr) $this->assertInternalType('boolean', $operator->isRightAssoc()); $this->assertInternalType('boolean', $operator->isLeftAssoc()); $this->assertNotEquals($operator->isLeftAssoc(), $operator->isRightAssoc()); - $this->assertGreaterThan(0, $operator->getPriority()); + $this->assertGreaterThan(0, $operator->getPriority('left')); $this->assertGreaterThan(0, $operator->getPriority('right')); return $operator;