@@ -148,6 +148,13 @@ trait HasAttributes
148
148
*/
149
149
protected static $ attributeMutatorCache = [];
150
150
151
+ /**
152
+ * The cache of the "Attribute" return type marked mutated, gettable attributes for each class.
153
+ *
154
+ * @var array
155
+ */
156
+ protected static $ getAttributeMutatorCache = [];
157
+
151
158
/**
152
159
* The cache of the "Attribute" return type marked mutated, settable attributes for each class.
153
160
*
@@ -418,7 +425,7 @@ public function getAttribute($key)
418
425
if (array_key_exists ($ key , $ this ->attributes ) ||
419
426
array_key_exists ($ key , $ this ->casts ) ||
420
427
$ this ->hasGetMutator ($ key ) ||
421
- $ this ->hasAttributeGetMutator ($ key ) ||
428
+ $ this ->hasAttributeMutator ($ key ) ||
422
429
$ this ->isClassCastable ($ key )) {
423
430
return $ this ->getAttributeValue ($ key );
424
431
}
@@ -552,12 +559,12 @@ public function hasGetMutator($key)
552
559
}
553
560
554
561
/**
555
- * Determine if a "Attribute" return type marked get mutator exists for an attribute.
562
+ * Determine if a "Attribute" return type marked mutator exists for an attribute.
556
563
*
557
564
* @param string $key
558
565
* @return bool
559
566
*/
560
- public function hasAttributeGetMutator ($ key )
567
+ public function hasAttributeMutator ($ key )
561
568
{
562
569
if (isset (static ::$ attributeMutatorCache [get_class ($ this )][$ key ])) {
563
570
return static ::$ attributeMutatorCache [get_class ($ this )][$ key ];
@@ -574,6 +581,25 @@ public function hasAttributeGetMutator($key)
574
581
$ returnType ->getName () === Attribute::class;
575
582
}
576
583
584
+ /**
585
+ * Determine if a "Attribute" return type marked get mutator exists for an attribute.
586
+ *
587
+ * @param string $key
588
+ * @return bool
589
+ */
590
+ public function hasAttributeGetMutator ($ key )
591
+ {
592
+ if (isset (static ::$ getAttributeMutatorCache [get_class ($ this )][$ key ])) {
593
+ return static ::$ getAttributeMutatorCache [get_class ($ this )][$ key ];
594
+ }
595
+
596
+ if (! $ this ->hasAttributeMutator ($ key )) {
597
+ return static ::$ getAttributeMutatorCache [get_class ($ this )][$ key ] = false ;
598
+ }
599
+
600
+ return static ::$ getAttributeMutatorCache [get_class ($ this )][$ key ] = is_callable ($ this ->{Str::camel ($ key )}()->get );
601
+ }
602
+
577
603
/**
578
604
* Get the value of an attribute using its mutator.
579
605
*
@@ -623,8 +649,8 @@ protected function mutateAttributeForArray($key, $value)
623
649
{
624
650
if ($ this ->isClassCastable ($ key )) {
625
651
$ value = $ this ->getClassCastableAttributeValue ($ key , $ value );
626
- } elseif (isset (static ::$ attributeMutatorCache [get_class ($ this )][$ key ]) &&
627
- static ::$ attributeMutatorCache [get_class ($ this )][$ key ] === true ) {
652
+ } elseif (isset (static ::$ getAttributeMutatorCache [get_class ($ this )][$ key ]) &&
653
+ static ::$ getAttributeMutatorCache [get_class ($ this )][$ key ] === true ) {
628
654
$ value = $ this ->mutateAttributeMarkedAttribute ($ key , $ value );
629
655
630
656
$ value = $ value instanceof DateTimeInterface
@@ -948,7 +974,8 @@ public function hasAttributeSetMutator($key)
948
974
949
975
return static ::$ setAttributeMutatorCache [$ class ][$ key ] = $ returnType &&
950
976
$ returnType instanceof ReflectionNamedType &&
951
- $ returnType ->getName () === Attribute::class;
977
+ $ returnType ->getName () === Attribute::class &&
978
+ is_callable ($ this ->{$ method }()->set );
952
979
}
953
980
954
981
/**
@@ -2029,7 +2056,7 @@ public function getMutatedAttributes()
2029
2056
*/
2030
2057
public static function cacheMutatedAttributes ($ class )
2031
2058
{
2032
- static ::$ attributeMutatorCache [$ class ] =
2059
+ static ::$ getAttributeMutatorCache [$ class ] =
2033
2060
collect ($ attributeMutatorMethods = static ::getAttributeMarkedMutatorMethods ($ class ))
2034
2061
->mapWithKeys (function ($ match ) {
2035
2062
return [lcfirst (static ::$ snakeAttributes ? Str::snake ($ match ) : $ match ) => true ];
0 commit comments