public function halObjects()
 {
     $entity = new Entity(['foo' => 'bar'], 'identifier', 'route');
     $link = new Link('self');
     $link->setRoute('resource/route')->setRouteParams(['id' => 'identifier']);
     $entity->getLinks()->add($link);
     $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);
     $halCollection = new Collection($collection);
     $halCollection->setCollectionRoute('foo');
     $halCollection->setPage($currentPage);
     $halCollection->setPageSize(1);
     return $halCollection;
 }
Example #3
0
 /**
  * @param  object $object
  * @param  Metadata $metadata
  * @return Collection
  */
 public function createCollectionFromMetadata($object, Metadata $metadata)
 {
     $halCollection = new Collection($object);
     $halCollection->setCollectionName($metadata->getCollectionName());
     $halCollection->setCollectionRoute($metadata->getRoute());
     $halCollection->setEntityRoute($metadata->getEntityRoute());
     $halCollection->setRouteIdentifierName($metadata->getRouteIdentifierName());
     $halCollection->setEntityIdentifierName($metadata->getEntityIdentifierName());
     $links = $halCollection->getLinks();
     $this->marshalMetadataLinks($metadata, $links);
     $forceSelfLink = $metadata->getForceSelfLink();
     if ($forceSelfLink && !$links->has('self') && ($metadata->hasUrl() || $metadata->hasRoute())) {
         $link = $this->marshalLinkFromMetadata($metadata, $object);
         $links->add($link);
     }
     return $halCollection;
 }
Example #4
0
    public function halObjects()
    {
        $entity = new Entity(array(
            'foo' => 'bar',
        ), 'identifier', 'route');
        $link = new Link('self');
        $link->setRoute('resource/route')->setRouteParams(array('id' => 'identifier'));
        $entity->getLinks()->add($link);

        $collection = new Collection(array($entity));
        $collection->setCollectionRoute('collection/route');
        $collection->setEntityRoute('resource/route');

        return array(
            'entity'     => array($entity),
            'collection' => array($collection),
        );
    }
 public function settingsDashboardAction()
 {
     $authentication = $this->authentication->fetch();
     if ($authentication) {
         $authenticationEntity = $authentication;
         $authentication = new Entity($authentication, null);
         $authentication->getLinks()->add(Link::factory(array('rel' => 'self', 'route' => $this->getRouteForEntity($authenticationEntity))));
     }
     $dbAdapters = new Collection($this->dbAdapters->fetchAll());
     $dbAdapters->setCollectionRoute('zf-apigility/api/db-adapter');
     $contentNegotiation = new Collection($this->contentNegotiation->fetchAll());
     $contentNegotiation->setCollectionRoute('zf-apigility/api/content-negotiation');
     $dashboard = array('authentication' => $authentication, 'content_negotiation' => $contentNegotiation, 'db_adapter' => $dbAdapters);
     $entity = new Entity($dashboard, 'settings-dashboard');
     $links = $entity->getLinks();
     $links->add(Link::factory(array('rel' => 'self', 'route' => array('name' => 'zf-apigility/api/settings-dashboard'))));
     return new ViewModel(array('payload' => $entity));
 }
 public function testAllowsSpecifyingAlternateCallbackForReturningEntityId()
 {
     $this->setUpHelpers();
     $this->helpers->get('Hal')->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 = array('foo' => 'bar');
     $items = array();
     foreach (range(1, 100) as $id) {
         $item = $prototype;
         $item['name'] = $id;
         $items[] = $item;
     }
     $collection = new Collection($items);
     $collection->setCollectionRoute('resource');
     $collection->setEntityRoute('resource');
     $links = $collection->getLinks();
     $self = new Link('self');
     $self->setRoute('resource');
     $links->add($self);
     $model = new HalJsonModel(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 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->setEntityRoute('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;
 }
Example #8
0
 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');
     $links = $collection->getLinks();
     $self = new Link('self');
     $self->setRoute('resource');
     $links->add($self);
     $result = $this->plugin->renderCollection($collection);
     $this->assertInternalType('array', $result, var_export($result, 1));
     $this->assertRelationalLinkEquals('http://localhost.localdomain/resource', 'self', $result);
     $this->assertArrayHasKey('_embedded', $result);
     $this->assertInternalType('array', $result['_embedded']);
     $this->assertArrayHasKey('items', $result['_embedded']);
     $this->assertInternalType('array', $result['_embedded']['items']);
     $this->assertEquals(100, count($result['_embedded']['items']));
     foreach ($result['_embedded']['items'] as $key => $item) {
         $id = $key + 1;
         $this->assertRelationalLinkEquals('http://localhost.localdomain/resource/' . $id, 'self', $item);
         $this->assertArrayHasKey('name', $item, var_export($item, 1));
         $this->assertEquals($id, $item['name']);
         $this->assertArrayHasKey('foo', $item);
         $this->assertEquals('bar', $item['foo']);
     }
 }
 /**
  * Inject the input filters collection, if any, as an embedded collection
  *
  * @param RestServiceEntity $service
  */
 protected function injectInputFilters(RestServiceEntity $service)
 {
     $inputFilters = $this->inputFilterModel->fetch($this->moduleName, $service->controllerServiceName);
     if (!$inputFilters instanceof InputFilterCollection || !count($inputFilters)) {
         return;
     }
     $collection = [];
     $parentName = str_replace('\\', '-', $service->controllerServiceName);
     foreach ($inputFilters as $inputFilter) {
         $inputFilter['input_filter_name'] = str_replace('\\', '-', $inputFilter['input_filter_name']);
         $entity = new HalEntity($inputFilter, $inputFilter['input_filter_name']);
         $links = $entity->getLinks();
         $links->add(Link::factory(['rel' => 'self', 'route' => ['name' => 'zf-apigility/api/module/rest-service/input-filter', 'params' => ['name' => $this->moduleName, 'controller_service_name' => $parentName, 'input_filter_name' => $inputFilter['input_filter_name']]]]));
         $collection[] = $entity;
     }
     $collection = new HalCollection($collection);
     $collection->setCollectionName('input_filter');
     $collection->setCollectionRoute('zf-apigility/module/rest-service/input-filter');
     $collection->setCollectionRouteParams(['name' => $service->module, 'controller_service_name' => $service->controllerServiceName]);
     $service->exchangeArray(['input_filters' => $collection]);
 }
Example #10
0
    /**
     * @group 14
     */
    public function testRenderingPaginatorCollectionRendersPaginationAttributes()
    {
        $set = array();
        for ($id = 1; $id <= 100; $id += 1) {
            $entity = new Entity((object) array('id' => $id, 'name' => 'foo'), 'foo');
            $links = $entity->getLinks();
            $self = new Link('self');
            $self->setRoute('hostname/users', array('id' => $id));
            $links->add($self);
            $set[] = $entity;
        }

        $paginator  = new Paginator(new ArrayPaginator($set));
        $collection = new Collection($paginator);
        $collection->setCollectionName('users');
        $collection->setCollectionRoute('hostname/users');
        $collection->setPage(3);
        $collection->setPageSize(10);

        $rendered = $this->plugin->renderCollection($collection);
        $expected = array(
            '_links',
            '_embedded',
            'page_count',
            'page_size',
            'total_items',
            'page',
        );
        $this->assertEquals($expected, array_keys($rendered));
        $this->assertEquals(100, $rendered['total_items']);
        $this->assertEquals(3, $rendered['page']);
        $this->assertEquals(10, $rendered['page_count']);
        $this->assertEquals(10, $rendered['page_size']);
        return $rendered;
    }
 /**
  * Inject the input filters collection, if any, as an embedded collection
  *
  * @param DoctrineRpcServiceEntity $service
  */
 protected function injectInputFilters(DoctrineRpcServiceEntity $service)
 {
     $inputFilters = $this->inputFilterModel->fetch($this->moduleName, $service->controllerServiceName);
     if (!$inputFilters instanceof InputFilterCollection || !count($inputFilters)) {
         return;
     }
     // @codeCoverageIgnoreStart
     $collection = array();
     foreach ($inputFilters as $inputFilter) {
         $resource = new HalResource($inputFilter, $inputFilter['input_filter_name']);
         $links = $resource->getLinks();
         $links->add(Link::factory(array('rel' => 'self', 'route' => array('name' => 'zf-apigility-admin/api/module/rpc-service/rpc_input_filter', 'params' => array('name' => $this->moduleName, 'controller_service_name' => $service->controllerServiceName, 'input_filter_name' => $inputFilter['input_filter_name'])))));
         $collection[] = $resource;
     }
     $collection = new HalCollection($collection);
     $collection->setCollectionName('input_filter');
     $collection->setCollectionRoute('zf-apigility-admin/module/rpc-service/inputfilter');
     $collection->setCollectionRouteParams(array('name' => $this->moduleName, 'controller_service_name' => $service->controllerServiceName));
     $service->exchangeArray(array('input_filters' => $collection));
 }