-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from o-p/op/arithmetic
Add math operators, rename existing comparison operators.
- Loading branch information
Showing
17 changed files
with
468 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
namespace JsonLogic\Operators; | ||
|
||
use Closure; | ||
use JsonLogic\Parameters; | ||
|
||
/** | ||
* @operator / | ||
*/ | ||
class Divide extends Operator | ||
{ | ||
protected $alias = '/'; | ||
|
||
public function params($param): Closure | ||
{ | ||
$this->autoValidateParams($param); | ||
$prepared = Parameters::from($param); | ||
|
||
return function (&$data) use (&$prepared) { | ||
return $prepared->reduce(function ($a, $b) { | ||
return $a / $b; | ||
}, $data); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
namespace JsonLogic\Operators; | ||
|
||
use Closure; | ||
use JsonLogic\Parameters; | ||
|
||
/** | ||
* @operator - | ||
*/ | ||
class Minus extends Operator | ||
{ | ||
protected $alias = '-'; | ||
|
||
public function params($param): Closure | ||
{ | ||
$this->autoValidateParams($param); | ||
$prepared = Parameters::from($param); | ||
|
||
return function (&$data) use (&$prepared) { | ||
return $prepared->reduce(function ($a, $b) { | ||
return $a - $b; | ||
}, $data); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
namespace JsonLogic\Operators; | ||
|
||
use Closure; | ||
use JsonLogic\Parameters; | ||
|
||
/** | ||
* @operator % | ||
*/ | ||
class Modulo extends Operator | ||
{ | ||
protected $alias = '%'; | ||
|
||
public function params($param): Closure | ||
{ | ||
$this->autoValidateParams($param); | ||
$prepared = Parameters::from($param); | ||
|
||
return function (&$data) use (&$prepared) { | ||
return $prepared->reduce(function ($a, $b) { | ||
return $a % $b; | ||
}, $data); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
namespace JsonLogic\Operators; | ||
|
||
use Closure; | ||
use JsonLogic\Parameters; | ||
|
||
/** | ||
* @operator * | ||
*/ | ||
class Multiply extends Operator | ||
{ | ||
protected $alias = '*'; | ||
|
||
public function params($param): Closure | ||
{ | ||
$this->autoValidateParams($param); | ||
$prepared = Parameters::from($param); | ||
|
||
return function (&$data) use (&$prepared) { | ||
return array_product($prepared->values($data)); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
namespace JsonLogic\Operators; | ||
|
||
use Closure; | ||
use JsonLogic\Parameters; | ||
|
||
/** | ||
* @operator + | ||
*/ | ||
class Plus extends Operator | ||
{ | ||
protected $alias = '+'; | ||
|
||
public function params($param): Closure | ||
{ | ||
$this->autoValidateParams($param); | ||
$prepared = Parameters::from($param); | ||
|
||
return function (&$data) use (&$prepared) { | ||
return array_sum($prepared->values($data)); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
namespace Tests\JsonLogic\Operators; | ||
|
||
use JsonLogic\Operators\Divide; | ||
use Tests\TestCase; | ||
|
||
use function number_format; | ||
|
||
/** | ||
* @group operators | ||
*/ | ||
class DivideTest extends TestCase | ||
{ | ||
/** @dataProvider cases */ | ||
public function test(array $params, $data, string $expect): void | ||
{ | ||
$this->assertSame( | ||
$expect, | ||
number_format((new Divide())->params($params)($data), 3) | ||
); | ||
} | ||
|
||
public function cases(): array | ||
{ | ||
return [ | ||
'1 ÷ 1' => [[1, 1], null, '1.000'], | ||
'3.1415926 ÷ 2.7182818' => [[3.1415926, 2.7182818], null, '1.156'], | ||
'5/7 ÷ 25/49' => [[5/7, 25/49], null, '1.400'], | ||
'1/2 ÷ 3/2 ÷ 4/3 ÷ 5/4' => [[1/2, 3/2, 4/3, 5/4], null, '0.200'], | ||
'Pi ÷ e' => [ | ||
[ | ||
['var' => 'real.irrational.Pi'], | ||
['var' => 'real.irrational.e'], | ||
], | ||
[ | ||
'real' => [ | ||
'irrational' => [ | ||
'Pi' => 3.1415926, | ||
'e' => 2.7182818, | ||
], | ||
], | ||
], | ||
'1.156', | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
namespace Tests\JsonLogic\Operators; | ||
|
||
use JsonLogic\Operators\Minus; | ||
use Tests\TestCase; | ||
|
||
use function number_format; | ||
|
||
/** | ||
* @group operators | ||
*/ | ||
class MinusTest extends TestCase | ||
{ | ||
/** @dataProvider cases */ | ||
public function test(array $params, $data, string $expect): void | ||
{ | ||
$this->assertSame( | ||
$expect, | ||
number_format((new Minus())->params($params)($data), 2) | ||
); | ||
} | ||
|
||
public function cases(): array | ||
{ | ||
return [ | ||
'1 - 1' => [[1, 1], null, '0.00'], | ||
'3.1415926 - 2.7182818' => [[3.1415926, 2.7182818], null, '0.42'], | ||
'1/3 - 2/3' => [[1/3, 2/3], null, '-0.33'], | ||
'1/2 - 1/3 - 1/4 - 1/5 - 1/6' => [[1/2, 1/3, 1/4, 1/5, 1/6], null, '-0.45'], | ||
'Pi - e' => [ | ||
[ | ||
['var' => 'real.irrational.Pi'], | ||
['var' => 'real.irrational.e'], | ||
], | ||
[ | ||
'real' => [ | ||
'irrational' => [ | ||
'Pi' => 3.1415926, | ||
'e' => 2.7182818, | ||
], | ||
], | ||
], | ||
'0.42', | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
namespace Tests\JsonLogic\Operators; | ||
|
||
use JsonLogic\Operators\Modulo; | ||
use Tests\TestCase; | ||
|
||
/** | ||
* @group operators | ||
*/ | ||
class ModuloTest extends TestCase | ||
{ | ||
/** @dataProvider cases */ | ||
public function test(array $params, $data, int $expect): void | ||
{ | ||
$this->assertSame( | ||
$expect, | ||
(new Modulo())->params($params)($data) | ||
); | ||
} | ||
|
||
public function cases(): array | ||
{ | ||
return [ | ||
'1 % 1' => [[1, 1], null, 0], | ||
'100 % 3' => [[100, 3], null, 1], | ||
'100 % 23 % 17 % 5' => [[100, 23, 17, 5], null, 3], | ||
'100 % 17' => [ | ||
[ | ||
['var' => 'one hundred'], | ||
['var' => 'seventeen'], | ||
], | ||
[ | ||
'one hundred' => 100, | ||
'seventeen' => 17, | ||
], | ||
15, | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.