forked from jorgecasas/php-ml
-
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.
* Replace eval with strategy * Use Factory Pattern, add tests * Add missing dockblocks * Replace strategy with simple object
- Loading branch information
1 parent
dda9e16
commit 11d05ce
Showing
6 changed files
with
144 additions
and
32 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
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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phpml\Math; | ||
|
||
use Phpml\Exception\InvalidArgumentException; | ||
|
||
class Comparison | ||
{ | ||
/** | ||
* @param mixed $a | ||
* @param mixed $b | ||
* @param string $operator | ||
* | ||
* @return bool | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public static function compare($a, $b, string $operator): bool | ||
{ | ||
switch ($operator) { | ||
case '>': | ||
return $a > $b; | ||
case '>=': | ||
return $a >= $b; | ||
case '=': | ||
case '==': | ||
return $a == $b; | ||
case '===': | ||
return $a === $b; | ||
case '<=': | ||
return $a <= $b; | ||
case '<': | ||
return $a < $b; | ||
case '!=': | ||
case '<>': | ||
return $a != $b; | ||
case '!==': | ||
return $a !== $b; | ||
default: | ||
throw InvalidArgumentException::invalidOperator($operator); | ||
} | ||
} | ||
} |
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,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace tests\Phpml\Math; | ||
|
||
use Phpml\Math\Comparison; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ComparisonTest extends TestCase | ||
{ | ||
/** | ||
* @param mixed $a | ||
* @param mixed $b | ||
* @param string $operator | ||
* @param bool $expected | ||
* | ||
* @dataProvider provideData | ||
*/ | ||
public function testResult($a, $b, string $operator, bool $expected) | ||
{ | ||
$result = Comparison::compare($a, $b, $operator); | ||
|
||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
/** | ||
* @expectedException \Phpml\Exception\InvalidArgumentException | ||
* @expectedExceptionMessage Invalid operator "~=" provided | ||
*/ | ||
public function testThrowExceptionWhenOperatorIsInvalid() | ||
{ | ||
Comparison::compare(1, 1, '~='); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function provideData() | ||
{ | ||
return [ | ||
// Greater | ||
[1, 0, '>', true], | ||
[1, 1, '>', false], | ||
[0, 1, '>', false], | ||
// Greater or equal | ||
[1, 0, '>=', true], | ||
[1, 1, '>=', true], | ||
[0, 1, '>=', false], | ||
// Equal | ||
[1, 0, '=', false], | ||
[1, 1, '==', true], | ||
[1, '1', '=', true], | ||
[1, '0', '==', false], | ||
// Identical | ||
[1, 0, '===', false], | ||
[1, 1, '===', true], | ||
[1, '1', '===', false], | ||
['a', 'a', '===', true], | ||
// Not equal | ||
[1, 0, '!=', true], | ||
[1, 1, '<>', false], | ||
[1, '1', '!=', false], | ||
[1, '0', '<>', true], | ||
// Not identical | ||
[1, 0, '!==', true], | ||
[1, 1, '!==', false], | ||
[1, '1', '!==', true], | ||
[1, '0', '!==', true], | ||
// Less or equal | ||
[1, 0, '<=', false], | ||
[1, 1, '<=', true], | ||
[0, 1, '<=', true], | ||
// Less | ||
[1, 0, '<', false], | ||
[1, 1, '<', false], | ||
[0, 1, '<', true], | ||
]; | ||
} | ||
} |