55use Illuminate \Contracts \Database \Eloquent \CastsAttributes ;
66use Illuminate \Database \Eloquent \Model ;
77
8+ /**
9+ * @implements CastsAttributes<Decimal, Decimal>
10+ */
811class Decimal2 implements CastsAttributes
912{
1013 /**
1114 * Cast the given value.
1215 *
1316 * @param Model $model
14- * @param string $key
15- * @param numeric-string|int $value
16- * @param array $attributes
17+ * @param numeric-string $value
18+ * @param array<mixed> $attributes
1719 */
1820 public function get ($ model , string $ key , $ value , array $ attributes ): Decimal
1921 {
@@ -24,14 +26,16 @@ public function get($model, string $key, $value, array $attributes): Decimal
2426 * Prepare the given value for storage.
2527 *
2628 * @param Model $model
27- * @param string $key
28- * @param Decimal $value
29- * @param array $attributes
29+ * @param array<mixed> $attributes
3030 *
31- * @return numeric- string
31+ * @return ? string
3232 */
33- public function set ($ model , string $ key , $ value , array $ attributes ): string
33+ public function set ($ model , string $ key , $ value , array $ attributes ): ? string
3434 {
35- return $ value ->toFixed ($ decimals , $ commas = false , PHP_ROUND_HALF_UP );
35+ if ($ value === null ) {
36+ return null ;
37+ }
38+
39+ return $ value ->toFixed ($ value ->precision (), false , PHP_ROUND_HALF_UP );
3640 }
3741}
0 commit comments