@@ -102,23 +102,31 @@ public function serialize($data)
102102 }
103103
104104 /**
105+ * @param array $included
105106 * @param ResourceInterface $model
106107 * @return array
107108 */
108- protected function serializeModel (ResourceInterface $ model , array $ included = null )
109+ protected function serializeModel (ResourceInterface $ model , array $ included = [] )
109110 {
110111 $ fields = $ this ->getRequestedFields ();
111112 $ type = $ this ->pluralize ? Inflector::pluralize ($ model ->getType ()) : $ model ->getType ();
112113 $ fields = isset ($ fields [$ type ]) ? $ fields [$ type ] : [];
113114
115+ $ topLevel = array_map (function ($ item ) {
116+ if (($ pos = strrpos ($ item , '. ' )) !== false ) {
117+ return substr ($ item , 0 , $ pos );
118+ }
119+ return $ item ;
120+ }, $ included );
121+
114122 $ attributes = $ model ->getResourceAttributes ($ fields );
115123 $ attributes = array_combine ($ this ->prepareMemberNames (array_keys ($ attributes )), array_values ($ attributes ));
116124
117125 $ data = array_merge ($ this ->serializeIdentifier ($ model ), [
118126 'attributes ' => $ attributes ,
119127 ]);
120128
121- $ relationships = $ model ->getResourceRelationships ($ included );
129+ $ relationships = $ model ->getResourceRelationships ($ topLevel );
122130 if (!empty ($ relationships )) {
123131 foreach ($ relationships as $ name => $ items ) {
124132 $ relationship = [];
@@ -161,19 +169,9 @@ protected function serializeResource(ResourceInterface $resource)
161169 if ($ this ->request ->getIsHead ()) {
162170 return null ;
163171 } else {
164- $ included = $ topLevel = $ this ->getIncluded ();
165-
166- if ($ included !== null ) {
167- $ topLevel = array_map (function ($ item ) {
168- if (($ pos = strrpos ($ item , '. ' )) !== false ) {
169- return substr ($ item , 0 , $ pos );
170- }
171- return $ item ;
172- }, $ included );
173- }
174-
172+ $ included = $ this ->getIncluded ();
175173 $ data = [
176- 'data ' => $ this ->serializeModel ($ resource , $ topLevel )
174+ 'data ' => $ this ->serializeModel ($ resource , $ included )
177175 ];
178176
179177 $ relatedResources = $ this ->serializeIncluded ($ resource , $ included );
@@ -210,37 +208,31 @@ protected function serializeIdentifier(ResourceIdentifierInterface $identifier)
210208
211209 /**
212210 * @param ResourceInterface|array $resources
213- * @param null|array $included
211+ * @param array $included
212+ * @param true $assoc
214213 * @return array
215214 */
216- protected function serializeIncluded ($ resources , array $ included = null )
215+ protected function serializeIncluded ($ resources , array $ included = [], $ assoc = false )
217216 {
218217 $ resources = is_array ($ resources ) ? $ resources : [$ resources ];
219218 $ data = [];
220219
221- if ($ included === null ) {
222- return [];
223- }
224-
225220 $ inclusion = [];
226- $ linked = [];
227221 foreach ($ included as $ path ) {
228222 if (($ pos = strrpos ($ path , '. ' )) === false ) {
229- $ linked [] = $ path ;
230223 $ inclusion [$ path ] = [];
231224 continue ;
232225 }
233226 $ name = substr ($ path , $ pos + 1 );
234227 $ key = substr ($ path , 0 , $ pos );
235228 $ inclusion [$ key ][] = $ name ;
236- $ linked [] = $ key ;
237229 }
238230
239231 foreach ($ resources as $ resource ) {
240232 if (!$ resource instanceof ResourceInterface) {
241233 continue ;
242234 }
243- $ relationships = $ resource ->getResourceRelationships ($ linked );
235+ $ relationships = $ resource ->getResourceRelationships (array_keys ( $ inclusion ) );
244236 foreach ($ relationships as $ name => $ relationship ) {
245237 if ($ relationship === null ) {
246238 continue ;
@@ -249,18 +241,21 @@ protected function serializeIncluded($resources, array $included = null)
249241 $ relationship = [$ relationship ];
250242 }
251243 foreach ($ relationship as $ model ) {
252- if ($ model instanceof ResourceInterface) {
253- $ uniqueKey = $ model ->getType () . '/ ' . $ model ->getId ();
244+ if (!$ model instanceof ResourceInterface) {
245+ continue ;
246+ }
247+ $ uniqueKey = $ model ->getType () . '/ ' . $ model ->getId ();
248+ if (!isset ($ data [$ uniqueKey ])) {
254249 $ data [$ uniqueKey ] = $ this ->serializeModel ($ model , $ inclusion [$ name ]);
255- if (! empty ( $ inclusion [ $ name ])) {
256- $ data = array_merge ( $ data , $ this -> serializeIncluded ( $ model , $ inclusion [$ name ]));
257- }
250+ }
251+ if (! empty ( $ inclusion [$ name ])) {
252+ $ data = array_merge ( $ data , $ this -> serializeIncluded ( $ model , $ inclusion [ $ name ], true ));
258253 }
259254 }
260255 }
261256 }
262257
263- return array_values ($ data );
258+ return $ assoc ? $ data : array_values ($ data );
264259 }
265260
266261 /**
@@ -359,9 +354,6 @@ protected function getRequestedFields()
359354 protected function getIncluded ()
360355 {
361356 $ include = $ this ->request ->get ($ this ->expandParam );
362- if (!$ this ->request ->isGet ) {
363- return null ;
364- }
365357 return is_string ($ include ) ? array_map ($ this ->formatMemberName , preg_split ('/\s*,\s*/ ' , $ include , -1 , PREG_SPLIT_NO_EMPTY )) : [];
366358 }
367359
0 commit comments