16
16
use Spatie \LaravelData \Optional ;
17
17
use Spatie \LaravelData \Resolvers \NameMappersResolver ;
18
18
use Spatie \LaravelData \Support \Annotations \DataIterableAnnotation ;
19
+ use Spatie \LaravelData \Support \AttributeCollection ;
19
20
use Spatie \LaravelData \Support \DataProperty ;
20
21
21
22
class DataPropertyFactory
@@ -35,9 +36,7 @@ public function build(
35
36
?DataIterableAnnotation $ classDefinedDataIterableAnnotation = null ,
36
37
?AutoLazy $ classAutoLazy = null ,
37
38
): DataProperty {
38
- $ attributes = collect ($ reflectionProperty ->getAttributes ())
39
- ->filter (fn (ReflectionAttribute $ reflectionAttribute ) => class_exists ($ reflectionAttribute ->getName ()))
40
- ->map (fn (ReflectionAttribute $ reflectionAttribute ) => $ reflectionAttribute ->newInstance ());
39
+ $ attributes = AttributeCollection::makeFromReflectionAttributes ($ reflectionProperty ->getAttributes ());
41
40
42
41
$ type = $ this ->typeFactory ->buildProperty (
43
42
$ reflectionProperty ->getType (),
@@ -61,17 +60,11 @@ public function build(
61
60
default => null ,
62
61
};
63
62
64
- $ computed = $ attributes ->contains (
65
- fn (object $ attribute ) => $ attribute instanceof Computed
66
- );
63
+ $ computed = $ attributes ->hasAttribute (Computed::class);
67
64
68
- $ hidden = $ attributes ->contains (
69
- fn (object $ attribute ) => $ attribute instanceof Hidden
70
- );
65
+ $ hidden = $ attributes ->hasAttribute (Hidden::class);
71
66
72
- $ validate = ! $ attributes ->contains (
73
- fn (object $ attribute ) => $ attribute instanceof WithoutValidation
74
- ) && ! $ computed ;
67
+ $ validate = ! $ attributes ->hasAttribute (WithoutValidation::class) && ! $ computed ;
75
68
76
69
if (! $ reflectionProperty ->isPromoted ()) {
77
70
$ hasDefaultValue = $ reflectionProperty ->hasDefaultValue ();
@@ -103,8 +96,8 @@ className: $reflectionProperty->class,
103
96
autoLazy: $ autoLazy ,
104
97
hasDefaultValue: $ hasDefaultValue ,
105
98
defaultValue: $ defaultValue ,
106
- cast: $ attributes ->first ( fn ( object $ attribute ) => $ attribute instanceof GetsCast)?->get(),
107
- transformer: $ attributes ->first ( fn ( object $ attribute ) => $ attribute instanceof WithTransformer || $ attribute instanceof WithCastAndTransformer)?->get(),
99
+ cast: $ attributes ->getAttribute ( GetsCast::class )?->get(),
100
+ transformer: ( $ attributes ->getAttribute (WithTransformer::class) ?? $ attributes -> getAttribute ( WithCastAndTransformer::class) )?->get(),
108
101
inputMappedName: $ inputMappedName ,
109
102
outputMappedName: $ outputMappedName ,
110
103
attributes: $ attributes ,
0 commit comments