Skip to content

Commit

Permalink
Merge pull request #2 from jstoone/feature/#1-big-int-calculations
Browse files Browse the repository at this point in the history
Change how big integers are calculated
  • Loading branch information
LasseRafn committed Aug 10, 2018
2 parents 20f0088 + 9006595 commit 350fa2f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 36 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
],
"require": {
"php": ">=7.1",
"ext-bcmath": "*",
"illuminate/contracts": "^5.5|^5.6",
"illuminate/support": "^5.5|^5.6",
"illuminate/validation": "^5.5|^5.6"
Expand Down
52 changes: 34 additions & 18 deletions src/BigInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,41 @@

class BigInteger extends BaseMysqlIntegerRule
{
/**
* @return int
*/
protected function min() {
if ( $this->unsigned ) {
return 0;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
*
* @return bool
*/
public function passes($attribute, $value)
{
return bccomp($value, $this->min) >= 0
&& bccomp($value, $this->max) <= 0;
}

return -2 ^ 63;
}
/**
* @return int
*/
protected function min()
{
if ($this->unsigned) {
return 0;
}

/**
* @return int
*/
protected function max() {
if ( $this->unsigned ) {
return 2 ^ 64 - 1;
}
return bcpow(-2, 63);
}

return 2 ^ 63 - 1;
}
/**
* @return int
*/
protected function max()
{
if ($this->unsigned) {
return bcsub(bcpow(2, 64), 1);
}

return bcsub(bcpow(2, 63), 1);
}
}
41 changes: 23 additions & 18 deletions tests/BigIntegerValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@

class BigIntegerValidationTest extends TestCase
{
/** @test */
public function can_validate_signed_big_integers() {
$rule = new \Rackbeat\Rules\BigInteger( false );
/** @test */
public function can_validate_signed_big_integers()
{
$rule = new \Rackbeat\Rules\BigInteger(false);
$min = '-9223372036854775808';
$max = '9223372036854775807';

$this->assertTrue( $rule->passes( 'number', 0 ) );
$this->assertTrue( $rule->passes( 'number', -2 ^ 63 ) );
$this->assertTrue( $rule->passes( 'number', 2 ^ 63 - 1 ) );
$this->assertTrue($rule->passes('number', 0));
$this->assertTrue($rule->passes('number', $min));
$this->assertTrue($rule->passes('number', $max));

$this->assertFalse( $rule->passes( 'number', -5000000000000000000 ) );
$this->assertFalse( $rule->passes( 'number', 50000000000000000000 ) );
}
$this->assertFalse($rule->passes('number', bcsub($min, 1)));
$this->assertFalse($rule->passes('number', bcadd($max, 1)));
}

/** @test */
public function can_validate_unsigned_big_integers() {
$rule = new \Rackbeat\Rules\BigInteger( true );
/** @test */
public function can_validate_unsigned_big_integers()
{
$rule = new \Rackbeat\Rules\BigInteger(true);
$min = 0;
$max = '18446744073709551615';

$this->assertTrue( $rule->passes( 'number', 0 ) );
$this->assertTrue( $rule->passes( 'number', 2 ^ 64 - 1 ) );
$this->assertTrue($rule->passes('number', $min));
$this->assertTrue($rule->passes('number', $max));

$this->assertFalse( $rule->passes( 'number', -2147483648 ) );
$this->assertFalse( $rule->passes( 'number', -50000000000000000000 ) );
$this->assertFalse( $rule->passes( 'number', 50000000000000000000 ) );
}
$this->assertFalse($rule->passes('number', $min - 1));
$this->assertFalse($rule->passes('number', bcadd($max, 1)));
}
}

0 comments on commit 350fa2f

Please sign in to comment.