Skip to content

Commit 3d11dd2

Browse files
committed
Reduce asDecimal override by using parent::asDecimal
1 parent e2d2931 commit 3d11dd2

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

src/Eloquent/Model.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace MongoDB\Laravel\Eloquent;
66

7-
use Brick\Math\BigDecimal;
8-
use Brick\Math\Exception\MathException as BrickMathException;
9-
use Brick\Math\RoundingMode;
107
use Carbon\CarbonInterface;
118
use DateTimeInterface;
129
use Illuminate\Contracts\Queue\QueueableCollection;
@@ -15,7 +12,6 @@
1512
use Illuminate\Database\Eloquent\Model as BaseModel;
1613
use Illuminate\Database\Eloquent\Relations\Relation;
1714
use Illuminate\Support\Arr;
18-
use Illuminate\Support\Exceptions\MathException;
1915
use Illuminate\Support\Facades\Date;
2016
use Illuminate\Support\Str;
2117
use MongoDB\BSON\Binary;
@@ -275,13 +271,12 @@ public function setAttribute($key, $value)
275271
/** @inheritdoc */
276272
protected function asDecimal($value, $decimals)
277273
{
278-
try {
279-
$value = (string) BigDecimal::of((string) $value)->toScale((int) $decimals, RoundingMode::HALF_UP);
280-
281-
return new Decimal128($value);
282-
} catch (BrickMathException $e) {
283-
throw new MathException('Unable to cast value to a decimal.', previous: $e);
274+
if ($value instanceof Decimal128) {
275+
// Convert it to a string to round, want to make it act exactly like we expect.
276+
$value = (string) $value;
284277
}
278+
279+
return parent::asDecimal($value, $decimals);
285280
}
286281

287282
/** @inheritdoc */

0 commit comments

Comments
 (0)