4
4
5
5
namespace MongoDB \Laravel \Eloquent ;
6
6
7
+ use BackedEnum ;
7
8
use Carbon \CarbonInterface ;
8
9
use DateTimeInterface ;
9
10
use Illuminate \Contracts \Queue \QueueableCollection ;
22
23
use MongoDB \BSON \UTCDateTime ;
23
24
use MongoDB \Laravel \Query \Builder as QueryBuilder ;
24
25
use Stringable ;
26
+ use ValueError ;
25
27
26
28
use function array_key_exists ;
27
29
use function array_keys ;
38
40
use function is_string ;
39
41
use function ltrim ;
40
42
use function method_exists ;
43
+ use function sprintf ;
41
44
use function str_contains ;
42
45
use function str_starts_with ;
43
46
use function strcmp ;
44
47
use function uniqid ;
48
+ use function var_export ;
45
49
46
50
abstract class Model extends BaseModel
47
51
{
@@ -704,7 +708,7 @@ protected function addCastAttributesToArray(array $attributes, array $mutatedAtt
704
708
}
705
709
706
710
if ($ this ->isEnumCastable ($ key ) && (! $ castValue instanceof Arrayable)) {
707
- $ castValue = $ castValue !== null ? $ this ->getStorableEnumValue ( $ castValue ) : null ;
711
+ $ castValue = $ castValue !== null ? $ this ->getStorableEnumValueFromLaravel11 ( $ this -> getCasts ()[ $ key ], $ castValue ) : null ;
708
712
}
709
713
710
714
if ($ castValue instanceof Arrayable) {
@@ -717,6 +721,23 @@ protected function addCastAttributesToArray(array $attributes, array $mutatedAtt
717
721
return $ attributes ;
718
722
}
719
723
724
+ /**
725
+ * Duplicate of {@see HasAttributes::getStorableEnumValue()} for Laravel 11 as the signature of the method has
726
+ * changed in a non-backward compatible way.
727
+ *
728
+ * @todo Remove this method when support for Laravel 10 is dropped.
729
+ */
730
+ private function getStorableEnumValueFromLaravel11 ($ expectedEnum , $ value )
731
+ {
732
+ if (! $ value instanceof $ expectedEnum ) {
733
+ throw new ValueError (sprintf ('Value [%s] is not of the expected enum type [%s]. ' , var_export ($ value , true ), $ expectedEnum ));
734
+ }
735
+
736
+ return $ value instanceof BackedEnum
737
+ ? $ value ->value
738
+ : $ value ->name ;
739
+ }
740
+
720
741
/**
721
742
* Is a value a BSON type?
722
743
*
0 commit comments