@@ -60,43 +60,58 @@ private function getReplicatorField(Blueprint $blueprint, string $field): Field
6060 $ remainingFieldPathComponents = explode ('. ' , $ field );
6161
6262 $ config = $ blueprint ->fields ()->all ()->get ($ remainingFieldPathComponents [0 ])->config ();
63- unset($ remainingFieldPathComponents [0 ]);
6463
65- foreach ($ remainingFieldPathComponents as $ index => $ fieldPathComponent ) {
66- unset($ remainingFieldPathComponents [$ fieldPathComponent ]);
67-
68- if (isset ($ config ['sets ' ])) {
69- $ config = collect ($ config ['sets ' ])
70- ->flatMap (fn (array $ setGroup ): array => $ setGroup ['sets ' ] ?? [])
71- ->get ($ fieldPathComponent );
72- }
73-
74- if (isset ($ config ['fields ' ])) {
75- $ fields = collect ($ config ['fields ' ])
76- ->flatMap (function ($ field ): array {
77- if (isset ($ field ['import ' ]) || (isset ($ field ['field ' ]) && is_string ($ field ['field ' ]))) {
78- return (new Fields ([$ field ]))
79- ->all ()
80- ->mapWithKeys (fn (Field $ field ) => [$ field ->handle () => [
81- 'handle ' => $ field ->handle (),
82- 'field ' => $ field ->config (),
83- ]])
84- ->all ();
85- }
86-
87- return [$ field ];
88- });
89-
90- if ($ fieldConfig = $ fields ->firstWhere ('handle ' , $ remainingFieldPathComponents [$ index ])) {
91- $ config = $ fieldConfig ['field ' ];
92- }
93- }
94- }
64+ $ config = $ this ->getConfig ($ config , $ remainingFieldPathComponents );
9565
9666 if (! isset ($ config ['type ' ])) {
9767 throw new \Exception ("Cannot find Replicator field [ $ field] " );
9868 }
9969
10070 return new Field (Str::afterLast ($ field , '. ' ), $ config );
10171 }
72+
73+ private function getConfig (array $ config , array $ remainingFieldPathComponents ): array
74+ {
75+ $ isReplicator = isset ($ config ['type ' ]) && in_array ($ config ['type ' ], ['bard ' , 'replicator ' ]);
76+
77+ if ($ isReplicator ) {
78+ $ flattenedSets = collect ($ config ['sets ' ])
79+ ->flatMap (fn (array $ setGroup ): array => $ setGroup ['sets ' ] ?? [])
80+ ->all ();
81+
82+ if (count ($ remainingFieldPathComponents ) === 1 ) {
83+ return $ config ;
84+ }
85+
86+ array_shift ($ remainingFieldPathComponents );
87+
88+ return $ this ->getConfig ($ flattenedSets , $ remainingFieldPathComponents );
89+ }
90+
91+ $ fields = $ this ->resolveFields ($ config [$ remainingFieldPathComponents [0 ]]['fields ' ]);
92+
93+ array_shift ($ remainingFieldPathComponents );
94+
95+ return $ this ->getConfig ($ fields [$ remainingFieldPathComponents [0 ]]['field ' ], $ remainingFieldPathComponents );
96+ }
97+
98+ private function resolveFields (array $ fields ): array
99+ {
100+ return collect ($ fields )
101+ ->flatMap (function ($ field ): array {
102+ if (isset ($ field ['import ' ]) || (isset ($ field ['field ' ]) && is_string ($ field ['field ' ]))) {
103+ return (new Fields ([$ field ]))
104+ ->all ()
105+ ->map (fn (Field $ field ) => [
106+ 'handle ' => $ field ->handle (),
107+ 'field ' => $ field ->config (),
108+ ])
109+ ->all ();
110+ }
111+
112+ return [$ field ];
113+ })
114+ ->keyBy ('handle ' )
115+ ->all ();
116+ }
102117}
0 commit comments