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));
 }
Пример #2
0
 /**
  * @param  object $object
  * @param  Metadata $metadata
  * @return HalCollection
  */
 public function createCollectionFromMetadata($object, Metadata $metadata)
 {
     $collection = new HalCollection($object);
     $collection->setCollectionRoute($metadata->getRoute());
     $collection->setResourceRoute($metadata->getResourceRoute());
     $collection->setIdentifierName($metadata->getIdentifierName());
     $this->marshalMetadataLinks($metadata, $collection->getLinks());
     return $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;
 }
 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);
     }
 }