1818
1919use function Tempest \invoke ;
2020use function Tempest \Support \arr ;
21+ use function Tempest \Support \str ;
2122
2223final class InertiaResponse implements Response
2324{
@@ -30,36 +31,34 @@ public function __construct(
3031 readonly string $ rootView ,
3132 readonly string $ version ,
3233 ) {
33-
3434 $ deferredProps = self ::resolvePropKeysThatShouldDefer (
3535 props: $ props ,
3636 request: $ request ,
37- component: $ page
37+ component: $ page,
3838 );
3939
4040 $ mergeProps = self ::resolvePropKeysThatShouldMerge (
4141 props: $ props ,
42- request: $ request
42+ request: $ request,
4343 );
4444
4545 // Build page data immutably
4646 $ pageData = array_merge (
4747 [
48- 'component ' => $ page ,
49- 'props ' => self ::composeProps (
48+ 'component ' => $ page ,
49+ 'props ' => self ::composeProps (
5050 props: $ this ->props ,
5151 request: $ this ->request ,
52- component: $ page
52+ component: $ page,
5353 ),
54- 'url ' => $ request ->uri ,
55- 'version ' => $ version ,
54+ 'url ' => $ request ->uri ,
55+ 'version ' => $ version ,
5656 ],
5757 count ($ deferredProps ) ? ['deferredProps ' => $ deferredProps ] : [],
58- count ($ mergeProps ) ? ['mergeProps ' => $ mergeProps ] : []
58+ count ($ mergeProps ) ? ['mergeProps ' => $ mergeProps ] : [],
5959 );
6060
61- $ isInertia = ($ request ->headers ->offsetExists (Header::INERTIA )
62- && $ request ->headers [Header::INERTIA ] === 'true ' );
61+ $ isInertia = $ request ->headers ->offsetExists (Header::INERTIA ) && $ request ->headers [Header::INERTIA ] === 'true ' ;
6362
6463 $ this ->body = $ isInertia
6564 ? (function () use ($ pageData ) {
@@ -70,7 +69,7 @@ public function __construct(
7069 })()
7170 : new InertiaBaseView (
7271 view: $ rootView ,
73- pageData: $ pageData
72+ pageData: $ pageData,
7473 );
7574 }
7675
@@ -88,11 +87,7 @@ private static function composeProps(array $props, Request $request, string $com
8887 $ always = static ::resolveAlwaysProps ($ props );
8988 $ partial = static ::resolvePartialProps ($ request , $ component , $ props );
9089
91- return static ::evaluateProps (
92- array_merge ($ always , $ partial ),
93- $ request ,
94- true
95- );
90+ return static ::evaluateProps (array_merge ($ always , $ partial ), $ request , true );
9691 }
9792
9893 /**
@@ -119,20 +114,13 @@ private static function resolvePartialProps(Request $request, string $component,
119114 $ only = array_filter (explode (', ' , $ headers [Header::PARTIAL_ONLY ] ?? '' ));
120115 $ except = array_filter (explode (', ' , $ headers [Header::PARTIAL_EXCEPT ] ?? '' ));
121116
122- $ filtered = $ only
123- ? array_intersect_key ($ props , array_flip ($ only ))
124- : $ props ;
117+ $ filtered = $ only ? array_intersect_key ($ props , array_flip ($ only )) : $ props ;
125118
126- return array_filter (
127- $ filtered ,
128- static fn ($ key ) => !in_array ($ key , $ except , true ),
129- ARRAY_FILTER_USE_KEY
130- );
119+ return array_filter ($ filtered , static fn ($ key ) => !in_array ($ key , $ except , true ), ARRAY_FILTER_USE_KEY );
131120 }
132121
133122 private static function resolvePropKeysThatShouldDefer (array $ props , Request $ request , string $ component ): array
134123 {
135-
136124 if (static ::isPartial ($ request , $ component )) {
137125 return [];
138126 }
@@ -143,7 +131,7 @@ private static function resolvePropKeysThatShouldDefer(array $props, Request $re
143131 })
144132 ->map (fn (DeferProp $ prop , string $ key ) => [
145133 'group ' => $ prop ->group ,
146- 'key ' => $ key ,
134+ 'key ' => $ key ,
147135 ])
148136 ->groupBy (fn (array $ prop ) => $ prop ['group ' ])
149137 ->map (fn (array $ group ) => arr ($ group )->pluck ('key ' )->toArray ())
@@ -164,33 +152,27 @@ private static function resolvePropKeysThatShouldMerge(array $props, Request $re
164152 * @pure
165153 * Evaluates props recursively.
166154 */
167- private static function evaluateProps (array $ props , Request $ request ): array
155+ private static function evaluateProps (array $ props , Request $ request, bool $ unpackDotProps = true ): array // @mago-expect best-practices/no-boolean-flag-parameter
168156 {
169157 return arr ($ props )
170158 ->map (function ($ value , string |int $ key ) use ($ request ): array {
171- $ evaluated = $ value instanceof Closure ? invoke ($ value ) : $ value ;
172- $ evaluated = ($ evaluated instanceof LazyProp || $ evaluated instanceof AlwaysProp)
173- ? $ evaluated ()
174- : $ evaluated ;
175- $ evaluated = $ evaluated instanceof ImmutableArray
176- ? $ evaluated ->toArray ()
177- : $ evaluated ;
159+ $ evaluated = ($ value instanceof Closure) ? invoke ($ value ) : $ value ;
160+ $ evaluated =
161+ $ evaluated instanceof LazyProp || $ evaluated instanceof AlwaysProp ? $ evaluated () : $ evaluated ;
162+ $ evaluated = ($ evaluated instanceof ImmutableArray) ? $ evaluated ->toArray () : $ evaluated ;
178163 $ evaluated = is_array ($ evaluated )
179- ? self ::evaluateProps ($ evaluated , $ request , false )
164+ ? self ::evaluateProps ($ evaluated , $ request , unpackDotProps: false )
180165 : $ evaluated ;
181166
182167 return [$ key , $ evaluated ];
183168 })
184- ->reduce (
185- function (array $ acc , array $ item ): array {
186- [$ key , $ value ] = $ item ;
187- if (str_contains ($ key , '. ' )) {
188- return arr ($ acc )->set ($ key , $ value )->toArray ();
189- }
190- $ acc [$ key ] = $ value ;
191- return $ acc ;
192- },
193- []
194- );
169+ ->reduce (function (array $ acc , array $ item ) use ($ unpackDotProps ): array {
170+ [$ key , $ value ] = $ item ;
171+ if ($ unpackDotProps && is_string ($ key ) && str_contains ($ key , '. ' )) {
172+ return arr ($ acc )->set ($ key , $ value )->toArray ();
173+ }
174+ $ acc [$ key ] = $ value ;
175+ return $ acc ;
176+ }, []);
195177 }
196178}
0 commit comments