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)
2145
2145
*/
2146
2146
public function getMutatedAttributes ()
2147
2147
{
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 );
2152
2150
}
2153
2151
2154
- return static ::$ mutatorCache [$ class ];
2152
+ return static ::$ mutatorCache [static :: class];
2155
2153
}
2156
2154
2157
2155
/**
2158
2156
* Extract and cache all the mutated attributes of a class.
2159
2157
*
2160
- * @param string $class
2158
+ * @param object| string $classOrInstance
2161
2159
* @return void
2162
2160
*/
2163
- public static function cacheMutatedAttributes ($ class )
2161
+ public static function cacheMutatedAttributes ($ classOrInstance )
2164
2162
{
2163
+ $ reflection = new ReflectionClass ($ classOrInstance );
2164
+
2165
+ $ class = $ reflection ->getName ();
2166
+
2165
2167
static ::$ getAttributeMutatorCache [$ class ] =
2166
- collect ($ attributeMutatorMethods = static ::getAttributeMarkedMutatorMethods ($ class ))
2168
+ collect ($ attributeMutatorMethods = static ::getAttributeMarkedMutatorMethods ($ classOrInstance ))
2167
2169
->mapWithKeys (function ($ match ) {
2168
2170
return [lcfirst (static ::$ snakeAttributes ? Str::snake ($ match ) : $ match ) => true ];
2169
2171
})->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