Example #1
0
 /**
  * "Render" a Collection
  *
  * Injects pagination links, if the composed collection is a Paginator, and
  * then loops through the collection to create the data structure representing
  * the collection.
  *
  * For each entity in the collection, the event "renderCollection.entity" is
  * triggered, with the following parameters:
  *
  * - "collection", which is the $jsonLDCollection passed to the method
  * - "entity", which is the current entity
  * - "route", the resource route that will be used to generate links
  * - "routeParams", any default routing parameters/substitutions to use in URL assembly
  * - "routeOptions", any default routing options to use in URL assembly
  *
  * This event can be useful particularly when you have multi-segment routes
  * and wish to ensure that route parameters are injected, or if you want to
  * inject query or fragment parameters.
  *
  * Event parameters are aggregated in an ArrayObject, which allows you to
  * directly manipulate them in your listeners:
  *
  * <code>
  * $params = $e->getParams();
  * $params['routeOptions']['query'] = ['format' => 'json'];
  * </code>
  *
  * @param  Collection $jsonLDCollection
  * @return array|ApiProblem Associative array representing the payload to render;
  *     returns ApiProblem if error in pagination occurs
  */
 public function renderCollection(Collection $jsonLDCollection)
 {
     $this->getEventManager()->trigger(__FUNCTION__, $this, ['collection' => $jsonLDCollection]);
     $collection = $jsonLDCollection->getCollection();
     $collectionName = $jsonLDCollection->getCollectionName();
     if ($collection instanceof Paginator) {
         $status = $this->injectPaginationProperties($jsonLDCollection);
         if ($status instanceof ApiProblem) {
             return $status;
         }
     }
     $metadataMap = $this->getMetadataMap();
     $maxDepth = is_object($collection) && $metadataMap->has($collection) ? $metadataMap->get($collection)->getMaxDepth() : null;
     $payload = $jsonLDCollection->getAttributes();
     $payload = ArrayUtils::merge($payload, $this->fromResource($jsonLDCollection));
     if (isset($payload[$collectionName])) {
         $payload[$collectionName] = ArrayUtils::merge($payload[$collectionName], $this->extractCollection($jsonLDCollection, 0, $maxDepth));
     } else {
         $payload[$collectionName] = $this->extractCollection($jsonLDCollection, 0, $maxDepth);
     }
     if ($collection instanceof Paginator) {
         if (!empty($payload['view'])) {
             $payload['view']['@type'] = 'PartialCollectionView';
             $payload['view']['itemsPerPage'] = isset($payload['itemsPerPage']) ? $payload['itemsPerPage'] : $jsonLDCollection->getPageSize();
         }
         $payload['totalItems'] = isset($payload['totalItems']) ? $payload['totalItems'] : (int) $collection->getTotalItemCount();
     } elseif (is_array($collection) || $collection instanceof Countable) {
         $payload['totalItems'] = isset($payload['totalItems']) ? $payload['totalItems'] : count($collection);
     }
     $payload['@context'] = 'http://www.w3.org/ns/hydra/context.jsonld';
     $payload['@type'] = 'Collection';
     $payload = new ArrayObject($payload);
     $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, ['payload' => $payload, 'collection' => $jsonLDCollection]);
     return (array) $payload;
 }
Example #2
0
 public function testCollectionNameIsMutable()
 {
     $jsonLD = new Collection([], 'item/route');
     $jsonLD->setCollectionName('records');
     $this->assertEquals('records', $jsonLD->getCollectionName());
 }