From e3bac0c101b8a6c762eda0df66fbfbda448b476c Mon Sep 17 00:00:00 2001 From: NeonXP Date: Fri, 6 Sep 2013 17:30:48 +0400 Subject: ~Change readme --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ea6d31b..b921f70 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,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" "v0.1" +``` + +Dev branch +``` +composer require "nxp/math-executor" "dev-master" +``` ## Sample usage: -- cgit v1.2.3 From ec9e06742193092f00454ca734e5defd28459759 Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Fri, 6 Sep 2013 17:33:47 +0400 Subject: Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b921f70..f32427a 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,12 @@ Simple math expressions calculator Stable branch ``` -composer require "nxp/math-executor" "v0.1" +composer require "nxp/math-executor" "dev-master" ``` Dev branch ``` -composer require "nxp/math-executor" "dev-master" +composer require "nxp/math-executor" "dev-dev" ``` ## Sample usage: -- cgit v1.2.3 From 4b092895bc510efb6cc329b325a0426e720f05d5 Mon Sep 17 00:00:00 2001 From: NeonXP Date: Wed, 18 Sep 2013 20:27:46 +0400 Subject: ~ remove callable for PHP 5.3 --- src/NXP/MathExecutor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index 7eaa914..f348355 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -164,7 +164,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); -- cgit v1.2.3 From 0b7e6b1b860f303d6c71a0d0d1692220dcb53dc4 Mon Sep 17 00:00:00 2001 From: NeonXP Date: Wed, 18 Sep 2013 20:34:51 +0400 Subject: + added test for custom user functions --- tests/MathTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/MathTest.php b/tests/MathTest.php index 1b96ee2..2b2727f 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -62,4 +62,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 -- cgit v1.2.3 From af2436d7cc555216526052b901583c0e75dddb24 Mon Sep 17 00:00:00 2001 From: Bruce Wells Date: Mon, 27 Aug 2018 17:47:28 -0400 Subject: Exception Messages Basically the token that is causing the exception for better diagnostics. --- src/NXP/Classes/Calculator.php | 2 +- src/NXP/Classes/TokenFactory.php | 6 +++--- src/NXP/MathExecutor.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/NXP/Classes/Calculator.php b/src/NXP/Classes/Calculator.php index 41e0d9f..b584d69 100644 --- a/src/NXP/Classes/Calculator.php +++ b/src/NXP/Classes/Calculator.php @@ -40,7 +40,7 @@ class Calculator if ($token instanceof TokenVariable) { $variable = $token->getValue(); if (!array_key_exists($variable, $variables)) { - throw new UnknownVariableException(); + throw new UnknownVariableException($variable); } $value = $variables[$variable]; array_push($stack, new TokenNumber($value)); diff --git a/src/NXP/Classes/TokenFactory.php b/src/NXP/Classes/TokenFactory.php index 19ba1cf..07b9ea8 100644 --- a/src/NXP/Classes/TokenFactory.php +++ b/src/NXP/Classes/TokenFactory.php @@ -61,7 +61,7 @@ class TokenFactory $class = new \ReflectionClass($operatorClass); if (!in_array('NXP\Classes\Token\InterfaceToken', $class->getInterfaceNames())) { - throw new UnknownOperatorException; + throw new UnknownOperatorException($operatorClass); } $this->operators[] = $operatorClass; @@ -140,10 +140,10 @@ class TokenFactory if (isset($this->functions[$token])) { return new TokenFunction($this->functions[$token]); } else { - throw new UnknownFunctionException(); + throw new UnknownFunctionException($token); } } - throw new UnknownTokenException(); + throw new UnknownTokenException($token); } } diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index f348355..02b034f 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -92,7 +92,7 @@ class MathExecutor public function setVar($variable, $value) { if (!is_numeric($value)) { - throw new \Exception("Variable value must be a number"); + throw new \Exception("Variable ({$variable}) value must be a number ({$value})"); } $this->variables[$variable] = $value; -- cgit v1.2.3 From 29d32a155f9956d1041a4420c19986565f695040 Mon Sep 17 00:00:00 2001 From: Bruce Wells Date: Fri, 31 Aug 2018 12:22:58 -0400 Subject: Add ability to get functions and operators that have been registered --- src/NXP/Classes/TokenFactory.php | 22 ++++++++++++++++++++++ src/NXP/MathExecutor.php | 21 +++++++++++++++++++++ 2 files changed, 43 insertions(+) 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 @@ -68,6 +80,16 @@ class TokenFactory $this->operators = array_unique($this->operators); } + /** + * Get registered operators + * + * @return array of operator class names + */ + public function getOperators() + { + return $this->operators; + } + /** * Add variable * @param string $name diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index f348355..0c3d185 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -156,6 +156,16 @@ class MathExecutor return $this; } + /** + * Get all registered operators to executor + * + * @return array of operator class names + */ + public function getOperators() + { + return $this->tokenFactory->getOperators(); + } + /** * Add function to executor * @@ -171,6 +181,17 @@ class MathExecutor return $this; } + /** + * Get all registered functions + * + * @return array containing callback and places indexed by + * function name + */ + public function getFunctions() + { + return $this->tokenFactory->getFunctions(); + } + /** * Execute expression * -- cgit v1.2.3 From 7db873a63655258bd30ff2d83f186da48f2c50e1 Mon Sep 17 00:00:00 2001 From: Bruce Wells Date: Wed, 5 Sep 2018 18:17:05 -0400 Subject: Added variable type to diagnostic message --- src/NXP/MathExecutor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index 02b034f..80f0bde 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -92,7 +92,7 @@ class MathExecutor public function setVar($variable, $value) { if (!is_numeric($value)) { - throw new \Exception("Variable ({$variable}) value must be a number ({$value})"); + throw new \Exception("Variable ({$variable}) value must be a number ({$value}) type ({gettype($value)})"); } $this->variables[$variable] = $value; -- cgit v1.2.3 From 76d1b4b8f02d555e4b4f4fd4d641597f0f6b5f4e Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Thu, 6 Sep 2018 21:12:54 +0300 Subject: Update .travis.yml --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 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 -- cgit v1.2.3