diff --git a/lib/MathParser/Expressions/Number.php b/lib/MathParser/Expressions/Number.php index 6bd2bbe..7b49ecc 100644 --- a/lib/MathParser/Expressions/Number.php +++ b/lib/MathParser/Expressions/Number.php @@ -8,7 +8,7 @@ class Number extends Expression { public function __construct($value) { - parent::__construct(+$value); + parent::__construct($value); } public function operate(array &$stack) diff --git a/lib/MathParser/Math.php b/lib/MathParser/Math.php index 08b31ae..a353f00 100644 --- a/lib/MathParser/Math.php +++ b/lib/MathParser/Math.php @@ -77,7 +77,7 @@ public function parse($string) public function registerVariable($name, $value) { - $this->assertVariableIsNumber($value); + $this->assertVariableIsNumberOrNull($value); $this->variables[$name] = $value; } @@ -198,7 +198,7 @@ public function clearVariables() */ public function setVariables(array $variables) { - $this->assertVariablesAreNumbers($variables); + $this->assertVariablesAreNumbersOrNull($variables); $this->variables = $variables; } @@ -211,17 +211,17 @@ private function substituteVariables(array &$stack, array $variables) } } - private function assertVariablesAreNumbers(array $variables) + private function assertVariablesAreNumbersOrNull(array $variables) { foreach ($variables as $variable) { - $this->assertVariableIsNumber($variable); + $this->assertVariableIsNumberOrNull($variable); } } - private function assertVariableIsNumber($variable) + private function assertVariableIsNumberOrNull($variable) { - if (!is_int($variable) && !is_float($variable)) { - throw new \InvalidArgumentException('provided variable is not a number'); + if (!is_int($variable) && !is_float($variable) && !($variable === null)) { + throw new \InvalidArgumentException('provided variable is not a number or null. found: ' . $variable); } } } diff --git a/test/MathParserTest.php b/test/MathParserTest.php index a0f5de2..698ac92 100644 --- a/test/MathParserTest.php +++ b/test/MathParserTest.php @@ -106,6 +106,7 @@ public static function provideVariableData() [['$a1 + $b2 + $0', ['a1' => 0, 'b2' => 41, 0 => 13]], 54], [['$0 + $2', [0, 41, 13]], 13], [['$0 + $2', [0, 41, 13]], 13], + [['$0 + $2', [null, 41, 13]], null], ]; return $data;