Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fab2s committed Apr 10, 2018
1 parent 044b15b commit 356932b
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 103 deletions.
99 changes: 63 additions & 36 deletions src/Math.php
Expand Up @@ -16,28 +16,71 @@
*/
class Math extends MathOpsAbstract
{
/**
* Math constructor.
*
* @param string|static $number
*
* @throws \InvalidArgumentException
*/
public function __construct($number)
{
if (isset(static::$globalPrecision)) {
$this->precision = static::$globalPrecision;
}

$this->number = static::validateInputNumber($number);
}

/**
* @return string
*/
public function __toString()
{
return static::normalizeNumber($this->number);
}

/**
* @param string $number
*
* @throws \InvalidArgumentException
*
* @return bool
* @return static
*/
public function gte($number)
public static function number($number)
{
return (bool) (bccomp($this->number, static::validateInputNumber($number), $this->precision) >= 0);
return new static($number);
}

/**
* convert any based value bellow or equals to 64 to its decimal value
*
* @param string $number
* @param int $base
*
* @throws \InvalidArgumentException
*
* @return bool
* @return static
*/
public function gt($number)
public static function fromBase($number, $base)
{
return (bool) (bccomp($this->number, static::validateInputNumber($number), $this->precision) === 1);
// trim base 64 padding char, only positive
$number = trim($number, ' =-');
if ($number === '' || strpos($number, '.') !== false) {
throw new \InvalidArgumentException('Argument number is not an integer');
}

$baseChar = static::getBaseChar($base);
if (trim($number, $baseChar[0]) === '') {
return new static('0');
}

if (static::$gmpSupport && $base <= 62) {
return new static(static::baseConvert($number, $base, 10));
}

// By now we know we have a correct base and number
return new static(static::bcDec2Base($number, $base, $baseChar));
}

/**
Expand All @@ -47,9 +90,9 @@ public function gt($number)
*
* @return bool
*/
public function lte($number)
public function gte($number)
{
return (bool) (bccomp($this->number, static::validateInputNumber($number), $this->precision) <= 0);
return (bool) (bccomp($this->number, static::validateInputNumber($number), $this->precision) >= 0);
}

/**
Expand All @@ -59,9 +102,9 @@ public function lte($number)
*
* @return bool
*/
public function lt($number)
public function gt($number)
{
return (bool) (bccomp($this->number, static::validateInputNumber($number), $this->precision) === -1);
return (bool) (bccomp($this->number, static::validateInputNumber($number), $this->precision) === 1);
}

/**
Expand All @@ -71,49 +114,33 @@ public function lt($number)
*
* @return bool
*/
public function eq($number)
public function lte($number)
{
return (bool) (bccomp($this->number, static::validateInputNumber($number), $this->precision) === 0);
return (bool) (bccomp($this->number, static::validateInputNumber($number), $this->precision) <= 0);
}

/**
* returns the highest number among all arguments
*
* @param string[] $numbers
* @param string $number
*
* @throws \InvalidArgumentException
*
* @return $this
* @return bool
*/
public function max(...$numbers)
public function lt($number)
{
foreach ($numbers as $number) {
if (bccomp(static::validateInputNumber($number), $this->number, $this->precision) === 1) {
$this->number = $number;
}
}

return $this;
return (bool) (bccomp($this->number, static::validateInputNumber($number), $this->precision) === -1);
}

/**
* returns the smallest number among all arguments
*
* @param string[] $numbers
* @param string $number
*
* @throws \InvalidArgumentException
*
* @return $this
* @return bool
*/
public function min(...$numbers)
public function eq($number)
{
foreach ($numbers as $number) {
if (bccomp(static::validateInputNumber($number), $this->number, $this->precision) === -1) {
$this->number = $number;
}
}

return $this;
return (bool) (bccomp($this->number, static::validateInputNumber($number), $this->precision) === 0);
}

/**
Expand Down
67 changes: 0 additions & 67 deletions src/Math/MathBaseAbstract.php
Expand Up @@ -76,73 +76,6 @@ abstract class MathBaseAbstract
*/
protected $precision = self::PRECISION;

/**
* Math constructor.
*
* @param string|static $number
*
* @throws \InvalidArgumentException
*/
public function __construct($number)
{
if (isset(static::$globalPrecision)) {
$this->precision = static::$globalPrecision;
}

$this->number = static::validateInputNumber($number);
}

/**
* @return string
*/
public function __toString()
{
return static::normalizeNumber($this->number);
}

/**
* @param string $number
*
* @throws \InvalidArgumentException
*
* @return static
*/
public static function number($number)
{
return new static($number);
}

/**
* convert any based value bellow or equals to 64 to its decimal value
*
* @param string $number
* @param int $base
*
* @throws \InvalidArgumentException
*
* @return static
*/
public static function fromBase($number, $base)
{
// trim base 64 padding char, only positive
$number = trim($number, ' =-');
if ($number === '' || strpos($number, '.') !== false) {
throw new \InvalidArgumentException('Argument number is not an integer');
}

$baseChar = static::getBaseChar($base);
if (trim($number, $baseChar[0]) === '') {
return new static('0');
}

if (static::$gmpSupport && $base <= 62) {
return new static(static::baseConvert($number, $base, 10));
}

// By now we know we have a correct base and number
return new static(static::bcDec2Base($number, $base, $baseChar));
}

/**
* @return string
*/
Expand Down
40 changes: 40 additions & 0 deletions src/Math/MathOpsAbstract.php
Expand Up @@ -197,4 +197,44 @@ public function abs()

return $this;
}

/**
* returns the highest number among all arguments
*
* @param string[] $numbers
*
* @throws \InvalidArgumentException
*
* @return $this
*/
public function max(...$numbers)
{
foreach ($numbers as $number) {
if (bccomp(static::validateInputNumber($number), $this->number, $this->precision) === 1) {
$this->number = $number;
}
}

return $this;
}

/**
* returns the smallest number among all arguments
*
* @param string[] $numbers
*
* @throws \InvalidArgumentException
*
* @return $this
*/
public function min(...$numbers)
{
foreach ($numbers as $number) {
if (bccomp(static::validateInputNumber($number), $this->number, $this->precision) === -1) {
$this->number = $number;
}
}

return $this;
}
}

0 comments on commit 356932b

Please sign in to comment.