Example #1
0
 public function testPropertiesAreAccessibleFollowingConstruction()
 {
     $jsonLD = new Collection([], 'item/route', ['version' => 1], ['query' => 'format=json']);
     $this->assertEquals([], $jsonLD->getCollection());
     $this->assertEquals('item/route', $jsonLD->getEntityRoute());
     $this->assertEquals(['version' => 1], $jsonLD->getEntityRouteParams());
     $this->assertEquals(['query' => 'format=json'], $jsonLD->getEntityRouteOptions());
 }
Example #2
0
 /**
  * Extract a collection as an array
  *
  * @param  Collection $jsonLDCollection
  * @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 $jsonLDCollection, $depth = 0, $maxDepth = null)
 {
     $collection = [];
     $events = $this->getEventManager();
     $routeIdentifierName = $jsonLDCollection->getRouteIdentifierName();
     $entityRoute = $jsonLDCollection->getEntityRoute();
     $entityRouteParams = $jsonLDCollection->getEntityRouteParams();
     $entityRouteOptions = $jsonLDCollection->getEntityRouteOptions();
     $metadataMap = $this->getMetadataMap();
     $entityMetadata = null;
     foreach ($jsonLDCollection->getCollection() as $entity) {
         $eventParams = new ArrayObject(['collection' => $jsonLDCollection, 'entity' => $entity, 'route' => $entityRoute, 'routeParams' => $entityRouteParams, 'routeOptions' => $entityRouteOptions]);
         $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 PropertyCollectionAwareInterface) {
             $properties = $eventParams['entity']->getProperties();
         } else {
             $properties = new PropertyCollection();
         }
         if (isset($entity['properties']) && $entity['properties'] instanceof PropertyCollection) {
             $properties = $entity['properties'];
         }
         if (!isset($entity['id'])) {
             $idProperty = new Property('id');
             $idProperty->setRoute($eventParams['route'], array_merge($eventParams['routeParams'], [$routeIdentifierName => $id]), $eventParams['routeOptions']);
             $properties->add($idProperty);
         }
         $entity = ArrayUtils::merge($entity, $this->fromPropertyCollection($properties));
         $collection[] = $entity;
     }
     return $collection;
 }