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]]; }
/** * @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; }
/** * @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; }
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']); } }