diff options
Diffstat (limited to 'src/NXP/MathExecutor.php')
-rw-r--r-- | src/NXP/MathExecutor.php | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index 1debc50..ca27539 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -412,22 +412,26 @@ class MathExecutor /** * Execute expression * - * @param $expression + * @param string $expression + * @param bool $cache * @return number - * @throws Exception\IncorrectExpressionException * @throws Exception\IncorrectBracketsException + * @throws Exception\IncorrectExpressionException * @throws Exception\UnknownOperatorException - * @throws Exception\UnknownVariableException + * @throws UnknownVariableException */ - public function execute(string $expression) + public function execute(string $expression, bool $cache = true) { $cachekey = $expression; if (!array_key_exists($cachekey, $this->cache)) { $tokens = (new Tokenizer($expression, $this->operators))->tokenize()->buildReversePolishNotation(); - $this->cache[$cachekey] = $tokens; + if ($cache) { + $this->cache[$cachekey] = $tokens; + } } else { $tokens = $this->cache[$cachekey]; } + $calculator = new Calculator($this->functions, $this->operators); return $calculator->calculate($tokens, $this->variables, $this->onVarNotFound); } @@ -596,6 +600,23 @@ class MathExecutor return $this; } + /** + * Get cache array with tokens + * @return array + */ + public function getCache() : array + { + return $this->cache; + } + + /** + * Clear token's cache + */ + public function clearCache() : void + { + $this->cache = []; + } + public function __clone() { $this->addDefaults(); |