diff options
author | Alexander Kiryukhin <alexander@kiryukhin.su> | 2018-09-12 20:05:09 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-12 20:05:09 +0300 |
commit | 4a672cfd94c07e1821227f27fd1edd2217685136 (patch) | |
tree | c50b777d06d9e9eeff6aeb59005c389ddf9a552b /src/NXP/MathExecutor.php | |
parent | 76d1b4b8f02d555e4b4f4fd4d641597f0f6b5f4e (diff) | |
parent | 2722a5201d326317e391ae6c0e2fa7e0c2b537b4 (diff) |
Merge pull request #28 from phpfui/support_for_double_quoted_strings
Support for double quoted strings
Diffstat (limited to 'src/NXP/MathExecutor.php')
-rw-r--r-- | src/NXP/MathExecutor.php | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index c5c7add..d62d07a 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -15,6 +15,7 @@ use NXP\Classes\Calculator; use NXP\Classes\Lexer; use NXP\Classes\Token; use NXP\Classes\TokenFactory; +use NXP\Exception\UnknownVariableException; /** * Class MathExecutor @@ -27,7 +28,7 @@ class MathExecutor * * @var array */ - private $variables = array(); + private $variables = []; /** * @var TokenFactory @@ -37,7 +38,7 @@ class MathExecutor /** * @var array */ - private $cache = array(); + private $cache = []; /** * Base math operators @@ -75,10 +76,36 @@ class MathExecutor $this->tokenFactory->addFunction('max', 'max', 2); $this->tokenFactory->addFunction('avg', function($arg1, $arg2) { return ($arg1 + $arg2) / 2; }, 2); - $this->setVars(array( + $this->setVars([ 'pi' => 3.14159265359, 'e' => 2.71828182846 - )); + ]); + } + + /** + * Get all vars + * + * @return array + */ + public function getVars() + { + return $this->variables; + } + + /** + * Get a specific var + * + * @param string $variable + * @return integer|float + * @throws UnknownVariableException + */ + public function getVar($variable) + { + if (! isset($this->variables[$variable])) { + throw new UnknownVariableException("Variable ({$variable}) not set"); + } + + return $this->variables[$variable]; } /** @@ -86,7 +113,6 @@ class MathExecutor * * @param string $variable * @param integer|float $value - * @throws \Exception * @return MathExecutor */ public function setVar($variable, $value) @@ -138,7 +164,7 @@ class MathExecutor */ public function removeVars() { - $this->variables = array(); + $this->variables = []; return $this; } |