From 808593bacfe50cb25d2edfd814135172295a884c Mon Sep 17 00:00:00 2001
From: Timur <icq-475204@ya.ru>
Date: Thu, 10 Jan 2019 01:51:21 +0300
Subject: MathExecutor allow override default operators, functions and vars
 (#36)

---
 src/NXP/Classes/TokenFactory.php | 11 +++--
 src/NXP/MathExecutor.php         | 99 ++++++++++++++++++++++++++++------------
 2 files changed, 78 insertions(+), 32 deletions(-)

(limited to 'src/NXP')

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
@@ -53,35 +53,6 @@ class MathExecutor
         $this->addDefaults();
     }
 
-    /**
-     * 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
      *
@@ -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
+        ];
+    }
 }
-- 
cgit v1.2.3