Skip to content

Commit 9f9cb2e

Browse files
authored
Add isGreaterThanOrEqual, isLessThanOrEqual, eq, gte and lte (#15)
* Add `isGreaterThanOrEqual`, `isLessThanOrEqual`, `eq`, `gte` and `lte` * Improve README.md with changes
1 parent 6c64fa5 commit 9f9cb2e

File tree

3 files changed

+125
-13
lines changed

3 files changed

+125
-13
lines changed

README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ To compare two numbers with each other, these helpers are available, which will
148148
``` php
149149
$number = new Number('200');
150150

151+
// check if the number is equal to x
152+
$number->isEqual('200');
153+
$number->eq('200');
154+
151155
// check if the number is positive
152156
$number->isPositive();
153157

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

157161
// check if the number is greater than x
158162
$number->isGreaterThan('100');
163+
$number->gt('100');
164+
165+
// check if the number is greater than or equal to x
166+
$number->isGreaterThanOrEqual('100');
167+
$number->gte('100');
159168

160169
// check if the number is less than x
161170
$number->isLessThan('300');
171+
$number->lt('300');
162172

163-
// check if the number is equal to x
164-
$number->isEqual('200');
173+
// check if the number is less than or equal to x
174+
$number->isLessThanOrEqual('300');
175+
$number->lte('300');
165176

166177
// check if the number is zero ("0")
167178
$number->isZero();

src/AbstractNumber.php

+75-5
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,29 @@ public function isThirteen(): bool
378378
return $this->isEqual('13');
379379
}
380380

381+
/**
382+
* Returns boolean if the current value is equal to the given value.
383+
*
384+
* @param AbstractNumber|string|float|int $value
385+
*/
386+
public function isEqual($value, int $scale = null): bool
387+
{
388+
$number = $this->getNumberFromInput($value);
389+
$scale = $scale ?? self::INTERNAL_SCALE;
390+
391+
return bccomp($this->value, $number->get(), $scale) === 0;
392+
}
393+
394+
/**
395+
* Alias for isEqual method.
396+
*
397+
* @param AbstractNumber|string|float|int $value
398+
*/
399+
public function eq($value, int $scale = null): bool
400+
{
401+
return $this->isEqual($value, $scale);
402+
}
403+
381404
/**
382405
* Returns boolean if the current value is greater than the given value.
383406
*
@@ -391,6 +414,36 @@ public function isGreaterThan($value, int $scale = null): bool
391414
return bccomp($this->value, $number->get(), $scale) === 1;
392415
}
393416

417+
/**
418+
* Alias for isGreaterThan method.
419+
*
420+
* @param AbstractNumber|string|float|int $value
421+
*/
422+
public function gt($value, int $scale = null): bool
423+
{
424+
return $this->isGreaterThan($value, $scale);
425+
}
426+
427+
/**
428+
* Returns boolean if the current value is greater than or equal to the given value.
429+
*
430+
* @param AbstractNumber|string|float|int $value
431+
*/
432+
public function isGreaterThanOrEqual($value, int $scale = null): bool
433+
{
434+
return $this->isGreaterThan($value, $scale) || $this->isEqual($value, $scale);
435+
}
436+
437+
/**
438+
* Alias for isGreaterThanOrEqual method.
439+
*
440+
* @param AbstractNumber|string|float|int $value
441+
*/
442+
public function gte($value, int $scale = null): bool
443+
{
444+
return $this->isGreaterThanOrEqual($value, $scale);
445+
}
446+
394447
/**
395448
* Returns boolean if the current value is less than the given value.
396449
*
@@ -405,16 +458,33 @@ public function isLessThan($value, int $scale = null): bool
405458
}
406459

407460
/**
408-
* Returns boolean if the current value is equal to the given value.
461+
* Alias for isLessThan method.
409462
*
410463
* @param AbstractNumber|string|float|int $value
411464
*/
412-
public function isEqual($value, int $scale = null): bool
465+
public function lt($value, int $scale = null): bool
413466
{
414-
$number = $this->getNumberFromInput($value);
415-
$scale = $scale ?? self::INTERNAL_SCALE;
467+
return $this->isLessThan($value, $scale);
468+
}
416469

417-
return bccomp($this->value, $number->get(), $scale) === 0;
470+
/**
471+
* Returns boolean if the current value is less than or equal to the given value.
472+
*
473+
* @param AbstractNumber|string|float|int $value
474+
*/
475+
public function isLessThanOrEqual($value, int $scale = null): bool
476+
{
477+
return $this->isLessThan($value, $scale) || $this->isEqual($value, $scale);
478+
}
479+
480+
/**
481+
* Alias for isGreaterThanOrEqual method.
482+
*
483+
* @param AbstractNumber|string|float|int $value
484+
*/
485+
public function lte($value, int $scale = null): bool
486+
{
487+
return $this->isLessThanOrEqual($value, $scale);
418488
}
419489

420490
/**

tests/NumberTest.php

+37-6
Original file line numberDiff line numberDiff line change
@@ -548,9 +548,22 @@ public function testIsZero(): void
548548
$this->assertFalse((new Number('0.000000085'))->isZero());
549549
}
550550

551+
public function testIsEqual(): void
552+
{
553+
$five = new Number(5);
554+
555+
$this->assertTrue($five->isEqual('5'));
556+
$this->assertTrue($five->isEqual('5.0000'));
557+
558+
$this->assertFalse($five->isEqual('-5'));
559+
$this->assertFalse($five->isEqual('4.9999'));
560+
$this->assertFalse($five->isEqual('5.0001'));
561+
}
562+
551563
public function testIsGreaterThan(): void
552564
{
553565
$five = new Number(5);
566+
554567
$this->assertTrue($five->isGreaterThan('3'));
555568
$this->assertFalse($five->isGreaterThan('7'));
556569

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

575+
public function testIsGreaterThanOrEqual(): void
576+
{
577+
$five = new Number(5);
578+
579+
$this->assertTrue($five->isGreaterThanOrEqual('5'));
580+
581+
$this->assertTrue($five->isGreaterThanOrEqual('3'));
582+
$this->assertFalse($five->isGreaterThanOrEqual('7'));
583+
584+
$this->assertTrue($five->isGreaterThanOrEqual('-500'));
585+
$this->assertTrue($five->isGreaterThanOrEqual('4.9999'));
586+
$this->assertFalse($five->isGreaterThanOrEqual('5.0001'));
587+
}
588+
562589
public function testIsLessThan(): void
563590
{
564591
$five = new Number(5);
592+
565593
$this->assertFalse($five->isLessThan('3'));
566594
$this->assertTrue($five->isLessThan('7'));
567595

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

573-
public function testIsEqual(): void
601+
public function testIsLessThanOrEqual(): void
574602
{
575603
$five = new Number(5);
576-
$this->assertTrue($five->isEqual('5'));
577-
$this->assertTrue($five->isEqual('5.0000'));
578604

579-
$this->assertFalse($five->isEqual('-5'));
580-
$this->assertFalse($five->isEqual('4.9999'));
581-
$this->assertFalse($five->isEqual('5.0001'));
605+
$this->assertTrue($five->isLessThanOrEqual('5'));
606+
607+
$this->assertFalse($five->isLessThanOrEqual('3'));
608+
$this->assertTrue($five->isLessThanOrEqual('7'));
609+
610+
$this->assertFalse($five->isLessThanOrEqual('-500'));
611+
$this->assertFalse($five->isLessThanOrEqual('4.9999'));
612+
$this->assertTrue($five->isLessThanOrEqual('5.0001'));
582613
}
583614

584615
public function testRound(): void

0 commit comments

Comments
 (0)