|
16 | 16 | trait DecimalObjectCast |
17 | 17 | { |
18 | 18 | /** |
19 | | - * Get all of the current attributes on the model for an insert operation. |
| 19 | + * Cast an attribute to a native PHP type. |
20 | 20 | * |
21 | | - * @return array<mixed> |
22 | | - */ |
23 | | - protected function getAttributesForInsert() |
24 | | - { |
25 | | - $attributes = parent::getAttributes(); |
26 | | - |
27 | | - foreach ($attributes as $key => $value) { |
28 | | - if ($value instanceof Decimal) { |
29 | | - $attributes[$key] = $this->toString($key, $value); |
30 | | - } |
31 | | - } |
32 | | - |
33 | | - return $attributes; |
34 | | - } |
35 | | - |
36 | | - /** |
37 | | - * Determine if the new and old values for a given key are equivalent. |
| 21 | + * @see \Illuminate\Database\Eloquent\Concerns\HasAttributes::castAttribute |
38 | 22 | * |
39 | 23 | * @param string $key |
| 24 | + * @param mixed $value Raw value |
40 | 25 | * |
41 | | - * @return bool |
| 26 | + * @return mixed Transformed value |
42 | 27 | */ |
43 | | - public function originalIsEquivalent($key) |
44 | | - { |
45 | | - $castTypeIsDecimal = false; |
46 | | - $casts = $this->getCasts(); |
47 | | - if (array_key_exists($key, $casts)) { |
48 | | - $castTypeIsDecimal = $this->isDecimalCast($casts[$key]); |
49 | | - } |
50 | | - |
51 | | - $attribute = $this->attributes[$key] ?? null; |
52 | | - $original = $this->original[$key] ?? null; |
53 | | - |
54 | | - if (!$castTypeIsDecimal && !$attribute instanceof Decimal && !$original instanceof Decimal) { |
55 | | - return parent::originalIsEquivalent($key); |
56 | | - } |
57 | | - |
58 | | - if ($attribute instanceof Decimal) { |
59 | | - $attribute = $this->toString($key, $attribute); |
60 | | - } |
61 | | - if ($original instanceof Decimal) { |
62 | | - $original = $this->toString($key, $original); |
63 | | - } |
64 | | - |
65 | | - return $attribute === $original; |
66 | | - } |
67 | | - |
68 | | - private function toString(string $key, Decimal $value): string |
| 28 | + protected function castAttribute($key, $value) |
69 | 29 | { |
70 | | - $casts = $this->getCasts(); |
71 | | - if (!array_key_exists($key, $casts)) { |
72 | | - return (string)$value; |
73 | | - } |
| 30 | + if ($value !== null) { |
| 31 | + $casts = $this->getCasts(); |
| 32 | + if (array_key_exists($key, $casts)) { |
| 33 | + $castType = $casts[$key]; |
| 34 | + if ($this->isDecimalCast($castType)) { |
| 35 | + $precision = explode(':', $castType)[2] ?? Decimal::DEFAULT_PRECISION; |
74 | 36 |
|
75 | | - $castType = $casts[$key]; |
76 | | - if (!$this->isDecimalCast($castType)) { |
77 | | - return $value; |
| 37 | + return new Decimal($value, $precision); |
| 38 | + } |
| 39 | + } |
78 | 40 | } |
79 | 41 |
|
80 | | - $decimals = explode(':', $castType)[1]; |
81 | | - |
82 | | - return $value->toFixed($decimals, false, PHP_ROUND_HALF_UP); |
| 42 | + return parent::castAttribute($key, $value); |
83 | 43 | } |
84 | 44 |
|
85 | 45 | /** |
86 | | - * Cast an attribute to a native PHP type. |
| 46 | + * Set a given attribute on the model. |
87 | 47 | * |
88 | | - * @see \Illuminate\Database\Eloquent\Concerns\HasAttributes::castAttribute |
| 48 | + * @see \Illuminate\Database\Eloquent\Concerns\HasAttributes::setAttribute |
89 | 49 | * |
90 | 50 | * @param string $key |
91 | 51 | * @param mixed $value Raw value |
92 | 52 | * |
93 | | - * @return mixed Transformed value |
| 53 | + * @return mixed The model |
94 | 54 | */ |
95 | | - protected function castAttribute($key, $value) |
| 55 | + public function setAttribute($key, $value) |
96 | 56 | { |
97 | 57 | if ($value !== null) { |
98 | 58 | $casts = $this->getCasts(); |
99 | 59 | if (array_key_exists($key, $casts)) { |
100 | 60 | $castType = $casts[$key]; |
101 | 61 | if ($this->isDecimalCast($castType)) { |
102 | | - $precision = explode(':', $castType)[2] ?? Decimal::DEFAULT_PRECISION; |
| 62 | + if (!$value instanceof Decimal) { |
| 63 | + $value = $this->castAttribute($key, $value); |
| 64 | + assert($value instanceof Decimal); |
| 65 | + } |
103 | 66 |
|
104 | | - return new Decimal($value, $precision); |
| 67 | + $decimals = explode(':', $castType)[1]; |
| 68 | + $this->attributes[$key] = $value->toFixed((int)$decimals, false, PHP_ROUND_HALF_UP); |
| 69 | + |
| 70 | + return $this; |
105 | 71 | } |
106 | 72 | } |
107 | 73 | } |
108 | 74 |
|
109 | | - return parent::castAttribute($key, $value); |
| 75 | + return parent::setAttribute($key, $value); |
110 | 76 | } |
111 | 77 | } |
0 commit comments