Exemplo 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]];
 }
Exemplo n.º 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;
 }
 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;
 }
Exemplo n.º 4
0
 /**
  * Creates a property object, given metadata and a resource
  *
  * @param  Metadata $metadata
  * @param  object $object
  * @param  null|string $id
  * @param  null|string $routeIdentifierName
  * @param  string $relation
  * @return Property
  * @throws Exception\RuntimeException
  */
 public function marshalPropertyFromMetadata(Metadata $metadata, $object, $id = null, $routeIdentifierName = null, $relation = 'id')
 {
     $property = new Property($relation);
     if ($metadata->hasUrl()) {
         $property->setUrl($metadata->getUrl());
         return $property;
     }
     if (!$metadata->hasRoute()) {
         throw new Exception\RuntimeException(sprintf('Unable to create a self property for resource of type "%s"; metadata does not contain a route or a url', get_class($object)));
     }
     $params = $metadata->getRouteParams();
     // process any callbacks
     foreach ($params as $key => $param) {
         // bind to the object if supported
         if ($param instanceof Closure && version_compare(PHP_VERSION, '5.4.0') >= 0) {
             $param = $param->bindTo($object);
         }
         // pass the object for callbacks and non-bound closures
         if (is_callable($param)) {
             $params[$key] = call_user_func_array($param, [$object]);
         }
     }
     if ($routeIdentifierName) {
         $params = array_merge($params, [$routeIdentifierName => $id]);
     }
     $property->setRoute($metadata->getRoute(), $params, $metadata->getRouteOptions());
     return $property;
 }
Exemplo n.º 5
0
 public function testIsCompleteReturnsTrueWhenRouteIsSet()
 {
     $property = new Property('describedby');
     $property->setRoute('api/docs');
     $this->assertTrue($property->isComplete());
 }
Exemplo n.º 6
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']);
     }
 }