File tree Expand file tree Collapse file tree 2 files changed +52
-8
lines changed
src/Illuminate/Database/Eloquent/Concerns Expand file tree Collapse file tree 2 files changed +52
-8
lines changed Original file line number Diff line number Diff line change @@ -2145,25 +2145,27 @@ public function hasAppended($attribute)
21452145 */
21462146 public function getMutatedAttributes ()
21472147 {
2148- $ class = static ::class;
2149-
2150- if (! isset (static ::$ mutatorCache [$ class ])) {
2151- static ::cacheMutatedAttributes ($ class );
2148+ if (! isset (static ::$ mutatorCache [static ::class])) {
2149+ static ::cacheMutatedAttributes ($ this );
21522150 }
21532151
2154- return static ::$ mutatorCache [$ class ];
2152+ return static ::$ mutatorCache [static :: class];
21552153 }
21562154
21572155 /**
21582156 * Extract and cache all the mutated attributes of a class.
21592157 *
2160- * @param string $class
2158+ * @param object| string $classOrInstance
21612159 * @return void
21622160 */
2163- public static function cacheMutatedAttributes ($ class )
2161+ public static function cacheMutatedAttributes ($ classOrInstance )
21642162 {
2163+ $ reflection = new ReflectionClass ($ classOrInstance );
2164+
2165+ $ class = $ reflection ->getName ();
2166+
21652167 static ::$ getAttributeMutatorCache [$ class ] =
2166- collect ($ attributeMutatorMethods = static ::getAttributeMarkedMutatorMethods ($ class ))
2168+ collect ($ attributeMutatorMethods = static ::getAttributeMarkedMutatorMethods ($ classOrInstance ))
21672169 ->mapWithKeys (function ($ match ) {
21682170 return [lcfirst (static ::$ snakeAttributes ? Str::snake ($ match ) : $ match ) => true ];
21692171 })->all ();
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Illuminate \Tests \Database ;
4+
5+ use Illuminate \Database \Eloquent \Casts \Attribute ;
6+ use Illuminate \Database \Eloquent \Concerns \HasAttributes ;
7+ use PHPUnit \Framework \TestCase ;
8+
9+ class DatabaseConcernsHasAttributesTest extends TestCase
10+ {
11+ public function testWithoutConstructor ()
12+ {
13+ $ instance = new HasAttributesWithoutConstructor ();
14+ $ attributes = $ instance ->getMutatedAttributes ();
15+ $ this ->assertEquals (['some_attribute ' ], $ attributes );
16+ }
17+
18+ public function testWithConstructorArguments ()
19+ {
20+ $ instance = new HasAttributesWithConstructorArguments (null );
21+ $ attributes = $ instance ->getMutatedAttributes ();
22+ $ this ->assertEquals (['some_attribute ' ], $ attributes );
23+ }
24+ }
25+
26+ class HasAttributesWithoutConstructor
27+ {
28+ use HasAttributes;
29+
30+ public function someAttribute (): Attribute
31+ {
32+ return new Attribute (function () {
33+ });
34+ }
35+ }
36+
37+ class HasAttributesWithConstructorArguments extends HasAttributesWithoutConstructor
38+ {
39+ public function __construct ($ someValue )
40+ {
41+ }
42+ }
You can’t perform that action at this time.
0 commit comments