5
5
use Illuminate \Database \Eloquent \MissingAttributeException ;
6
6
use Illuminate \Database \Eloquent \Model ;
7
7
use Illuminate \Support \Str ;
8
- use ReflectionProperty ;
9
8
use Spatie \LaravelData \Attributes \LoadRelation ;
10
9
use Spatie \LaravelData \Support \DataProperty ;
11
10
12
11
class NormalizedModel implements Normalized
13
12
{
14
13
protected array $ properties = [];
15
14
16
- protected ReflectionProperty $ castsProperty ;
17
-
18
- protected ReflectionProperty $ attributesProperty ;
19
-
20
15
public function __construct (
21
16
protected Model $ model ,
22
17
) {
23
18
}
24
19
25
20
public function getProperty (string $ name , DataProperty $ dataProperty ): mixed
26
21
{
27
- $ value = array_key_exists ($ name , $ this ->properties )
28
- ? $ this ->properties [$ name ]
29
- : $ this ->fetchNewProperty ($ name , $ dataProperty );
22
+ $ propertyName = $ this ->model ::$ snakeAttributes ? Str::snake ($ name ) : $ name ;
23
+
24
+ $ value = array_key_exists ($ propertyName , $ this ->properties )
25
+ ? $ this ->properties [$ propertyName ]
26
+ : $ this ->fetchNewProperty ($ propertyName , $ dataProperty );
30
27
31
28
if ($ value === null && ! $ dataProperty ->type ->isNullable ) {
32
29
return UnknownProperty::create ();
@@ -37,46 +34,31 @@ public function getProperty(string $name, DataProperty $dataProperty): mixed
37
34
38
35
protected function fetchNewProperty (string $ name , DataProperty $ dataProperty ): mixed
39
36
{
40
- if ($ dataProperty ->attributes ->contains (fn (object $ attribute ) => $ attribute ::class === LoadRelation::class)) {
37
+ $ camelName = Str::camel ($ name );
38
+
39
+ if ($ dataProperty ->attributes ->hasAttribute (LoadRelation::class)) {
41
40
if (method_exists ($ this ->model , $ name )) {
42
41
$ this ->model ->loadMissing ($ name );
42
+ } elseif (method_exists ($ this ->model , $ camelName )) {
43
+ $ this ->model ->loadMissing ($ camelName );
43
44
}
44
45
}
45
46
46
47
if ($ this ->model ->relationLoaded ($ name )) {
47
48
return $ this ->properties [$ name ] = $ this ->model ->getRelation ($ name );
48
49
}
50
+ if ($ this ->model ->relationLoaded ($ camelName )) {
51
+ return $ this ->properties [$ name ] = $ this ->model ->getRelation ($ camelName );
52
+ }
49
53
50
- if (!$ this ->model ->isRelation ($ name )) {
54
+ if (! $ this ->model ->isRelation ($ name) && ! $ this -> model -> isRelation ( $ camelName )) {
51
55
try {
52
- $ propertyName = $ this ->model ::$ snakeAttributes ? Str::snake ($ name ) : $ name ;
53
- return $ this ->properties [$ name ] = $ this ->model ->getAttribute ($ propertyName );
56
+ return $ this ->properties [$ name ] = $ this ->model ->getAttribute ($ name );
54
57
} catch (MissingAttributeException ) {
55
58
// Fallback if missing Attribute
56
59
}
57
60
}
58
61
59
62
return $ this ->properties [$ name ] = UnknownProperty::create ();
60
63
}
61
-
62
- protected function hasModelAttribute (string $ name ): bool
63
- {
64
- if (method_exists ($ this ->model , 'hasAttribute ' )) {
65
- return $ this ->model ->hasAttribute ($ name );
66
- }
67
-
68
- // TODO: remove this once we stop supporting Laravel 10
69
- if (! isset ($ this ->attributesProperty )) {
70
- $ this ->attributesProperty = new ReflectionProperty ($ this ->model , 'attributes ' );
71
- }
72
-
73
- if (! isset ($ this ->castsProperty )) {
74
- $ this ->castsProperty = new ReflectionProperty ($ this ->model , 'casts ' );
75
- }
76
-
77
- return array_key_exists ($ name , $ this ->attributesProperty ->getValue ($ this ->model )) ||
78
- array_key_exists ($ name , $ this ->castsProperty ->getValue ($ this ->model )) ||
79
- $ this ->model ->hasGetMutator ($ name ) ||
80
- $ this ->model ->hasAttributeMutator ($ name );
81
- }
82
64
}
0 commit comments