5
5
use Illuminate \Contracts \Database \Eloquent \CastsAttributes ;
6
6
use Illuminate \Database \Eloquent \Model ;
7
7
8
+ /**
9
+ * @implements CastsAttributes<Decimal, Decimal>
10
+ */
8
11
class Decimal2 implements CastsAttributes
9
12
{
10
13
/**
11
14
* Cast the given value.
12
15
*
13
16
* @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
17
19
*/
18
20
public function get ($ model , string $ key , $ value , array $ attributes ): Decimal
19
21
{
@@ -24,14 +26,16 @@ public function get($model, string $key, $value, array $attributes): Decimal
24
26
* Prepare the given value for storage.
25
27
*
26
28
* @param Model $model
27
- * @param string $key
28
- * @param Decimal $value
29
- * @param array $attributes
29
+ * @param array<mixed> $attributes
30
30
*
31
- * @return numeric- string
31
+ * @return ? string
32
32
*/
33
- public function set ($ model , string $ key , $ value , array $ attributes ): string
33
+ public function set ($ model , string $ key , $ value , array $ attributes ): ? string
34
34
{
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 );
36
40
}
37
41
}
0 commit comments