diff options
-rw-r--r-- | .travis.yml | 6 | ||||
-rw-r--r-- | README.md | 10 | ||||
-rw-r--r-- | src/NXP/Classes/TokenFactory.php | 12 | ||||
-rw-r--r-- | src/NXP/MathExecutor.php | 6 | ||||
-rw-r--r-- | tests/MathTest.php | 12 |
5 files changed, 40 insertions, 6 deletions
diff --git a/.travis.yml b/.travis.yml index 842031b..47d1020 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,9 @@ language: php php: - - 5.3 - - 5.4 + - 5.6 + - 7.2 before_script: - wget http://getcomposer.org/composer.phar - - php composer.phar install
\ No newline at end of file + - php composer.phar install @@ -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 dbe6624..e5aa865 100644 --- a/src/NXP/Classes/TokenFactory.php +++ b/src/NXP/Classes/TokenFactory.php @@ -64,6 +64,18 @@ class TokenFactory return $this->functions; } + + /** + * get functions + * + * @return array containing callback and places indexed by + * function name + */ + public function getFunctions() + { + return $this->functions; + } + /** * Add operator * @param string $operatorClass diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index 56cefe5..d62d07a 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -117,6 +117,10 @@ class MathExecutor */ public function setVar($variable, $value) { + if (!is_numeric($value)) { + throw new \Exception("Variable ({$variable}) value must be a number ({$value}) type ({gettype($value)})"); + } + $this->variables[$variable] = $value; return $this; @@ -196,7 +200,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); diff --git a/tests/MathTest.php b/tests/MathTest.php index 3fa7270..db6e486 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -74,4 +74,14 @@ class MathTest extends \PHPUnit_Framework_TestCase ['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 |