aboutsummaryrefslogtreecommitdiff
path: root/src/NXP/MathExecutor.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/NXP/MathExecutor.php')
-rw-r--r--src/NXP/MathExecutor.php59
1 files changed, 53 insertions, 6 deletions
diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php
index 5cb4bca..56cefe5 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)
@@ -134,7 +160,7 @@ class MathExecutor
*/
public function removeVars()
{
- $this->variables = array();
+ $this->variables = [];
return $this;
}
@@ -153,6 +179,16 @@ class MathExecutor
}
/**
+ * Get all registered operators to executor
+ *
+ * @return array of operator class names
+ */
+ public function getOperators()
+ {
+ return $this->tokenFactory->getOperators();
+ }
+
+ /**
* Add function to executor
*
* @param string $name Name of function
@@ -168,6 +204,17 @@ class MathExecutor
}
/**
+ * Get all registered functions
+ *
+ * @return array containing callback and places indexed by
+ * function name
+ */
+ public function getFunctions()
+ {
+ return $this->tokenFactory->getFunctions();
+ }
+
+ /**
* Execute expression
*
* @param $expression