/** * Extract a collection as an array * * @todo Remove 'resource' from event parameters for 1.0.0 * @todo Remove trigger of 'renderCollection.resource' for 1.0.0 * @param Collection $halCollection * @param int $depth depth of the current rendering recursion * @param int $maxDepth maximum rendering depth for the current metadata * @return array */ protected function extractCollection(Collection $halCollection, $depth = 0, $maxDepth = null) { $collection = []; $events = $this->getEventManager(); $routeIdentifierName = $halCollection->getRouteIdentifierName(); $entityRoute = $halCollection->getEntityRoute(); $entityRouteParams = $halCollection->getEntityRouteParams(); $entityRouteOptions = $halCollection->getEntityRouteOptions(); $metadataMap = $this->getMetadataMap(); foreach ($halCollection->getCollection() as $entity) { $eventParams = new ArrayObject(['collection' => $halCollection, 'entity' => $entity, 'resource' => $entity, 'route' => $entityRoute, 'routeParams' => $entityRouteParams, 'routeOptions' => $entityRouteOptions]); $events->trigger('renderCollection.resource', $this, $eventParams); $events->trigger('renderCollection.entity', $this, $eventParams); $entity = $eventParams['entity']; if (is_object($entity) && $metadataMap->has($entity)) { $entity = $this->getResourceFactory()->createEntityFromMetadata($entity, $metadataMap->get($entity)); } if ($entity instanceof Entity) { // Depth does not increment at this level $collection[] = $this->renderEntity($entity, $this->getRenderCollections(), $depth, $maxDepth); continue; } if (!is_array($entity)) { $entity = $this->getEntityExtractor()->extract($entity); } foreach ($entity as $key => $value) { if (is_object($value) && $metadataMap->has($value)) { $value = $this->getResourceFactory()->createEntityFromMetadata($value, $metadataMap->get($value)); } if ($value instanceof Entity) { $this->extractEmbeddedEntity($entity, $key, $value, $depth + 1, $maxDepth); } if ($value instanceof Collection) { $this->extractEmbeddedCollection($entity, $key, $value, $depth + 1, $maxDepth); } } $id = $this->getIdFromEntity($entity); if ($id === false) { // Cannot handle entities without an identifier // Return as-is $collection[] = $entity; continue; } if ($eventParams['entity'] instanceof LinkCollectionAwareInterface) { $links = $eventParams['entity']->getLinks(); } else { $links = new LinkCollection(); } if (isset($entity['links']) && $entity['links'] instanceof LinkCollection) { $links = $entity['links']; } /* $entity is always an array here. We don't have metadata config for arrays so the self link is forced by default (at the moment) and should be removed manually if not required. But at some point it should be discussed if it makes sense to force self links in this particular use-case. */ $selfLink = new Link('self'); $selfLink->setRoute($eventParams['route'], array_merge($eventParams['routeParams'], [$routeIdentifierName => $id]), $eventParams['routeOptions']); $links->add($selfLink); $entity['_links'] = $this->fromLinkCollection($links); $collection[] = $entity; } return $collection; }
/** * Extract a collection as an array * * @todo Remove 'resource' from event parameters for 1.0.0 * @todo Remove trigger of 'renderCollection.resource' for 1.0.0 * @param Collection $halCollection * @param int $depth depth of the current rendering recursion * @param int $maxDepth maximum rendering depth for the current metadata * @return array */ protected function extractCollection(Collection $halCollection, $depth = 0, $maxDepth = null) { $collection = array(); $events = $this->getEventManager(); $routeIdentifierName = $halCollection->getRouteIdentifierName(); $entityRoute = $halCollection->getEntityRoute(); $entityRouteParams = $halCollection->getEntityRouteParams(); $entityRouteOptions = $halCollection->getEntityRouteOptions(); $metadataMap = $this->getMetadataMap(); foreach ($halCollection->getCollection() as $entity) { $eventParams = new ArrayObject(array( 'collection' => $halCollection, 'entity' => $entity, 'resource' => $entity, 'route' => $entityRoute, 'routeParams' => $entityRouteParams, 'routeOptions' => $entityRouteOptions, )); $events->trigger('renderCollection.resource', $this, $eventParams); $events->trigger('renderCollection.entity', $this, $eventParams); $entity = $eventParams['entity']; if (is_object($entity) && $metadataMap->has($entity)) { $entity = $this->createEntityFromMetadata($entity, $metadataMap->get($entity)); } if ($entity instanceof Entity) { // Depth does not increment at this level $collection[] = $this->renderEntity($entity, $this->getRenderCollections(), $depth, $maxDepth); continue; } if (!is_array($entity)) { $entity = $this->convertEntityToArray($entity); } foreach ($entity as $key => $value) { if (is_object($value) && $metadataMap->has($value)) { $value = $this->createEntityFromMetadata($value, $metadataMap->get($value)); } if ($value instanceof Entity) { $this->extractEmbeddedEntity($entity, $key, $value, $depth + 1, $maxDepth); } if ($value instanceof Collection) { $this->extractEmbeddedCollection($entity, $key, $value, $depth + 1, $maxDepth); } } $id = $this->getIdFromEntity($entity); if ($id === false) { // Cannot handle entities without an identifier // Return as-is $collection[] = $entity; continue; } if ($eventParams['entity'] instanceof LinkCollectionAwareInterface) { $links = $eventParams['entity']->getLinks(); } else { $links = new LinkCollection(); } if (isset($entity['links']) && $entity['links'] instanceof LinkCollection) { $links = $entity['links']; } $selfLink = new Link('self'); $selfLink->setRoute( $eventParams['route'], array_merge($eventParams['routeParams'], array($routeIdentifierName => $id)), $eventParams['routeOptions'] ); $links->add($selfLink); $entity['_links'] = $this->fromLinkCollection($links); $collection[] = $entity; } return $collection; }