aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Kiryukhin <alexander@kiryukhin.su>2018-09-06 20:39:56 +0300
committerGitHub <noreply@github.com>2018-09-06 20:39:56 +0300
commitaa1a092a9e231e8f923ab40365dff092c2908903 (patch)
treec192d6dd2948e83122893ab67228141b271c2d22
parent855ca5dfc1a6d70d9872df4b0d7bea8ba3c4c040 (diff)
parent3011a1c55686ef5a0c7759ae1efcb2a71623ab9c (diff)
Merge pull request #26 from phpfui/getters_for_function_and_operators
Getters for function and operators
-rw-r--r--README.md10
-rw-r--r--src/NXP/Classes/TokenFactory.php22
-rw-r--r--src/NXP/MathExecutor.php23
-rw-r--r--tests/MathTest.php12
4 files changed, 64 insertions, 3 deletions
diff --git a/README.md b/README.md
index 8c611b8..2fdb5c1 100644
--- a/README.md
+++ b/README.md
@@ -7,7 +7,15 @@ Simple math expressions calculator
## Install via Composer
-All instructions to install here: https://packagist.org/packages/nxp/math-executor
+Stable branch
+```
+composer require "nxp/math-executor" "dev-master"
+```
+
+Dev branch
+```
+composer require "nxp/math-executor" "dev-dev"
+```
## Sample usage:
diff --git a/src/NXP/Classes/TokenFactory.php b/src/NXP/Classes/TokenFactory.php
index 19ba1cf..2b1e00e 100644
--- a/src/NXP/Classes/TokenFactory.php
+++ b/src/NXP/Classes/TokenFactory.php
@@ -51,6 +51,18 @@ class TokenFactory
$this->functions[$name] = array($places, $function);
}
+
+ /**
+ * get functions
+ *
+ * @return array containing callback and places indexed by
+ * function name
+ */
+ public function getFunctions()
+ {
+ return $this->functions;
+ }
+
/**
* Add operator
* @param string $operatorClass
@@ -69,6 +81,16 @@ class TokenFactory
}
/**
+ * Get registered operators
+ *
+ * @return array of operator class names
+ */
+ public function getOperators()
+ {
+ return $this->operators;
+ }
+
+ /**
* Add variable
* @param string $name
* @param mixed $value
diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php
index 5cb4bca..9e7ce25 100644
--- a/src/NXP/MathExecutor.php
+++ b/src/NXP/MathExecutor.php
@@ -153,6 +153,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
@@ -160,7 +170,7 @@ class MathExecutor
* @param int $places Count of arguments
* @return MathExecutor
*/
- public function addFunction($name, callable $function = null, $places = 1)
+ public function addFunction($name, $function = null, $places = 1)
{
$this->tokenFactory->addFunction($name, $function, $places);
@@ -168,6 +178,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
diff --git a/tests/MathTest.php b/tests/MathTest.php
index a83a0d4..9bf8d97 100644
--- a/tests/MathTest.php
+++ b/tests/MathTest.php
@@ -74,4 +74,14 @@ class MathTest extends \PHPUnit_Framework_TestCase
array('100500 * 3.5E-5')
);
}
-}
+
+ public function testFunction()
+ {
+ $calculator = new MathExecutor();
+
+ $calculator->addFunction('round', function ($arg) { return round($arg); }, 1);
+ /** @var float $phpResult */
+ eval('$phpResult = round(100/30);');
+ $this->assertEquals($calculator->execute('round(100/30)'), $phpResult);
+ }
+} \ No newline at end of file