aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimur <icq-475204@ya.ru>2019-01-10 01:51:21 +0300
committerBruce Wells <brucekwells@gmail.com>2019-01-10 01:51:21 +0300
commit808593bacfe50cb25d2edfd814135172295a884c (patch)
tree1562247fbd2c510343ad24d771c1c64f20519959
parente8ede3d6a56fdef00f14de7ae8699ed4ca890712 (diff)
MathExecutor allow override default operators, functions and vars (#36)
-rw-r--r--src/NXP/Classes/TokenFactory.php11
-rw-r--r--src/NXP/MathExecutor.php99
2 files changed, 78 insertions, 32 deletions
diff --git a/src/NXP/Classes/TokenFactory.php b/src/NXP/Classes/TokenFactory.php
index 6956f31..4ed55af 100644
--- a/src/NXP/Classes/TokenFactory.php
+++ b/src/NXP/Classes/TokenFactory.php
@@ -50,12 +50,17 @@ class TokenFactory
/**
* Add function
- * @param string $name
+ * @param string $name
* @param callable $function
- * @param int $places
+ * @param int $places
+ * @throws \ReflectionException
*/
- public function addFunction($name, callable $function, $places = 1)
+ public function addFunction($name, callable $function, $places = null)
{
+ if ($places === null) {
+ $reflector = new \ReflectionFunction($function);
+ $places = $reflector->getNumberOfParameters();
+ }
$this->functions[$name] = [$places, $function];
}
diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php
index fbf6ab1..706f8af 100644
--- a/src/NXP/MathExecutor.php
+++ b/src/NXP/MathExecutor.php
@@ -54,35 +54,6 @@ class MathExecutor
}
/**
- * Set default operands and functions
- */
- protected function addDefaults()
- {
- $this->tokenFactory = new TokenFactory();
-
- $this->tokenFactory->addOperator('NXP\Classes\Token\TokenPlus');
- $this->tokenFactory->addOperator('NXP\Classes\Token\TokenMinus');
- $this->tokenFactory->addOperator('NXP\Classes\Token\TokenMultiply');
- $this->tokenFactory->addOperator('NXP\Classes\Token\TokenDivision');
- $this->tokenFactory->addOperator('NXP\Classes\Token\TokenDegree');
-
- $this->tokenFactory->addFunction('sin', 'sin');
- $this->tokenFactory->addFunction('cos', 'cos');
- $this->tokenFactory->addFunction('tn', 'tan');
- $this->tokenFactory->addFunction('asin', 'asin');
- $this->tokenFactory->addFunction('acos', 'acos');
- $this->tokenFactory->addFunction('atn', 'atan');
- $this->tokenFactory->addFunction('min', 'min', 2);
- $this->tokenFactory->addFunction('max', 'max', 2);
- $this->tokenFactory->addFunction('avg', function($arg1, $arg2) { return ($arg1 + $arg2) / 2; }, 2);
-
- $this->setVars([
- 'pi' => 3.14159265359,
- 'e' => 2.71828182846
- ]);
- }
-
- /**
* Get all vars
*
* @return array
@@ -262,4 +233,74 @@ class MathExecutor
return $result;
}
+
+ /**
+ * Set default operands and functions
+ */
+ protected function addDefaults()
+ {
+ $this->tokenFactory = new TokenFactory();
+
+ foreach ($this->defaultOperators() as $operatorClass) {
+ $this->tokenFactory->addOperator($operatorClass);
+ }
+
+ foreach ($this->defaultFunctions() as $name => $callable) {
+ $this->tokenFactory->addFunction($name, $callable);
+ }
+
+ $this->setVars($this->defaultVars());
+ }
+
+ protected function defaultOperators()
+ {
+ return [
+ 'NXP\Classes\Token\TokenPlus',
+ 'NXP\Classes\Token\TokenMinus',
+ 'NXP\Classes\Token\TokenMultiply',
+ 'NXP\Classes\Token\TokenDivision',
+ 'NXP\Classes\Token\TokenDegree',
+ ];
+ }
+
+ protected function defaultFunctions()
+ {
+ return [
+ 'sin' => function ($arg) {
+ return sin($arg);
+ },
+ 'cos' => function ($arg) {
+ return cos($arg);
+ },
+ 'tn' => function ($arg) {
+ return tan($arg);
+ },
+ 'asin' => function ($arg) {
+ return asin($arg);
+ },
+ 'acos' => function ($arg) {
+ return acos($arg);
+ },
+ 'atn' => function ($arg) {
+ return atan($arg);
+ },
+ 'min' => function ($arg1, $arg2) {
+ return min($arg1, $arg2);
+ },
+ 'max' => function ($arg1, $arg2) {
+ return max($arg1, $arg2);
+ },
+ 'avg' => function ($arg1, $arg2) {
+ return ($arg1 + $arg2) / 2;
+ },
+ ];
+ }
+
+ protected function defaultVars()
+ {
+ return [
+ 'pi' => 3.14159265359,
+ 'e' => 2.71828182846
+ ];
+ }
}