Skip to content

Commit

Permalink
add defineVars(array) + small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Алексей Скляров committed Dec 27, 2015
1 parent 542d841 commit 3579320
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
21 changes: 18 additions & 3 deletions src/Calc.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ final class Calc

private $variables = [];

//protected $allowedPhpFunctions = false;

/**
* @param integer $scale
*/
Expand All @@ -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()) {
Expand Down Expand Up @@ -170,6 +168,23 @@ public function defineVar($name, $value)
return $this;
}

/**
* Define new variables.
* <br>
* 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
Expand Down
4 changes: 0 additions & 4 deletions src/Token/OperatorToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public function getPriority($assoc = 'left')
return 2;
case '+':
return 1;
default:
return 0;
}
} else {
switch ($this->value) {
Expand All @@ -57,8 +55,6 @@ public function getPriority($assoc = 'left')
case '+':
case '-':
return 1;
default:
return 0;
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions tests/CalcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion tests/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3579320

Please sign in to comment.