Added Not(!) logical operator and median function with tests. (#121)

This commit is contained in:
Fatih Kızmaz 2022-12-26 18:35:27 +03:00 committed by GitHub
parent 84ac3bbdf0
commit 3a18c7d47f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 138 additions and 111 deletions

View file

@ -5,7 +5,7 @@
## Features:
* Built in support for +, -, *, /, % and power (^) operators
* Paratheses () and arrays [] are fully supported
* Logical operators (==, !=, <, <, >=, <=, &&, ||)
* Logical operators (==, !=, <, <, >=, <=, &&, ||, !)
* Built in support for most PHP math functions
* Support for BCMath Arbitrary Precision Math
* Support for variable number of function parameters and optional function parameters
@ -71,6 +71,7 @@ Default functions:
* log10 (lg)
* log1p
* max
* median
* min
* octdec
* pi
@ -125,7 +126,7 @@ $executor->addOperator(new Operator(
```
## 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:
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)

View file

@ -85,10 +85,10 @@ class MathExecutor
/**
* Execute expression
*
* @throws Exception\IncorrectBracketsException
* @throws Exception\IncorrectExpressionException
* @throws Exception\UnknownOperatorException
* @throws UnknownVariableException
* @throws Exception\IncorrectBracketsException
* @return int|float|string|null
*/
public function execute(string $expression, bool $cache = true)
@ -113,7 +113,7 @@ class MathExecutor
/**
* Add function to executor
*
* @param string $name Name of function
* @param string $name Name of function
* @param callable|null $function Function
*
* @throws ReflectionException
@ -182,8 +182,8 @@ class MathExecutor
/**
* Add variables to executor
*
* @param array<string, float|int|string> $variables
* @param bool $clear Clear previous variables
* @param array<string, float|int|string> $variables
* @param bool $clear Clear previous variables
* @throws \Exception
*/
public function setVars(array $variables, bool $clear = true) : self
@ -285,7 +285,7 @@ class MathExecutor
*/
public function setDivisionByZeroIsZero() : self
{
$this->addOperator(new Operator('/', false, 180, static fn ($a, $b) => 0 == $b ? 0 : $a / $b));
$this->addOperator(new Operator('/', false, 180, static fn($a, $b) => 0 == $b ? 0 : $a / $b));
return $this;
}
@ -311,23 +311,23 @@ class MathExecutor
public function useBCMath(int $scale = 2) : self
{
\bcscale($scale);
$this->addOperator(new Operator('+', false, 170, static fn ($a, $b) => \bcadd("{$a}", "{$b}")));
$this->addOperator(new Operator('-', false, 170, static fn ($a, $b) => \bcsub("{$a}", "{$b}")));
$this->addOperator(new Operator('uNeg', false, 200, static fn ($a) => \bcsub('0.0', "{$a}")));
$this->addOperator(new Operator('*', false, 180, static fn ($a, $b) => \bcmul("{$a}", "{$b}")));
$this->addOperator(new Operator('/', false, 180, static function($a, $b) {
/** @todo PHP8: Use throw as expression -> static fn($a, $b) => 0 == $b ? throw new DivisionByZeroException() : $a / $b */
if (0 == $b) {
throw new DivisionByZeroException();
}
\bcscale($scale);
$this->addOperator(new Operator('+', false, 170, static fn($a, $b) => \bcadd("{$a}", "{$b}")));
$this->addOperator(new Operator('-', false, 170, static fn($a, $b) => \bcsub("{$a}", "{$b}")));
$this->addOperator(new Operator('uNeg', false, 200, static fn($a) => \bcsub('0.0', "{$a}")));
$this->addOperator(new Operator('*', false, 180, static fn($a, $b) => \bcmul("{$a}", "{$b}")));
$this->addOperator(new Operator('/', false, 180, static function($a, $b) {
/** @todo PHP8: Use throw as expression -> static fn($a, $b) => 0 == $b ? throw new DivisionByZeroException() : $a / $b */
if (0 == $b) {
throw new DivisionByZeroException();
}
return \bcdiv("{$a}", "{$b}");
}));
$this->addOperator(new Operator('^', true, 220, static fn ($a, $b) => \bcpow("{$a}", "{$b}")));
$this->addOperator(new Operator('%', false, 180, static fn ($a, $b) => \bcmod("{$a}", "{$b}")));
return \bcdiv("{$a}", "{$b}");
}));
$this->addOperator(new Operator('^', true, 220, static fn($a, $b) => \bcpow("{$a}", "{$b}")));
$this->addOperator(new Operator('%', false, 180, static fn($a, $b) => \bcmod("{$a}", "{$b}")));
return $this;
return $this;
}
/**
@ -359,15 +359,16 @@ class MathExecutor
protected function defaultOperators() : array
{
return [
'+' => [static fn ($a, $b) => $a + $b, 170, false],
'-' => [static fn ($a, $b) => $a - $b, 170, false],
'+' => [static fn($a, $b) => $a + $b, 170, false],
'-' => [static fn($a, $b) => $a - $b, 170, false],
// unary positive token
'uPos' => [static fn ($a) => $a, 200, false],
'uPos' => [static fn($a) => $a, 200, false],
// unary minus token
'uNeg' => [static fn ($a) => 0 - $a, 200, false],
'*' => [static fn ($a, $b) => $a * $b, 180, false],
'uNeg' => [static fn($a) => 0 - $a, 200, false],
'*' => [static fn($a, $b) => $a * $b, 180, false],
'/' => [
static function($a, $b) { /** @todo PHP8: Use throw as expression -> static fn($a, $b) => 0 == $b ? throw new DivisionByZeroException() : $a / $b */
static function($a, $b) {
/** @todo PHP8: Use throw as expression -> static fn($a, $b) => 0 == $b ? throw new DivisionByZeroException() : $a / $b */
if (0 == $b) {
throw new DivisionByZeroException();
}
@ -377,16 +378,17 @@ class MathExecutor
180,
false
],
'^' => [static fn ($a, $b) => $a ** $b, 220, true],
'%' => [static fn ($a, $b) => $a % $b, 180, false],
'&&' => [static fn ($a, $b) => $a && $b, 100, false],
'||' => [static fn ($a, $b) => $a || $b, 90, false],
'==' => [static fn ($a, $b) => \is_string($a) || \is_string($b) ? 0 == \strcmp($a, $b) : $a == $b, 140, false],
'!=' => [static fn ($a, $b) => \is_string($a) || \is_string($b) ? 0 != \strcmp($a, $b) : $a != $b, 140, false],
'>=' => [static fn ($a, $b) => $a >= $b, 150, false],
'>' => [static fn ($a, $b) => $a > $b, 150, false],
'<=' => [static fn ($a, $b) => $a <= $b, 150, false],
'<' => [static fn ($a, $b) => $a < $b, 150, false],
'^' => [static fn($a, $b) => $a ** $b, 220, true],
'%' => [static fn($a, $b) => $a % $b, 180, false],
'&&' => [static fn($a, $b) => $a && $b, 100, false],
'||' => [static fn($a, $b) => $a || $b, 90, false],
'==' => [static fn($a, $b) => \is_string($a) || \is_string($b) ? 0 == \strcmp($a, $b) : $a == $b, 140, false],
'!=' => [static fn($a, $b) => \is_string($a) || \is_string($b) ? 0 != \strcmp($a, $b) : $a != $b, 140, false],
'>=' => [static fn($a, $b) => $a >= $b, 150, false],
'>' => [static fn($a, $b) => $a > $b, 150, false],
'<=' => [static fn($a, $b) => $a <= $b, 150, false],
'<' => [static fn($a, $b) => $a < $b, 150, false],
'!' => [static fn($a) => ! $a, 190, false],
];
}
@ -399,28 +401,28 @@ class MathExecutor
protected function defaultFunctions() : array
{
return [
'abs' => static fn ($arg) => \abs($arg),
'acos' => static fn ($arg) => \acos($arg),
'acosh' => static fn ($arg) => \acosh($arg),
'arcsin' => static fn ($arg) => \asin($arg),
'arcctg' => static fn ($arg) => M_PI / 2 - \atan($arg),
'arccot' => static fn ($arg) => M_PI / 2 - \atan($arg),
'arccotan' => static fn ($arg) => M_PI / 2 - \atan($arg),
'arcsec' => static fn ($arg) => \acos(1 / $arg),
'arccosec' => static fn ($arg) => \asin(1 / $arg),
'arccsc' => static fn ($arg) => \asin(1 / $arg),
'arccos' => static fn ($arg) => \acos($arg),
'arctan' => static fn ($arg) => \atan($arg),
'arctg' => static fn ($arg) => \atan($arg),
'array' => static fn (...$args) => $args,
'asin' => static fn ($arg) => \asin($arg),
'atan' => static fn ($arg) => \atan($arg),
'atan2' => static fn ($arg1, $arg2) => \atan2($arg1, $arg2),
'atanh' => static fn ($arg) => \atanh($arg),
'atn' => static fn ($arg) => \atan($arg),
'abs' => static fn($arg) => \abs($arg),
'acos' => static fn($arg) => \acos($arg),
'acosh' => static fn($arg) => \acosh($arg),
'arcsin' => static fn($arg) => \asin($arg),
'arcctg' => static fn($arg) => M_PI / 2 - \atan($arg),
'arccot' => static fn($arg) => M_PI / 2 - \atan($arg),
'arccotan' => static fn($arg) => M_PI / 2 - \atan($arg),
'arcsec' => static fn($arg) => \acos(1 / $arg),
'arccosec' => static fn($arg) => \asin(1 / $arg),
'arccsc' => static fn($arg) => \asin(1 / $arg),
'arccos' => static fn($arg) => \acos($arg),
'arctan' => static fn($arg) => \atan($arg),
'arctg' => static fn($arg) => \atan($arg),
'array' => static fn(...$args) => $args,
'asin' => static fn($arg) => \asin($arg),
'atan' => static fn($arg) => \atan($arg),
'atan2' => static fn($arg1, $arg2) => \atan2($arg1, $arg2),
'atanh' => static fn($arg) => \atanh($arg),
'atn' => static fn($arg) => \atan($arg),
'avg' => static function($arg1, ...$args) {
if (\is_array($arg1)){
if (0 === \count($arg1)){
if (\is_array($arg1)) {
if (0 === \count($arg1)) {
throw new \InvalidArgumentException('avg() must have at least one argument!');
}
@ -431,27 +433,27 @@ class MathExecutor
return \array_sum($args) / \count($args);
},
'bindec' => static fn ($arg) => \bindec($arg),
'ceil' => static fn ($arg) => \ceil($arg),
'cos' => static fn ($arg) => \cos($arg),
'cosec' => static fn ($arg) => 1 / \sin($arg),
'csc' => static fn ($arg) => 1 / \sin($arg),
'cosh' => static fn ($arg) => \cosh($arg),
'ctg' => static fn ($arg) => \cos($arg) / \sin($arg),
'cot' => static fn ($arg) => \cos($arg) / \sin($arg),
'cotan' => static fn ($arg) => \cos($arg) / \sin($arg),
'cotg' => static fn ($arg) => \cos($arg) / \sin($arg),
'ctn' => static fn ($arg) => \cos($arg) / \sin($arg),
'decbin' => static fn ($arg) => \decbin($arg),
'dechex' => static fn ($arg) => \dechex($arg),
'decoct' => static fn ($arg) => \decoct($arg),
'deg2rad' => static fn ($arg) => \deg2rad($arg),
'exp' => static fn ($arg) => \exp($arg),
'expm1' => static fn ($arg) => \expm1($arg),
'floor' => static fn ($arg) => \floor($arg),
'fmod' => static fn ($arg1, $arg2) => \fmod($arg1, $arg2),
'hexdec' => static fn ($arg) => \hexdec($arg),
'hypot' => static fn ($arg1, $arg2) => \hypot($arg1, $arg2),
'bindec' => static fn($arg) => \bindec($arg),
'ceil' => static fn($arg) => \ceil($arg),
'cos' => static fn($arg) => \cos($arg),
'cosec' => static fn($arg) => 1 / \sin($arg),
'csc' => static fn($arg) => 1 / \sin($arg),
'cosh' => static fn($arg) => \cosh($arg),
'ctg' => static fn($arg) => \cos($arg) / \sin($arg),
'cot' => static fn($arg) => \cos($arg) / \sin($arg),
'cotan' => static fn($arg) => \cos($arg) / \sin($arg),
'cotg' => static fn($arg) => \cos($arg) / \sin($arg),
'ctn' => static fn($arg) => \cos($arg) / \sin($arg),
'decbin' => static fn($arg) => \decbin($arg),
'dechex' => static fn($arg) => \dechex($arg),
'decoct' => static fn($arg) => \decoct($arg),
'deg2rad' => static fn($arg) => \deg2rad($arg),
'exp' => static fn($arg) => \exp($arg),
'expm1' => static fn($arg) => \expm1($arg),
'floor' => static fn($arg) => \floor($arg),
'fmod' => static fn($arg1, $arg2) => \fmod($arg1, $arg2),
'hexdec' => static fn($arg) => \hexdec($arg),
'hypot' => static fn($arg1, $arg2) => \hypot($arg1, $arg2),
'if' => function($expr, $trueval, $falseval) {
if (true === $expr || false === $expr) {
$exres = $expr;
@ -465,39 +467,56 @@ class MathExecutor
return $this->execute($falseval);
},
'intdiv' => static fn ($arg1, $arg2) => \intdiv($arg1, $arg2),
'ln' => static fn ($arg) => \log($arg),
'lg' => static fn ($arg) => \log10($arg),
'log' => static fn ($arg) => \log($arg),
'log10' => static fn ($arg) => \log10($arg),
'log1p' => static fn ($arg) => \log1p($arg),
'intdiv' => static fn($arg1, $arg2) => \intdiv($arg1, $arg2),
'ln' => static fn($arg) => \log($arg),
'lg' => static fn($arg) => \log10($arg),
'log' => static fn($arg) => \log($arg),
'log10' => static fn($arg) => \log10($arg),
'log1p' => static fn($arg) => \log1p($arg),
'max' => static function($arg1, ...$args) {
if (\is_array($arg1) && 0 === \count($arg1)){
if (\is_array($arg1) && 0 === \count($arg1)) {
throw new \InvalidArgumentException('max() must have at least one argument!');
}
return \max(\is_array($arg1) ? $arg1 : [$arg1, ...$args]);
},
'median' => static function($arg1, ...$args) {
if (\is_array($arg1)) {
if (0 === \count($arg1)) {
throw new \InvalidArgumentException('Array must contain at least one element!');
}
$finalArgs = $arg1;
} else {
$finalArgs = [$arg1, ...$args];
}
$count = \count($finalArgs);
\sort($finalArgs);
$index = \floor($count / 2);
return ($count & 1) ? $finalArgs[$index] : ($finalArgs[$index - 1] + $finalArgs[$index]) / 2;
},
'min' => static function($arg1, ...$args) {
if (\is_array($arg1) && 0 === \count($arg1)){
if (\is_array($arg1) && 0 === \count($arg1)) {
throw new \InvalidArgumentException('min() must have at least one argument!');
}
return \min(\is_array($arg1) ? $arg1 : [$arg1, ...$args]);
},
'octdec' => static fn ($arg) => \octdec($arg),
'pi' => static fn () => M_PI,
'pow' => static fn ($arg1, $arg2) => $arg1 ** $arg2,
'rad2deg' => static fn ($arg) => \rad2deg($arg),
'round' => static fn ($num, int $precision = 0) => \round($num, $precision),
'sin' => static fn ($arg) => \sin($arg),
'sinh' => static fn ($arg) => \sinh($arg),
'sec' => static fn ($arg) => 1 / \cos($arg),
'sqrt' => static fn ($arg) => \sqrt($arg),
'tan' => static fn ($arg) => \tan($arg),
'tanh' => static fn ($arg) => \tanh($arg),
'tn' => static fn ($arg) => \tan($arg),
'tg' => static fn ($arg) => \tan($arg)
'octdec' => static fn($arg) => \octdec($arg),
'pi' => static fn() => M_PI,
'pow' => static fn($arg1, $arg2) => $arg1 ** $arg2,
'rad2deg' => static fn($arg) => \rad2deg($arg),
'round' => static fn($num, int $precision = 0) => \round($num, $precision),
'sin' => static fn($arg) => \sin($arg),
'sinh' => static fn($arg) => \sinh($arg),
'sec' => static fn($arg) => 1 / \cos($arg),
'sqrt' => static fn($arg) => \sqrt($arg),
'tan' => static fn($arg) => \tan($arg),
'tanh' => static fn($arg) => \tanh($arg),
'tn' => static fn($arg) => \tan($arg),
'tg' => static fn($arg) => \tan($arg)
];
}

View file

@ -256,6 +256,8 @@ class MathTest extends TestCase
['7 % 4'],
['99 % 4'],
['123 % 7'],
['!(1||0)'],
['!(1&&0)'],
];
}
@ -493,6 +495,8 @@ class MathTest extends TestCase
['7 % 4'],
['99 % 4'],
['123 % 7'],
['!(1||0)'],
['!(1&&0)'],
];
}
@ -600,7 +604,7 @@ class MathTest extends TestCase
$this->assertEquals("'teststring", $calculator->execute("'\'teststring'"));
$this->assertEquals("teststring'", $calculator->execute("'teststring\''"));
$calculator->addFunction('concat', static fn ($arg1, $arg2) => $arg1 . $arg2);
$calculator->addFunction('concat', static fn($arg1, $arg2) => $arg1 . $arg2);
$this->assertEquals('test"ing', $calculator->execute('concat("test\"","ing")'));
$this->assertEquals("test'ing", $calculator->execute("concat('test\'','ing')"));
}
@ -614,7 +618,7 @@ class MathTest extends TestCase
$this->assertEquals(\max([1, 5, 2]), $calculator->execute('max(array(1, 5, 2))'));
$calculator->addFunction('arr_with_max_elements', static function($arg1, ...$args) {
$args = \is_array($arg1) ? $arg1 : [$arg1, ...$args];
\usort($args, static fn ($arr1, $arr2) => (\is_countable($arr2) ? \count($arr2) : 0) <=> \count($arr1));
\usort($args, static fn($arr1, $arr2) => (\is_countable($arr2) ? \count($arr2) : 0) <=> \count($arr1));
return $args[0];
});
@ -625,7 +629,7 @@ class MathTest extends TestCase
{
$calculator = new MathExecutor();
$calculator->addFunction('concat', static fn ($arg1, $arg2) => $arg1 . $arg2);
$calculator->addFunction('concat', static fn($arg1, $arg2) => $arg1 . $arg2);
$this->assertEquals('testing', $calculator->execute('concat("test","ing")'));
$this->assertEquals('testing', $calculator->execute("concat('test','ing')"));
}
@ -633,31 +637,34 @@ class MathTest extends TestCase
public function testFunction() : void
{
$calculator = new MathExecutor();
$calculator->addFunction('round', static fn ($arg) => \round($arg));
$calculator->addFunction('round', static fn($arg) => \round($arg));
$this->assertEquals(\round(100 / 30), $calculator->execute('round(100/30)'));
}
public function testFunctionUnlimitedParameters() : void
{
$calculator = new MathExecutor();
$calculator->addFunction('give_me_an_array', static fn () => [5, 3, 7, 9, 8]);
$calculator->addFunction('give_me_an_array', static fn() => [5, 3, 7, 9, 8]);
$this->assertEquals(6.4, $calculator->execute('avg(give_me_an_array())'));
$this->assertEquals(10, $calculator->execute('avg(12,8,15,5)'));
$this->assertEquals(3, $calculator->execute('min(give_me_an_array())'));
$this->assertEquals(1, $calculator->execute('min(1,2,3)'));
$this->assertEquals(9, $calculator->execute('max(give_me_an_array())'));
$this->assertEquals(3, $calculator->execute('max(1,2,3)'));
$this->assertEquals(7, $calculator->execute('median(give_me_an_array())'));
$this->assertEquals(4, $calculator->execute('median(1,3,5,7)'));
$calculator->setVar('monthly_salaries', [100, 200, 300]);
$this->assertEquals([100, 200, 300], $calculator->execute('$monthly_salaries'));
$this->assertEquals(200, $calculator->execute('avg($monthly_salaries)'));
$this->assertEquals(\min([100, 200, 300]), $calculator->execute('min($monthly_salaries)'));
$this->assertEquals(\max([100, 200, 300]), $calculator->execute('max($monthly_salaries)'));
$this->assertEquals(200, $calculator->execute('median($monthly_salaries)'));
}
public function testFunctionOptionalParameters() : void
{
$calculator = new MathExecutor();
$calculator->addFunction('round', static fn ($num, $precision = 0) => \round($num, $precision));
$calculator->addFunction('round', static fn($num, $precision = 0) => \round($num, $precision));
$this->assertEquals(\round(11.176), $calculator->execute('round(11.176)'));
$this->assertEquals(\round(11.176, 2), $calculator->execute('round(11.176,2)'));
}
@ -666,7 +673,7 @@ class MathTest extends TestCase
{
$calculator = new MathExecutor();
$this->expectException(IncorrectNumberOfFunctionParametersException::class);
$calculator->addFunction('myfunc', static fn ($arg1, $arg2) => $arg1 + $arg2);
$calculator->addFunction('myfunc', static fn($arg1, $arg2) => $arg1 + $arg2);
$calculator->execute('myfunc(1)');
}
@ -674,7 +681,7 @@ class MathTest extends TestCase
{
$calculator = new MathExecutor();
$this->expectException(IncorrectNumberOfFunctionParametersException::class);
$calculator->addFunction('myfunc', static fn ($arg1, $arg2) => $arg1 + $arg2);
$calculator->addFunction('myfunc', static fn($arg1, $arg2) => $arg1 + $arg2);
$calculator->execute('myfunc(1,2,3)');
}
@ -826,7 +833,7 @@ class MathTest extends TestCase
$calculator = new MathExecutor();
$calculator->addFunction(
'round',
static fn ($value, $decimals) => \round($value, $decimals)
static fn($value, $decimals) => \round($value, $decimals)
);
$expression = 'round(100 * 1.111111, 2)';
$phpResult = 0;
@ -840,7 +847,7 @@ class MathTest extends TestCase
public function testFunctionsWithQuotes() : void
{
$calculator = new MathExecutor();
$calculator->addFunction('concat', static fn ($first, $second) => $first . $second);
$calculator->addFunction('concat', static fn($first, $second) => $first . $second);
$this->assertEquals('testing', $calculator->execute('concat("test", "ing")'));
$this->assertEquals('testing', $calculator->execute("concat('test', 'ing')"));
}