6
6
*/
7
7
8
8
class RestfulFormatterHalJson extends \RestfulFormatterBase implements \RestfulFormatterInterface {
9
+
9
10
/**
10
11
* Content Type
11
12
*
@@ -24,8 +25,8 @@ public function prepare(array $data) {
24
25
return $ data ;
25
26
}
26
27
// Here we get the data after calling the backend storage for the resources.
27
-
28
28
$ curies_resource = $ this ->withCurie ($ this ->handler ->getResourceName ());
29
+
29
30
$ output = array ();
30
31
31
32
foreach ($ data as &$ row ) {
@@ -40,7 +41,7 @@ public function prepare(array $data) {
40
41
method_exists ($ this ->handler , 'isListRequest ' ) &&
41
42
$ this ->handler ->isListRequest ()
42
43
) {
43
- // Get the total number of items for the current request without pagination.
44
+ // Get total number of items for the current request w/out pagination.
44
45
$ output ['count ' ] = $ this ->handler ->getTotalCount ();
45
46
}
46
47
if (method_exists ($ this ->handler , 'additionalHateoas ' )) {
@@ -68,7 +69,7 @@ public function prepare(array $data) {
68
69
/**
69
70
* Add HATEOAS links to list of item.
70
71
*
71
- * @param $data
72
+ * @param array $data
72
73
* The data array after initial massaging.
73
74
*/
74
75
protected function addHateoas (array &$ data ) {
@@ -142,20 +143,31 @@ public function prepareRow(array $row, array &$output) {
142
143
return $ row ;
143
144
}
144
145
145
- foreach ($ this ->handler ->getPublicFields () as $ pubilc_field_name => $ public_field ) {
146
+ $ embedded = array ();
147
+
148
+ foreach ($ this ->handler ->getPublicFields () as $ public_field_name => $ public_field ) {
146
149
if (empty ($ public_field ['resource ' ])) {
147
150
// Not a resource.
148
151
continue ;
149
152
}
150
153
151
- if (empty ($ row [$ pubilc_field_name ])) {
154
+ if (empty ($ row [$ public_field_name ])) {
152
155
// No value.
153
156
continue ;
154
157
}
155
158
156
- $ output += array ('_embedded ' => array ());
159
+ $ nested_embed = $ this ->handler ->isListRequest ();
160
+
161
+ if ($ nested_embed ) {
162
+ $ output += array ('_embedded ' => array ());
163
+ }
164
+
165
+ $ this ->moveReferencesToEmbeds ($ embedded , $ row , $ public_field , $ public_field_name , $ output );
157
166
158
- $ this ->moveReferencesToEmbeds ($ output , $ row , $ public_field , $ pubilc_field_name );
167
+ }
168
+
169
+ if (!empty ($ embedded ) && $ nested_embed ) {
170
+ $ row ['_embedded ' ] = $ embedded ;
159
171
}
160
172
161
173
return $ row ;
@@ -219,70 +231,71 @@ protected function getCurie() {
219
231
/**
220
232
* Move the fields referencing other resources to the _embed key.
221
233
*
222
- * @param array $output
223
- * Output array to be modified.
234
+ * Note that for multiple value entityreference
235
+ * fields $row[$public_field_name] will be an array of values rather than a
236
+ * single value.
237
+ *
238
+ * @param array $embedded
239
+ * Embedded array to be modified.
224
240
* @param array $row
225
241
* The row being processed.
226
242
* @param array $public_field
227
243
* The public field configuration array.
228
- * @param $public_field_name
244
+ * @param string $public_field_name
229
245
* The name of the public field.
246
+ * @param array $output
247
+ * Output array to be modified.
230
248
*/
231
- protected function moveReferencesToEmbeds (array &$ output , array &$ row , $ public_field , $ public_field_name ) {
232
- $ value_metadata = $ this ->handler ->getValueMetadata ($ row ['id ' ], $ public_field_name );
233
- if (\RestfulBase::isArrayNumeric ($ row [$ public_field_name ])) {
234
- foreach ($ row [$ public_field_name ] as $ index => $ resource_row ) {
235
- if (empty ($ value_metadata [$ index ])) {
249
+ protected function moveReferencesToEmbeds (array &$ embedded , array &$ row , $ public_field , $ public_field_name , array &$ output ) {
250
+ $ values_metadata = $ this ->handler ->getValueMetadata ($ row ['id ' ], $ public_field_name );
251
+
252
+ // Wrap the row in an array if it isn't.
253
+ if (!is_array ($ row [$ public_field_name ])) {
254
+ $ row [$ public_field_name ] = array ();
255
+ }
256
+ $ rows = RestfulBase::isArrayNumeric ($ row [$ public_field_name ]) ? $ row [$ public_field_name ] : array ($ row [$ public_field_name ]);
257
+
258
+ foreach ($ rows as $ subindex => $ subrow ) {
259
+ $ metadata = $ values_metadata [$ subindex ];
260
+
261
+ // Loop through each value for the field.
262
+ foreach ($ subrow as $ index => $ resource_row ) {
263
+ if (empty ($ metadata [$ index ])) {
236
264
// No metadata.
237
265
continue ;
238
266
}
239
- $ metadata = $ value_metadata [$ index ];
240
- $ this ->moveMetadataResource ($ output , $ public_field , $ metadata , $ resource_row );
241
- }
242
- }
243
- else {
244
- $ this ->moveMetadataResource ($ output , $ public_field , $ value_metadata , $ row [$ public_field_name ]);
245
- }
246
267
247
- // Remove the original reference.
248
- unset($ row [$ public_field_name ]);
249
- }
268
+ // If there is no resource name in the metadata for this particular
269
+ // value, assume that we are referring to the first resource in the
270
+ // field definition.
271
+ $ resource_name = NULL ;
272
+ if (!empty ($ metadata ['resource_name ' ])) {
273
+ // Make sure that the resource in the metadata exists in the list of
274
+ // resources available for this particular public field.
275
+ foreach ($ public_field ['resource ' ] as $ resource ) {
276
+ if ($ resource ['name ' ] != $ metadata ['resource_name ' ]) {
277
+ continue ;
278
+ }
279
+ $ resource_name = $ metadata ['resource_name ' ];
280
+ }
281
+ }
282
+ if (empty ($ resource_name )) {
283
+ $ resource = reset ($ public_field ['resource ' ]);
284
+ $ resource_name = $ resource ['name ' ];
285
+ }
250
286
251
- /**
252
- * Move a single "embedded resource" to be under the "_embedded" property.
253
- *
254
- * @param array $output
255
- * Output array to be modified. Passed by reference.
256
- * @param array $public_field
257
- * The public field configuration array.
258
- * @param $metadata
259
- * The metadata to add.
260
- * @param $resource_row
261
- * The resource row.
262
- */
263
- protected function moveMetadataResource (array &$ output , $ public_field , $ metadata , $ resource_row ) {
264
- // If there is no resource name in the metadata for this particular value,
265
- // assume that we are referring to the first resource in the field
266
- // definition.
267
- $ resource_name = NULL ;
268
- if (!empty ($ metadata ['resource_name ' ])) {
269
- // Make sure that the resource in the metadata exists in the list of
270
- // resources available for this particular public field.
271
- foreach ($ public_field ['resource ' ] as $ resource ) {
272
- if ($ resource ['name ' ] != $ metadata ['resource_name ' ]) {
273
- continue ;
287
+ $ curies_resource = $ this ->withCurie ($ resource_name );
288
+ $ prepared_row = $ this ->prepareRow ($ subrow , $ output );
289
+ if ($ this ->handler ->isListRequest ()) {
290
+ $ embedded [$ curies_resource ][] = $ prepared_row ;
291
+ }
292
+ else {
293
+ $ output ['_embedded ' ][$ curies_resource ][] = $ prepared_row ;
274
294
}
275
- $ resource_name = $ metadata ['resource_name ' ];
276
295
}
277
296
}
278
- if (empty ($ resource_name )) {
279
- $ resource = reset ($ public_field ['resource ' ]);
280
- $ resource_name = $ resource ['name ' ];
281
- }
282
297
283
- $ curies_resource = $ this ->withCurie ($ resource_name );
284
- $ resource_row = $ this ->prepareRow ($ resource_row , $ output );
285
- $ output ['_embedded ' ][$ curies_resource ][] = $ resource_row ;
298
+ // Remove the original reference.
299
+ unset($ row [$ public_field_name ]);
286
300
}
287
-
288
301
}
0 commit comments