Skip to content

Commit

Permalink
Removes dependency on bcmath (#45729)
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald authored Jan 20, 2023
1 parent 666c706 commit 3bf0be1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@
}
},
"suggest": {
"ext-bcmath": "Required to use the multiple_of validation rule.",
"ext-ftp": "Required to use the Flysystem FTP driver.",
"ext-gd": "Required to use Illuminate\\Http\\Testing\\FileFactory::image().",
"ext-memcached": "Required to use the memcache cache driver.",
Expand Down
26 changes: 22 additions & 4 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Validation\Concerns;

use Brick\Math\BigDecimal;
use Brick\Math\Exception\MathException as BrickMathException;
use DateTime;
use DateTimeInterface;
use Egulias\EmailValidator\EmailValidator;
Expand All @@ -13,6 +15,7 @@
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Exceptions\MathException;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
use Illuminate\Validation\Rules\Exists;
Expand Down Expand Up @@ -1605,11 +1608,26 @@ public function validateMultipleOf($attribute, $value, $parameters)
return false;
}

if ((float) $parameters[0] === 0.0) {
return false;
}
try {
$numerator = BigDecimal::of($value);
$denominator = BigDecimal::of($parameters[0]);

return bcmod($value, $parameters[0], 16) === '0.0000000000000000';
if ($numerator->isZero() && $denominator->isZero()) {
return false;
}

if ($numerator->isZero()) {
return true;
}

if ($denominator->isZero()) {
return false;
}

return $numerator->remainder($denominator)->isZero();
} catch (BrickMathException $e) {
throw new MathException('An error occurred while handling the mulitple_of input values.', previous: $e);
}
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Illuminate/Validation/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
}
},
"suggest": {
"ext-bcmath": "Required to use the multiple_of validation rule.",
"illuminate/database": "Required to use the database presence verifier (^9.0)."
},
"config": {
Expand Down

0 comments on commit 3bf0be1

Please sign in to comment.