/** * @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; }
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; }
/** * 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; }
public function testPropertyCollectionMayBeInjected() { $jsonLD = new Collection([], 'item/route'); $properties = new PropertyCollection(); $jsonLD->setProperties($properties); $this->assertSame($properties, $jsonLD->getProperties()); }
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']); } }