Skip to content

Commit

Permalink
Add isGreaterThanOrEqual, isLessThanOrEqual, eq, gte and lte (
Browse files Browse the repository at this point in the history
#15)

* Add `isGreaterThanOrEqual`, `isLessThanOrEqual`, `eq`, `gte` and `lte`

* Improve README.md with changes
  • Loading branch information
bobmulder authored Oct 22, 2022
1 parent 6c64fa5 commit 9f9cb2e
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 13 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ To compare two numbers with each other, these helpers are available, which will
``` php
$number = new Number('200');

// check if the number is equal to x
$number->isEqual('200');
$number->eq('200');

// check if the number is positive
$number->isPositive();

Expand All @@ -156,12 +160,19 @@ $number->isNegative();

// check if the number is greater than x
$number->isGreaterThan('100');
$number->gt('100');

// check if the number is greater than or equal to x
$number->isGreaterThanOrEqual('100');
$number->gte('100');

// check if the number is less than x
$number->isLessThan('300');
$number->lt('300');

// check if the number is equal to x
$number->isEqual('200');
// check if the number is less than or equal to x
$number->isLessThanOrEqual('300');
$number->lte('300');

// check if the number is zero ("0")
$number->isZero();
Expand Down
80 changes: 75 additions & 5 deletions src/AbstractNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,29 @@ public function isThirteen(): bool
return $this->isEqual('13');
}

/**
* Returns boolean if the current value is equal to the given value.
*
* @param AbstractNumber|string|float|int $value
*/
public function isEqual($value, int $scale = null): bool
{
$number = $this->getNumberFromInput($value);
$scale = $scale ?? self::INTERNAL_SCALE;

return bccomp($this->value, $number->get(), $scale) === 0;
}

/**
* Alias for isEqual method.
*
* @param AbstractNumber|string|float|int $value
*/
public function eq($value, int $scale = null): bool
{
return $this->isEqual($value, $scale);
}

/**
* Returns boolean if the current value is greater than the given value.
*
Expand All @@ -391,6 +414,36 @@ public function isGreaterThan($value, int $scale = null): bool
return bccomp($this->value, $number->get(), $scale) === 1;
}

/**
* Alias for isGreaterThan method.
*
* @param AbstractNumber|string|float|int $value
*/
public function gt($value, int $scale = null): bool
{
return $this->isGreaterThan($value, $scale);
}

/**
* Returns boolean if the current value is greater than or equal to the given value.
*
* @param AbstractNumber|string|float|int $value
*/
public function isGreaterThanOrEqual($value, int $scale = null): bool
{
return $this->isGreaterThan($value, $scale) || $this->isEqual($value, $scale);
}

/**
* Alias for isGreaterThanOrEqual method.
*
* @param AbstractNumber|string|float|int $value
*/
public function gte($value, int $scale = null): bool
{
return $this->isGreaterThanOrEqual($value, $scale);
}

/**
* Returns boolean if the current value is less than the given value.
*
Expand All @@ -405,16 +458,33 @@ public function isLessThan($value, int $scale = null): bool
}

/**
* Returns boolean if the current value is equal to the given value.
* Alias for isLessThan method.
*
* @param AbstractNumber|string|float|int $value
*/
public function isEqual($value, int $scale = null): bool
public function lt($value, int $scale = null): bool
{
$number = $this->getNumberFromInput($value);
$scale = $scale ?? self::INTERNAL_SCALE;
return $this->isLessThan($value, $scale);
}

return bccomp($this->value, $number->get(), $scale) === 0;
/**
* Returns boolean if the current value is less than or equal to the given value.
*
* @param AbstractNumber|string|float|int $value
*/
public function isLessThanOrEqual($value, int $scale = null): bool
{
return $this->isLessThan($value, $scale) || $this->isEqual($value, $scale);
}

/**
* Alias for isGreaterThanOrEqual method.
*
* @param AbstractNumber|string|float|int $value
*/
public function lte($value, int $scale = null): bool
{
return $this->isLessThanOrEqual($value, $scale);
}

/**
Expand Down
43 changes: 37 additions & 6 deletions tests/NumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,22 @@ public function testIsZero(): void
$this->assertFalse((new Number('0.000000085'))->isZero());
}

public function testIsEqual(): void
{
$five = new Number(5);

$this->assertTrue($five->isEqual('5'));
$this->assertTrue($five->isEqual('5.0000'));

$this->assertFalse($five->isEqual('-5'));
$this->assertFalse($five->isEqual('4.9999'));
$this->assertFalse($five->isEqual('5.0001'));
}

public function testIsGreaterThan(): void
{
$five = new Number(5);

$this->assertTrue($five->isGreaterThan('3'));
$this->assertFalse($five->isGreaterThan('7'));

Expand All @@ -559,9 +572,24 @@ public function testIsGreaterThan(): void
$this->assertFalse($five->isGreaterThan('5.0001'));
}

public function testIsGreaterThanOrEqual(): void
{
$five = new Number(5);

$this->assertTrue($five->isGreaterThanOrEqual('5'));

$this->assertTrue($five->isGreaterThanOrEqual('3'));
$this->assertFalse($five->isGreaterThanOrEqual('7'));

$this->assertTrue($five->isGreaterThanOrEqual('-500'));
$this->assertTrue($five->isGreaterThanOrEqual('4.9999'));
$this->assertFalse($five->isGreaterThanOrEqual('5.0001'));
}

public function testIsLessThan(): void
{
$five = new Number(5);

$this->assertFalse($five->isLessThan('3'));
$this->assertTrue($five->isLessThan('7'));

Expand All @@ -570,15 +598,18 @@ public function testIsLessThan(): void
$this->assertTrue($five->isLessThan('5.0001'));
}

public function testIsEqual(): void
public function testIsLessThanOrEqual(): void
{
$five = new Number(5);
$this->assertTrue($five->isEqual('5'));
$this->assertTrue($five->isEqual('5.0000'));

$this->assertFalse($five->isEqual('-5'));
$this->assertFalse($five->isEqual('4.9999'));
$this->assertFalse($five->isEqual('5.0001'));
$this->assertTrue($five->isLessThanOrEqual('5'));

$this->assertFalse($five->isLessThanOrEqual('3'));
$this->assertTrue($five->isLessThanOrEqual('7'));

$this->assertFalse($five->isLessThanOrEqual('-500'));
$this->assertFalse($five->isLessThanOrEqual('4.9999'));
$this->assertTrue($five->isLessThanOrEqual('5.0001'));
}

public function testRound(): void
Expand Down

0 comments on commit 9f9cb2e

Please sign in to comment.