Update README.md and more function support
This commit is contained in:
parent
44a13487b5
commit
84f3f967b7
4 changed files with 205 additions and 31 deletions
67
README.md
67
README.md
|
@ -4,6 +4,9 @@
|
|||
|
||||
## Features:
|
||||
* Built in support for +, -, *, / and power (^) operators plus ()
|
||||
* Logical operators (==, !=, <, <, >=, <=, &&, ||)
|
||||
* Built in support for most PHP math functions
|
||||
* Conditional If logic
|
||||
* Support for user defined operators
|
||||
* Support for user defined functions
|
||||
* Unlimited variable name lengths
|
||||
|
@ -20,24 +23,54 @@ composer require "nxp/math-executor"
|
|||
|
||||
## Sample usage:
|
||||
```php
|
||||
require "vendor/autoload.php";
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
$executor = new \NXP\MathExecutor();
|
||||
|
||||
echo $executor->execute("1 + 2 * (2 - (4+10))^2 + sin(10)");
|
||||
echo $executor->execute('1 + 2 * (2 - (4+10))^2 + sin(10)');
|
||||
```
|
||||
|
||||
## Functions:
|
||||
Default functions:
|
||||
* sin
|
||||
* cos
|
||||
* tn
|
||||
* asin
|
||||
* abs
|
||||
* acos
|
||||
* atn
|
||||
* min
|
||||
* max
|
||||
* acosh
|
||||
* asin
|
||||
* atan (atn)
|
||||
* atan2
|
||||
* atanh
|
||||
* avg
|
||||
* bindec
|
||||
* ceil
|
||||
* cos
|
||||
* cosh
|
||||
* decbin
|
||||
* dechex
|
||||
* decoct
|
||||
* deg2rad
|
||||
* exp
|
||||
* expm1
|
||||
* floor
|
||||
* fmod
|
||||
* hexdec
|
||||
* hypot
|
||||
* if
|
||||
* intdiv
|
||||
* log
|
||||
* log10
|
||||
* log1p
|
||||
* max
|
||||
* min
|
||||
* octdec
|
||||
* pi
|
||||
* pow
|
||||
* rad2deg
|
||||
* round
|
||||
* sin
|
||||
* sinh
|
||||
* sqrt
|
||||
* tan (tn)
|
||||
* tanh
|
||||
|
||||
Add custom function to executor:
|
||||
```php
|
||||
|
@ -75,7 +108,7 @@ class ModulusToken extends AbstractOperator
|
|||
*/
|
||||
public function getPriority()
|
||||
{
|
||||
return 3;
|
||||
return 180;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -109,6 +142,18 @@ And adding to executor:
|
|||
$executor->addOperator('MyNamespace\ModulusToken');
|
||||
```
|
||||
|
||||
## Logical operators:
|
||||
Logical operators (==, !=, <, <, >=, <=, &&, ||) are supported, but logically they can only return true (1) or false (0). In order to leverage them, use the built in **if** function:
|
||||
|
||||
```
|
||||
if($a > $b, $a - $b, $b - $a)
|
||||
```
|
||||
|
||||
You can think of the **if** function as prototyped like:
|
||||
|
||||
```
|
||||
function if($condition, $returnIfTrue, $returnIfFalse)
|
||||
```
|
||||
## Variables:
|
||||
Default variables:
|
||||
|
||||
|
@ -161,4 +206,4 @@ Also note that you can replace an existing default operator by adding a new oper
|
|||
|
||||
## Future Enhancements
|
||||
|
||||
At some point this package will be upgraded to a currently supported version of PHP.
|
||||
This package will continue to track currently supported versions of PHP. We recommend you keep PHP up-to-date. Currently the code will run under 5.6, but don't expect 5.6 support going forward.
|
||||
|
|
|
@ -4,7 +4,7 @@ namespace NXP\Classes\Token;
|
|||
|
||||
use NXP\Exception\IncorrectExpressionException;
|
||||
|
||||
class TokenUnequal extends AbstractOperator
|
||||
class TokenNotEqual extends AbstractOperator
|
||||
{
|
||||
/**
|
||||
* @return string
|
|
@ -267,7 +267,7 @@ class MathExecutor
|
|||
'NXP\Classes\Token\TokenAnd',
|
||||
'NXP\Classes\Token\TokenOr',
|
||||
'NXP\Classes\Token\TokenEqual',
|
||||
'NXP\Classes\Token\TokenUnequal',
|
||||
'NXP\Classes\Token\TokenNotEqual',
|
||||
'NXP\Classes\Token\TokenGreaterThanOrEqual',
|
||||
'NXP\Classes\Token\TokenGreaterThan',
|
||||
'NXP\Classes\Token\TokenLessThanOrEqual',
|
||||
|
@ -278,33 +278,75 @@ class MathExecutor
|
|||
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);
|
||||
'abs' => function ($arg) {
|
||||
return abs($arg);
|
||||
},
|
||||
'acos' => function ($arg) {
|
||||
return acos($arg);
|
||||
},
|
||||
'acosh' => function ($arg) {
|
||||
return acosh($arg);
|
||||
},
|
||||
'asin' => function ($arg) {
|
||||
return asin($arg);
|
||||
},
|
||||
'atan' => function ($arg) {
|
||||
return atan($arg);
|
||||
},
|
||||
'atan2' => function ($arg1, $arg2) {
|
||||
return atan2($arg1, $arg2);
|
||||
},
|
||||
'atanh' => function ($arg) {
|
||||
return atanh($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;
|
||||
},
|
||||
'bindec' => function ($arg) {
|
||||
return bindec($arg);
|
||||
},
|
||||
'ceil' => function ($arg) {
|
||||
return ceil($arg);
|
||||
},
|
||||
'cos' => function ($arg) {
|
||||
return cos($arg);
|
||||
},
|
||||
'cosh' => function ($arg) {
|
||||
return cosh($arg);
|
||||
},
|
||||
'decbin' => function ($arg) {
|
||||
return decbin($arg);
|
||||
},
|
||||
'dechex' => function ($arg) {
|
||||
return dechex($arg);
|
||||
},
|
||||
'decoct' => function ($arg) {
|
||||
return decoct($arg);
|
||||
},
|
||||
'deg2rad' => function ($arg) {
|
||||
return deg2rad($arg);
|
||||
},
|
||||
'exp' => function ($arg) {
|
||||
return exp($arg);
|
||||
},
|
||||
'expm1' => function ($arg) {
|
||||
return expm1($arg);
|
||||
},
|
||||
'floor' => function ($arg) {
|
||||
return floor($arg);
|
||||
},
|
||||
'fmod' => function ($arg1, $arg2) {
|
||||
return fmod($arg1, $arg2);
|
||||
},
|
||||
'hexdec' => function ($arg) {
|
||||
return hexdec($arg);
|
||||
},
|
||||
'hypot' => function ($arg1, $arg2) {
|
||||
return hypot($arg1, $arg2);
|
||||
},
|
||||
'if' => function ($expr, $trueval, $falseval) {
|
||||
if ($expr === true || $expr === false) {
|
||||
$exres = $expr;
|
||||
|
@ -316,6 +358,57 @@ class MathExecutor
|
|||
} else {
|
||||
return $this->execute($falseval);
|
||||
}
|
||||
},
|
||||
'intdiv' => function ($arg1, $arg2) {
|
||||
return intdiv($arg1, $arg2);
|
||||
},
|
||||
'log' => function ($arg) {
|
||||
return log($arg);
|
||||
},
|
||||
'log10' => function ($arg) {
|
||||
return log10($arg);
|
||||
},
|
||||
'log1p' => function ($arg) {
|
||||
return log1p($arg);
|
||||
},
|
||||
'max' => function ($arg1, $arg2) {
|
||||
return max($arg1, $arg2);
|
||||
},
|
||||
'min' => function ($arg1, $arg2) {
|
||||
return min($arg1, $arg2);
|
||||
},
|
||||
'octdec' => function ($arg) {
|
||||
return octdec($arg);
|
||||
},
|
||||
'pi' => function () {
|
||||
return pi();
|
||||
},
|
||||
'pow' => function ($arg1, $arg2) {
|
||||
return pow($arg1, $arg2);
|
||||
},
|
||||
'rad2deg' => function ($arg) {
|
||||
return rad2deg($arg);
|
||||
},
|
||||
'round' => function ($arg) {
|
||||
return round($arg);
|
||||
},
|
||||
'sin' => function ($arg) {
|
||||
return sin($arg);
|
||||
},
|
||||
'sinh' => function ($arg) {
|
||||
return sinh($arg);
|
||||
},
|
||||
'sqrt' => function ($arg) {
|
||||
return sqrt($arg);
|
||||
},
|
||||
'tan' => function ($arg) {
|
||||
return tan($arg);
|
||||
},
|
||||
'tanh' => function ($arg) {
|
||||
return tanh($arg);
|
||||
},
|
||||
'tn' => function ($arg) {
|
||||
return tan($arg);
|
||||
}
|
||||
];
|
||||
}
|
||||
|
|
|
@ -50,7 +50,43 @@ class MathTest extends \PHPUnit\Framework\TestCase
|
|||
['4*-5'],
|
||||
['4 * -5'],
|
||||
|
||||
[cos(2)],
|
||||
['abs(1.5)'],
|
||||
['acos(0.15)'],
|
||||
['acosh(1.5)'],
|
||||
['asin(0.15)'],
|
||||
['atan(0.15)'],
|
||||
['atan2(1.5, 3.5)'],
|
||||
['atanh(0.15)'],
|
||||
['bindec("10101")'],
|
||||
['ceil(1.5)'],
|
||||
['cos(1.5)'],
|
||||
['cosh(1.5)'],
|
||||
['decbin("15")'],
|
||||
['dechex("15")'],
|
||||
['decoct("15")'],
|
||||
['deg2rad(1.5)'],
|
||||
['exp(1.5)'],
|
||||
['expm1(1.5)'],
|
||||
['floor(1.5)'],
|
||||
['fmod(1.5, 3.5)'],
|
||||
['hexdec("abcdef")'],
|
||||
['hypot(1.5, 3.5)'],
|
||||
['intdiv(10, 2)'],
|
||||
['log(1.5)'],
|
||||
['log10(1.5)'],
|
||||
['log1p(1.5)'],
|
||||
['max(1.5, 3.5)'],
|
||||
['min(1.5, 3.5)'],
|
||||
['octdec(1.5)'],
|
||||
['pi()'],
|
||||
['pow(1.5, 3.5)'],
|
||||
['rad2deg(1.5)'],
|
||||
['round(1.5)'],
|
||||
['sin(1.5)'],
|
||||
['sinh(1.5)'],
|
||||
['sqrt(1.5)'],
|
||||
['tan(1.5)'],
|
||||
['tanh(1.5)'],
|
||||
|
||||
['0.1 + 0.2'],
|
||||
['1 + 2'],
|
||||
|
|
Loading…
Reference in a new issue