/** * @group 79 */ public function testRenderCollectionTriggersEvent() { $collection = new HalCollection(array((object) array('id' => 'foo', 'name' => 'foo'), (object) array('id' => 'bar', 'name' => 'bar'), (object) array('id' => 'baz', 'name' => 'baz')), 'hostname/contacts'); $self = new Link('self'); $self->setRoute('hostname/contacts'); $collection->getLinks()->add($self); $collection->setCollectionName('resources'); $this->plugin->getEventManager()->attach('renderCollection', function ($e) { $collection = $e->getParam('collection'); $collection->setAttributes(array('injected' => true)); }); $rendered = $this->plugin->renderCollection($collection); $this->assertArrayHasKey('injected', $rendered); $this->assertTrue($rendered['injected']); }
public function halObjects() { $resource = new HalResource(array('foo' => 'bar'), 'identifier', 'route'); $link = new Link('self'); $link->setRoute('resource/route')->setRouteParams(array('id' => 'identifier')); $resource->getLinks()->add($link); $collection = new HalCollection(array($resource)); $collection->setCollectionRoute('collection/route'); $collection->setResourceRoute('resource/route'); return array('resource' => array($resource), 'collection' => array($collection)); }
public function setUpChildCollection() { $children = array(array('luke', 'Luke Skywalker'), array('leia', 'Leia Organa')); $this->collection = array(); foreach ($children as $info) { $collection[] = call_user_func_array(array($this, 'setUpChildResource'), $info); } $collection = new HalCollection($this->collection); $collection->setCollectionRoute('parent/child'); $collection->setResourceRoute('parent/child'); $collection->setPage(1); $collection->setPageSize(10); $collection->setCollectionName('child'); $link = new Link('self'); $link->setRoute('parent/child'); $collection->getLinks()->add($link); return $collection; }
/** * Generate HAL links for a paginated collection * * @param HalCollection $halCollection * @return array */ protected function injectPaginationLinks(HalCollection $halCollection) { $collection = $halCollection->collection; $page = $halCollection->page; $pageSize = $halCollection->pageSize; $route = $halCollection->collectionRoute; $params = $halCollection->collectionRouteParams; $options = $halCollection->collectionRouteOptions; $collection->setItemCountPerPage($pageSize); $collection->setCurrentPageNumber($page); $count = count($collection); if (!$count) { return true; } if ($page < 1 || $page > $count) { return new ApiProblem(409, 'Invalid page provided'); } $links = $halCollection->getLinks(); $next = $page < $count ? $page + 1 : false; $prev = $page > 1 ? $page - 1 : false; // self link $link = new Link('self'); $link->setRoute($route); $link->setRouteParams($params); $link->setRouteOptions(ArrayUtils::merge($options, array('query' => array('page' => $page)))); $links->add($link, true); // first link $link = new Link('first'); $link->setRoute($route); $link->setRouteParams($params); $link->setRouteOptions($options); $links->add($link); // last link $link = new Link('last'); $link->setRoute($route); $link->setRouteParams($params); $link->setRouteOptions(ArrayUtils::merge($options, array('query' => array('page' => $count)))); $links->add($link); // prev link if ($prev) { $link = new Link('prev'); $link->setRoute($route); $link->setRouteParams($params); $link->setRouteOptions(ArrayUtils::merge($options, array('query' => array('page' => $prev)))); $links->add($link); } // next link if ($next) { $link = new Link('next'); $link->setRoute($route); $link->setRouteParams($params); $link->setRouteOptions(ArrayUtils::merge($options, array('query' => array('page' => $next)))); $links->add($link); } return true; }
public function testAllowsSpecifyingAlternateCallbackForReturningResourceId() { $this->setUpHelpers(); $this->helpers->get('HalLinks')->getEventManager()->attach('getIdFromResource', function ($e) { $resource = $e->getParam('resource'); if (!is_array($resource)) { return false; } if (array_key_exists('name', $resource)) { return $resource['name']; } return false; }, 10); $prototype = array('foo' => 'bar'); $items = array(); foreach (range(1, 100) as $id) { $item = $prototype; $item['name'] = $id; $items[] = $item; } $collection = new HalCollection($items); $collection->setCollectionRoute('resource'); $collection->setResourceRoute('resource'); $links = $collection->getLinks(); $self = new Link('self'); $self->setRoute('resource'); $links->add($self); $model = new RestfulJsonModel(array('payload' => $collection)); $test = $this->renderer->render($model); $test = json_decode($test); $this->assertInstanceof('stdClass', $test, var_export($test, 1)); $this->assertRelationalLinkEquals('http://localhost.localdomain/resource', 'self', $test); $this->assertObjectHasAttribute('_embedded', $test); $this->assertInstanceof('stdClass', $test->_embedded); $this->assertObjectHasAttribute('items', $test->_embedded); $this->assertInternalType('array', $test->_embedded->items); $this->assertEquals(100, count($test->_embedded->items)); foreach ($test->_embedded->items as $key => $item) { $id = $key + 1; $this->assertRelationalLinkEquals('http://localhost.localdomain/resource/' . $id, 'self', $item); $this->assertObjectHasAttribute('name', $item, var_export($item, 1)); $this->assertEquals($id, $item->name); $this->assertObjectHasAttribute('foo', $item); $this->assertEquals('bar', $item->foo); } }
public function testAllowsSettingAdditionalResourceLinks() { $links = new LinkCollection(); $links->add(new Link('describedby')); $links->add(new Link('orders')); $hal = new HalCollection(array(), 'item/route'); $hal->setResourceLinks($links); $this->assertSame($links, $hal->getResourceLinks()); $this->assertSame($links, $hal->resourceLinks); }