Add a link to the document
public addLink ( string $rel, |
||
$rel | string | The relation of the link to the document. See RFC 5988 http://tools.ietf.org/html/rfc5988#section-6.2.2 A document MUST always have a "self" link. |
$link | The actual link object | |
$overwrite | boolean | When false and a link of $rel relation exists, an array of links is created. Otherwise the existing link is overwriten with the new one |
리턴 | boolean | True if the link was added to the collection |
/** * Creates a \FOF30\Hal\Document using the provided data * * @param mixed|array $data The data to put in the document * @param DataModel $model The model of this view * * @return \FOF30\Hal\Document A HAL-enabled document */ protected function _createDocumentWithHypermedia($data, $model = null) { // Create a new HAL document if (is_array($data)) { $count = count($data); } else { $count = null; } if ($count == 1) { reset($data); $document = new Document(end($data)); } else { $document = new Document($data); } // Create a self link $uri = (string) \JUri::getInstance(); $uri = $this->_removeURIBase($uri); $uri = \JRoute::_($uri); $document->addLink('self', new Link($uri)); // Create relative links in a record list context if (is_array($data) && $model instanceof DataModel) { if (!isset($this->total)) { $this->total = $model->count(); } if (!isset($this->limitStart)) { $this->limitStart = $model->getState('limitstart', 0); } if (!isset($this->limit)) { $this->limit = $model->getState('limit', 0); } $pagination = new \JPagination($this->total, $this->limitStart, $this->limit); if ($pagination->pagesTotal > 1) { // Try to guess URL parameters and create a prototype URL // NOTE: You are better off specialising this method $protoUri = $this->_getPrototypeURIForPagination(); // The "first" link $uri = clone $protoUri; $uri->setVar('limitstart', 0); $uri = \JRoute::_($uri); $document->addLink('first', new Link($uri)); // Do we need a "prev" link? if ($pagination->pagesCurrent > 1) { $prevPage = $pagination->pagesCurrent - 1; $limitstart = ($prevPage - 1) * $pagination->limit; $uri = clone $protoUri; $uri->setVar('limitstart', $limitstart); $uri = \JRoute::_($uri); $document->addLink('prev', new Link($uri)); } // Do we need a "next" link? if ($pagination->pagesCurrent < $pagination->pagesTotal) { $nextPage = $pagination->pagesCurrent + 1; $limitstart = ($nextPage - 1) * $pagination->limit; $uri = clone $protoUri; $uri->setVar('limitstart', $limitstart); $uri = \JRoute::_($uri); $document->addLink('next', new Link($uri)); } // The "last" link? $lastPage = $pagination->pagesTotal; $limitstart = ($lastPage - 1) * $pagination->limit; $uri = clone $protoUri; $uri->setVar('limitstart', $limitstart); $uri = \JRoute::_($uri); $document->addLink('last', new Link($uri)); } } return $document; }
/** * @covers FOF30\Hal\Document::getLinks */ public function testGetLinks() { $data = array('test1' => 'one', 'test2' => 'two', 'testArray' => array('testUno' => 'uno', 'testDue' => 'Due')); $document = new Document($data); $myLink = new Link('http://www.example.com/foo.json', false, 'test', null, 'A test link'); $document->addLink('foo', $myLink); // Make sure trying to add links with replace=false adds, doesn't replace, links $myOtherLink = new Link('http://www.example.com/bar.json', false, 'test', null, 'Another test link'); $document->addLink('foo', $myOtherLink, false); $extraData = array('newData' => 'something'); $document->addData($extraData, false); $allLinks = $document->getLinks(); $this->assertInternalType('array', $allLinks, 'Line: ' . __LINE__ . '.'); $this->assertArrayHasKey('foo', $allLinks, 'Line: ' . __LINE__ . '.'); $links = $document->getLinks('foo'); $this->assertInternalType('array', $links, 'Line: ' . __LINE__ . '.'); $this->assertEquals($links, $allLinks['foo'], 'Line: ' . __LINE__ . '.'); $this->assertCount(2, $links, 'Line: ' . __LINE__ . '.'); $this->assertTrue($links[0] instanceof Link, 'Line: ' . __LINE__ . '.'); $this->assertEquals('http://www.example.com/foo.json', $links[0]->href, 'Line: ' . __LINE__ . '.'); $this->assertTrue($links[1] instanceof Link, 'Line: ' . __LINE__ . '.'); $this->assertEquals('http://www.example.com/bar.json', $links[1]->href, 'Line: ' . __LINE__ . '.'); }