2017-10-24 16:59:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Phpml\Math;
|
|
|
|
|
|
|
|
use Phpml\Exception\InvalidArgumentException;
|
|
|
|
|
|
|
|
class Comparison
|
|
|
|
{
|
|
|
|
/**
|
2018-10-28 06:44:52 +00:00
|
|
|
* @param mixed $a
|
|
|
|
* @param mixed $b
|
|
|
|
*
|
2017-10-24 16:59:12 +00:00
|
|
|
* @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:
|
2018-03-03 15:03:53 +00:00
|
|
|
throw new InvalidArgumentException(sprintf('Invalid operator "%s" provided', $operator));
|
2017-10-24 16:59:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|