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));
 }
 /**
  * Add a link
  *
  * @param  Link $link
  * @param  bool $overwrite
  * @return self
  */
 public function add(Link $link, $overwrite = false)
 {
     $relation = $link->getRelation();
     if (!isset($this->links[$relation]) || $overwrite) {
         $this->links[$relation] = $link;
         return $this;
     }
     if ($this->links[$relation] instanceof Link) {
         $this->links[$relation] = array($this->links[$relation]);
     }
     if (!is_array($this->links[$relation])) {
         throw new Exception\DomainException(sprintf('%s::$links should be either a %s\\Link or an array; however, it is a "%s"', __CLASS__, __NAMESPACE__, is_object($this->links[$relation]) ? get_class($this->links[$relation]) : gettype($this->links[$relation])));
     }
     $this->links[$relation][] = $link;
     return $this;
 }
Exemplo n.º 3
0
 /**
  * @group 95
  */
 public function testPassingFalseReuseParamsOptionShouldOmitMatchedParametersInGeneratedLink()
 {
     $matches = $this->matchUrl('/resource/foo');
     $this->assertEquals('foo', $matches->getParam('id', false));
     $link = Link::factory(array('rel' => 'resource', 'route' => array('name' => 'hostname/resource', 'options' => array('reuse_matched_params' => false))));
     $result = $this->plugin->fromLink($link);
     $expected = array('href' => 'http://localhost.localdomain/resource');
     $this->assertEquals($expected, $result);
 }
Exemplo n.º 4
0
 /**
  * @group 79
  */
 public function testFactoryCanGenerateLinkWithRouteInformation()
 {
     $rel = 'describedby';
     $route = 'api/docs';
     $params = array('version' => '1.1');
     $options = array('query' => 'version=1.1');
     $link = Link::factory(array('rel' => $rel, 'route' => array('name' => $route, 'params' => $params, 'options' => $options)));
     $this->assertInstanceOf('PhlyRestfully\\Link', $link);
     $this->assertEquals('describedby', $link->getRelation());
     $this->assertEquals($route, $link->getRoute());
     $this->assertEquals($params, $link->getRouteParams());
     $this->assertEquals($options, $link->getRouteOptions());
 }
 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;
 }
Exemplo n.º 6
0
 /**
  * Inject any links found in the metadata into the resource's link collection
  *
  * @param  Metadata $metadata
  * @param  LinkCollection $links
  */
 protected function marshalMetadataLinks(Metadata $metadata, LinkCollection $links)
 {
     foreach ($metadata->getLinks() as $linkData) {
         $link = Link::factory($linkData);
         $links->add($link);
     }
 }
 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);
     }
 }