Ejemplo n.º 1
0
 public function jsonLDObjects()
 {
     $entity = new Entity(['foo' => 'bar'], 'identifier', 'route');
     $property = new Property('self');
     $property->setRoute('resource/route')->setRouteParams(['id' => 'identifier']);
     $entity->getProperties()->add($property);
     $collection = new Collection([$entity]);
     $collection->setCollectionRoute('collection/route');
     $collection->setEntityRoute('resource/route');
     return ['entity' => [$entity], 'collection' => [$collection]];
 }
Ejemplo n.º 2
0
 /**
  * @param  int $pages
  * @param  int $currentPage
  * @return Collection
  */
 private function getHalCollection($pages, $currentPage)
 {
     $items = [];
     for ($i = 0; $i < $pages; $i++) {
         $items[] = [];
     }
     $adapter = new ArrayAdapter($items);
     $collection = new Paginator($adapter);
     $jsonLDCollection = new Collection($collection);
     $jsonLDCollection->setCollectionRoute('foo');
     $jsonLDCollection->setPage($currentPage);
     $jsonLDCollection->setPageSize(1);
     return $jsonLDCollection;
 }
Ejemplo n.º 3
0
 /**
  * @param  object $object
  * @param  Metadata $metadata
  * @return Collection
  */
 public function createCollectionFromMetadata($object, Metadata $metadata)
 {
     $jsonLDCollection = new Collection($object);
     $jsonLDCollection->setCollectionName($metadata->getCollectionName());
     $jsonLDCollection->setCollectionRoute($metadata->getRoute());
     $jsonLDCollection->setEntityRoute($metadata->getEntityRoute());
     $jsonLDCollection->setRouteIdentifierName($metadata->getRouteIdentifierName());
     $jsonLDCollection->setEntityIdentifierName($metadata->getEntityIdentifierName());
     $properties = $jsonLDCollection->getProperties();
     $this->marshalMetadataProperties($metadata, $properties);
     $forceFullUriID = $metadata->getForceFullUriID();
     if ($forceFullUriID && !$properties->has('id') && ($metadata->hasUrl() || $metadata->hasRoute())) {
         $property = $this->marshalPropertyFromMetadata($metadata, $object);
         $properties->add($property);
     }
     return $jsonLDCollection;
 }
Ejemplo n.º 4
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;
 }
 public function setUpChildCollection()
 {
     $children = [['luke', 'Luke Skywalker'], ['leia', 'Leia Organa']];
     $collection = [];
     foreach ($children as $info) {
         $collection[] = call_user_func_array([$this, 'setUpChildEntity'], $info);
     }
     $collection = new Collection($collection);
     $collection->setCollectionRoute('parent/child');
     $collection->setEntityRoute('parent/child');
     $collection->setPage(1);
     $collection->setPageSize(10);
     $collection->setCollectionName('child');
     $property = new Property('id');
     $property->setRoute('parent/child');
     $collection->getProperties()->add($property);
     return $collection;
 }
Ejemplo n.º 6
0
 /**
  * Unlike name suggests this prepare a JsonLD collection
  * with the metadata for the current instance.
  *
  * Chanign the name would meen that would have to override almost every method in this class
  *
  *
  * @param JsonLDCollection $collection
  * @return JsonLDCollection|ApiProblem
  */
 protected function prepareJsonLDCollection(JsonLDCollection $collection)
 {
     if (!$collection->getProperties()->has('id')) {
         $plugin = $this->plugin('JsonLD');
         $plugin->injectIDProperty($collection, $this->route);
     }
     $collection->setCollectionRoute($this->route);
     $collection->setRouteIdentifierName($this->getRouteIdentifierName());
     $collection->setEntityRoute($this->route);
     $collection->setCollectionName($this->collectionName);
     $collection->setPageSize($this->getPageSize());
     try {
         $collection->setPage($this->getRequest()->getQuery('page', 1));
     } catch (JsonLDInvalidArgumentException $e) {
         return new ApiProblem(400, $e->getMessage());
     }
     return $collection;
 }
Ejemplo n.º 7
0
 public function testAllowsSettingAdditionalEntityProperties()
 {
     $properties = new PropertyCollection();
     $properties->add(new Property('describedby'));
     $properties->add(new Property('orders'));
     $jsonLD = new Collection([], 'item/route');
     $jsonLD->setEntityProperties($properties);
     $this->assertSame($properties, $jsonLD->getEntityProperties());
 }
Ejemplo n.º 8
0
 public function testAllowsSpecifyingAlternateCallbackForReturningEntityId()
 {
     $this->plugin->getEventManager()->attach('getIdFromEntity', function ($e) {
         $entity = $e->getParam('entity');
         if (!is_array($entity)) {
             return false;
         }
         if (array_key_exists('name', $entity)) {
             return $entity['name'];
         }
         return false;
     }, 10);
     $prototype = ['foo' => 'bar'];
     $items = [];
     foreach (range(1, 100) as $id) {
         $item = $prototype;
         $item['name'] = $id;
         $items[] = $item;
     }
     $collection = new Collection($items);
     $collection->setCollectionRoute('resource');
     $collection->setEntityRoute('resource');
     $properties = $collection->getProperties();
     $idProperty = new Property('id');
     $idProperty->setRoute('resource');
     $properties->add($idProperty);
     $result = $this->plugin->renderCollection($collection);
     $this->assertInternalType('array', $result, var_export($result, 1));
     $this->assertPropertyEquals('http://localhost.localdomain/resource', 'id', $result);
     $this->assertArrayHasKey('member', $result);
     $this->assertInternalType('array', $result['member']);
     $this->assertEquals(100, count($result['member']));
     foreach ($result['member'] as $key => $item) {
         $id = $key + 1;
         $this->assertPropertyEquals('http://localhost.localdomain/resource/' . $id, 'id', $item);
         $this->assertArrayHasKey('name', $item, var_export($item, 1));
         $this->assertEquals($id, $item['name']);
         $this->assertArrayHasKey('foo', $item);
         $this->assertEquals('bar', $item['foo']);
     }
 }