|
2 | 2 |
|
3 | 3 | namespace Illuminate\Database\Eloquent\Concerns;
|
4 | 4 |
|
| 5 | +use BackedEnum; |
5 | 6 | use Carbon\CarbonImmutable;
|
6 | 7 | use Carbon\CarbonInterface;
|
7 | 8 | use DateTimeImmutable;
|
@@ -307,7 +308,7 @@ protected function addCastAttributesToArray(array $attributes, array $mutatedAtt
|
307 | 308 | }
|
308 | 309 |
|
309 | 310 | if ($this->isEnumCastable($key) && (! ($attributes[$key] ?? null) instanceof Arrayable)) {
|
310 |
| - $attributes[$key] = isset($attributes[$key]) ? $attributes[$key]->value : null; |
| 311 | + $attributes[$key] = isset($attributes[$key]) ? $this->getStorableEnumValue($attributes[$key]) : null; |
311 | 312 | }
|
312 | 313 |
|
313 | 314 | if ($attributes[$key] instanceof Arrayable) {
|
@@ -814,11 +815,7 @@ protected function getEnumCastableAttributeValue($key, $value)
|
814 | 815 | return $value;
|
815 | 816 | }
|
816 | 817 |
|
817 |
| - if (is_subclass_of($castType, \BackedEnum::class)) { |
818 |
| - return $castType::from($value); |
819 |
| - } |
820 |
| - |
821 |
| - return constant($castType.'::'.$value); |
| 818 | + return $this->getEnumCaseFromValue($castType, $value); |
822 | 819 | }
|
823 | 820 |
|
824 | 821 | /**
|
@@ -1123,21 +1120,42 @@ protected function setEnumCastableAttribute($key, $value)
|
1123 | 1120 |
|
1124 | 1121 | if (! isset($value)) {
|
1125 | 1122 | $this->attributes[$key] = null;
|
1126 |
| - } elseif (is_subclass_of($enumClass, \BackedEnum::class)) { |
1127 |
| - if ($value instanceof $enumClass) { |
1128 |
| - $this->attributes[$key] = $value->value; |
1129 |
| - } else { |
1130 |
| - $this->attributes[$key] = $enumClass::from($value)->value; |
1131 |
| - } |
| 1123 | + } elseif (is_object($value)) { |
| 1124 | + $this->attributes[$key] = $this->getStorableEnumValue($value); |
1132 | 1125 | } else {
|
1133 |
| - if ($value instanceof $enumClass) { |
1134 |
| - $this->attributes[$key] = $value->name; |
1135 |
| - } else { |
1136 |
| - $this->attributes[$key] = constant($enumClass.'::'.$value)->name; |
1137 |
| - } |
| 1126 | + $this->attributes[$key] = $this->getStorableEnumValue( |
| 1127 | + $this->getEnumCaseFromValue($enumClass, $value) |
| 1128 | + ); |
1138 | 1129 | }
|
1139 | 1130 | }
|
1140 | 1131 |
|
| 1132 | + /** |
| 1133 | + * Get an enum case instance from a given class and value. |
| 1134 | + * |
| 1135 | + * @param string $enumClass |
| 1136 | + * @param string|int $value |
| 1137 | + * @return \UnitEnum|\BackedEnum |
| 1138 | + */ |
| 1139 | + protected function getEnumCaseFromValue($enumClass, $value) |
| 1140 | + { |
| 1141 | + return is_subclass_of($enumClass, BackedEnum::class) |
| 1142 | + ? $enumClass::from($value) |
| 1143 | + : constant($enumClass.'::'.$value); |
| 1144 | + } |
| 1145 | + |
| 1146 | + /** |
| 1147 | + * Get the storable value from the given enum. |
| 1148 | + * |
| 1149 | + * @param \UnitEnum|\BackedEnum $value |
| 1150 | + * @return string|int |
| 1151 | + */ |
| 1152 | + protected function getStorableEnumValue($value) |
| 1153 | + { |
| 1154 | + return $value instanceof BackedEnum |
| 1155 | + ? $value->value |
| 1156 | + : $value->name; |
| 1157 | + } |
| 1158 | + |
1141 | 1159 | /**
|
1142 | 1160 | * Get an array attribute with the given key and value set.
|
1143 | 1161 | *
|
|
0 commit comments