Skip to content

Commit ca6ecbd

Browse files
authored
Update DecimalObjectCast.php
1 parent 069abe5 commit ca6ecbd

File tree

1 file changed

+28
-62
lines changed

1 file changed

+28
-62
lines changed

src/DecimalObjectCast.php

+28-62
Original file line numberDiff line numberDiff line change
@@ -16,96 +16,62 @@
1616
trait DecimalObjectCast
1717
{
1818
/**
19-
* Get all of the current attributes on the model for an insert operation.
19+
* Cast an attribute to a native PHP type.
2020
*
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
3822
*
3923
* @param string $key
24+
* @param mixed $value Raw value
4025
*
41-
* @return bool
26+
* @return mixed Transformed value
4227
*/
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)
6929
{
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;
7436

75-
$castType = $casts[$key];
76-
if (!$this->isDecimalCast($castType)) {
77-
return $value;
37+
return new Decimal($value, $precision);
38+
}
39+
}
7840
}
7941

80-
$decimals = explode(':', $castType)[1];
81-
82-
return $value->toFixed($decimals, false, PHP_ROUND_HALF_UP);
42+
return parent::castAttribute($key, $value);
8343
}
8444

8545
/**
86-
* Cast an attribute to a native PHP type.
46+
* Set a given attribute on the model.
8747
*
88-
* @see \Illuminate\Database\Eloquent\Concerns\HasAttributes::castAttribute
48+
* @see \Illuminate\Database\Eloquent\Concerns\HasAttributes::setAttribute
8949
*
9050
* @param string $key
9151
* @param mixed $value Raw value
9252
*
93-
* @return mixed Transformed value
53+
* @return mixed The model
9454
*/
95-
protected function castAttribute($key, $value)
55+
public function setAttribute($key, $value)
9656
{
9757
if ($value !== null) {
9858
$casts = $this->getCasts();
9959
if (array_key_exists($key, $casts)) {
10060
$castType = $casts[$key];
10161
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+
}
10366

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;
10571
}
10672
}
10773
}
10874

109-
return parent::castAttribute($key, $value);
75+
return parent::setAttribute($key, $value);
11076
}
11177
}

0 commit comments

Comments
 (0)