55namespace Webfox \InertiaDataProviders ;
66
77use Closure ;
8- use Illuminate \Contracts \Support \Arrayable ;
9- use Illuminate \Contracts \Support \Jsonable ;
10- use Inertia \DeferProp ;
8+ use ReflectionClass ;
119use Inertia \LazyProp ;
1210use Inertia \Response ;
13- use ReflectionClass ;
1411use ReflectionMethod ;
15- use ReflectionNamedType ;
12+ use Inertia \ DeferProp ;
1613use ReflectionProperty ;
14+ use ReflectionNamedType ;
15+ use Illuminate \Contracts \Support \Jsonable ;
1716use Symfony \Component \VarDumper \VarDumper ;
17+ use Illuminate \Contracts \Support \Arrayable ;
18+ use Webfox \InertiaDataProviders \WrappingAttributes \WrappingAttribute ;
1819use Webfox \InertiaDataProviders \AttributeNameFormatters \AttributeNameFormatter ;
1920
2021abstract class DataProvider implements Arrayable, Jsonable
@@ -30,30 +31,41 @@ public static function collection(DataProvider|array ...$dataProviders): DataPro
3031
3132 public function toArray (): array
3233 {
33- $ staticData = $ this ->staticData instanceof Arrayable ? $ this ->staticData ->toArray () : $ this ->staticData ;
34+ $ staticData = $ this ->staticData instanceof Arrayable ? $ this ->staticData ->toArray () : $ this ->staticData ;
3435 $ reflectionClass = (new ReflectionClass ($ this ));
3536
3637 $ convertedProperties = collect ($ reflectionClass ->getProperties (ReflectionProperty::IS_PUBLIC ))
37- ->filter (fn (ReflectionProperty $ property ) => ! $ property ->isStatic ())
38- ->mapWithKeys (fn (ReflectionProperty $ property ) => [$ property ->getName () => $ property ->getValue ($ this )])
39- ->map (fn ($ value ) => $ value instanceof Arrayable ? $ value ->toArray () : $ value );
38+ ->filter (fn (ReflectionProperty $ property ) => !$ property ->isStatic ())
39+ ->mapWithKeys (fn (ReflectionProperty $ property ) => [$ property ->getName () => $ property ->getValue ($ this )])
40+ ->map (fn ($ value ) => $ value instanceof Arrayable ? $ value ->toArray () : $ value );
4041
4142 $ convertedMethods = collect ($ reflectionClass ->getMethods (ReflectionMethod::IS_PUBLIC ))
42- ->filter (fn (ReflectionMethod $ method ) => ! $ method ->isStatic () && ! in_array ($ method ->name , $ this ->excludedMethods ))
43+ ->filter (fn (ReflectionMethod $ method ) => !$ method ->isStatic () && !in_array ($ method ->name , $ this ->excludedMethods ))
4344 ->mapWithKeys (function (ReflectionMethod $ method ) {
45+ $ attributes = $ method ->getAttributes ();
4446 $ returnType = $ method ->getReturnType ();
47+
4548 if ($ returnType instanceof ReflectionNamedType && in_array ($ returnType ->getName (), [DeferProp::class, LazyProp::class, Closure::class])) {
4649 return [$ method ->name => $ method ->invoke ($ this )];
4750 }
4851
49- return [$ method ->name => fn () => app ()->call ([$ this , $ method ->name ])];
52+ if (count ($ attributes ) > 0 ) {
53+ foreach ($ attributes as $ attribute ) {
54+ $ attributeInstance = $ attribute ->newInstance ();
55+ if ($ attributeInstance instanceof WrappingAttribute) {
56+ return [$ method ->name => $ attributeInstance (fn () => app ()->call ([$ this , $ method ->name ]))];
57+ }
58+ }
59+ }
60+
61+ return [$ method ->name => fn () => app ()->call ([$ this , $ method ->name ])];
5062 });
5163
5264 return collect ()
5365 ->merge ($ staticData )
5466 ->merge ($ convertedProperties )
5567 ->merge ($ convertedMethods )
56- ->mapWithKeys (fn ($ value , $ key ) => [$ this ->attributeNameFormatter ()($ key ) => $ value ])
68+ ->mapWithKeys (fn ($ value , $ key ) => [$ this ->attributeNameFormatter ()($ key ) => $ value ])
5769 ->toArray ();
5870 }
5971
0 commit comments