diff options
author | Bruce Wells <bruce.wells@simparel.com> | 2019-01-11 04:29:20 +0300 |
---|---|---|
committer | Bruce Wells <bruce.wells@simparel.com> | 2019-01-11 04:32:22 +0300 |
commit | e03df64281a1c33639b13c9f490d4452c1a1784d (patch) | |
tree | 449ad548f128c09503ef62df570a8d700ca9145d | |
parent | 18b12aeeff34c8ac9a350165ae36f08f4138dc9c (diff) |
Fixed function argument ordering and default parameter type for addFunction
Updated unit tests
Fixed docs
-rw-r--r-- | README.md | 8 | ||||
-rw-r--r-- | src/NXP/Classes/Token/TokenFunction.php | 2 | ||||
-rw-r--r-- | src/NXP/MathExecutor.php | 2 | ||||
-rw-r--r-- | tests/MathTest.php | 8 |
4 files changed, 14 insertions, 6 deletions
@@ -41,11 +41,11 @@ Default functions: Add custom function to executor: ```php -$executor->addFunction('abs', function($arg) { - return abs($arg); -}, 1); +$executor->addFunction('abs', function($arg) {return abs($arg);}); ``` +Default parameters are not currently supported. + ## Operators: Default operators: `+ - * / ^` @@ -113,7 +113,7 @@ Default variables: ``` $pi = 3.14159265359 -$e = 2.71828182846 +$e = 2.71828182846 ``` You can add your own variables to executor: diff --git a/src/NXP/Classes/Token/TokenFunction.php b/src/NXP/Classes/Token/TokenFunction.php index b2866c3..d61bb5d 100644 --- a/src/NXP/Classes/Token/TokenFunction.php +++ b/src/NXP/Classes/Token/TokenFunction.php @@ -32,7 +32,7 @@ class TokenFunction extends AbstractContainerToken implements InterfaceFunction $args = []; list($places, $function) = $this->value; for ($i = 0; $i < $places; $i++) { - array_push($args, array_pop($stack)->getValue()); + $args[] = array_shift($stack)->getValue(); } $result = call_user_func_array($function, $args); diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index 29c6a64..6325d35 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -174,7 +174,7 @@ class MathExecutor * @return MathExecutor * @throws \ReflectionException */ - public function addFunction($name, $function = null, $places = 1) + public function addFunction($name, $function = null, $places = null) { $this->tokenFactory->addFunction($name, $function, $places); diff --git a/tests/MathTest.php b/tests/MathTest.php index 55e0799..d1c9604 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -146,6 +146,14 @@ class MathTest extends \PHPUnit_Framework_TestCase $this->assertEquals($calculator->execute('round(100/30)'), $phpResult); } + public function testFunctionsWithQuotes() + { + $calculator = new MathExecutor(); + $calculator->addFunction('concat', function($first, $second){return $first.$second;}); + $this->assertEquals('testing', $calculator->execute('concat("test", "ing")')); + $this->assertEquals('testing', $calculator->execute("concat('test', 'ing')")); + } + public function testQuotes() { $calculator = new MathExecutor(); |