protected function build($key, $value) { $entity = new HalEntity(new GadgetEntity($value), $value->getId()); $entity->getLinks()->add(Link::factory(['rel' => 'self', 'route' => ['name' => 'zource-application.rest.gadget', 'params' => ['gadget_id' => $value->getId()]]])); $entity->getLinks()->add(Link::factory(['rel' => 'gadget-container', 'route' => ['name' => 'zource-application.rest.gadget-container', 'params' => ['gadget_container_id' => $value->getGadgetContainer()->getId()]]])); return $entity; }
private function buildEntity($plugin) { $entity = new HalEntity(new PluginEntity($plugin), $plugin->getId()); $entity->getLinks()->add(Link::factory(['rel' => 'activate', 'route' => ['name' => 'zource-application.rest.plugin', 'params' => ['plugin_id' => $plugin->getId()]]])); $entity->getLinks()->add(Link::factory(['rel' => 'deactivate', 'route' => ['name' => 'zource-application.rest.plugin', 'params' => ['plugin_id' => $plugin->getId()]]])); return $entity; }
/** * @param \ZB\Utils\Entity\IdProviderInterface $idProvider * @param \ZF\Hal\Plugin\Hal $halPlugin * @return \ZF\Hal\Entity */ public function convertToHalEntity(IdProviderInterface $idProvider, HalPlugin $halPlugin) { $halMetadata = $halPlugin->getMetadataMap()->get($idProvider); $halEntity = new HalEntity($idProvider); $halEntity->getLinks()->add(HalLink::factory(array('rel' => 'self', 'route' => array('name' => $halMetadata->getRoute(), 'options' => $halMetadata->getRouteOptions(), 'params' => array_merge($halMetadata->getRouteParams(), array($halMetadata->getRouteIdentifierName() => $idProvider->getId())))))); return $halEntity; }
public function authenticationAction() { $request = $this->getRequest(); switch ($request->getMethod()) { case $request::METHOD_GET: $entity = $this->model->fetch(); if (!$entity) { $response = $this->getResponse(); $response->setStatusCode(204); return $response; } break; case $request::METHOD_POST: $entity = $this->model->create($this->bodyParams()); $response = $this->getResponse(); $response->setStatusCode(201); $response->getHeaders()->addHeaderLine('Location', $this->plugin('hal')->createLink($this->getRouteForEntity($entity))); break; case $request::METHOD_PATCH: $entity = $this->model->update($this->bodyParams()); break; case $request::METHOD_DELETE: if ($this->model->remove()) { return $this->getResponse()->setStatusCode(204); } return new ApiProblemResponse(new ApiProblem(404, 'No authentication configuration found')); default: return new ApiProblemResponse(new ApiProblem(405, 'Only the methods GET, POST, PATCH, and DELETE are allowed for this URI')); } $halEntity = new Entity($entity, null); $halEntity->getLinks()->add(Link::factory(array('rel' => 'self', 'route' => $this->getRouteForEntity($entity)))); return new ViewModel(array('payload' => $halEntity)); }
public function onRenderEntity(EventManagerEvent $e) { $entity = $e->getParam('entity'); if (!$entity->entity instanceof Pages) { return; } $entity->getLinks()->add(HalLink::factory(array('rel' => 'images', 'route' => array('name' => 'application.rest.images', 'params' => ['page_id' => $entity->entity->getUuid()])))); }
public function onRenderEntity($e) { $entity = $e->getParam('entity'); /** * Attach a link to the revision entity data */ if (!$entity->entity instanceof RevisionEntity) { // do nothing return; } $entity->getLinks()->add(\ZF\Hal\Link\Link::factory(array('rel' => 'revisionEntityData', 'route' => array('name' => 'zf.doctrine.audit.api.rpc.revision-entity-value', 'params' => array('revision_entity_id' => $entity->entity->getId()))))); }
public function testLinkCollectionWithTwoLinksForSameRelationShouldReturnArrayWithOneKeyAggregatingLinks() { $linkCollection = new LinkCollection(); $linkCollection->add(Link::factory(['rel' => 'foo', 'url' => 'http://example.com/foo'])); $linkCollection->add(Link::factory(['rel' => 'foo', 'url' => 'http://example.com/bar'])); $linkCollection->add(Link::factory(['rel' => 'baz', 'url' => 'http://example.com/baz'])); $result = $this->linkCollectionExtractor->extract($linkCollection); $this->assertInternalType('array', $result); $this->assertCount(2, $result); $this->assertInternalType('array', $result['foo']); $this->assertCount(2, $result['foo']); }
/** * @group 95 */ public function testPassingFalseReuseParamsOptionShouldOmitMatchedParametersInGeneratedLink() { $serverUrlHelper = $this->getMock('Zend\\View\\Helper\\ServerUrl'); $urlHelper = new UrlHelper(); $linkExtractor = new LinkExtractor($serverUrlHelper, $urlHelper); $match = $this->matchUrl('/resource/foo', $urlHelper); $this->assertEquals('foo', $match->getParam('id', false)); $link = Link::factory(['rel' => 'resource', 'route' => ['name' => 'hostname/resource', 'options' => ['reuse_matched_params' => false]]]); $result = $linkExtractor->extract($link); $this->assertInternalType('array', $result); $this->assertArrayHasKey('href', $result); $this->assertEquals('http://localhost.localdomain/resource', $result['href']); }
protected function injectThumbnailLinks($entity) { $config = $this->sm->get('Config'); if (empty($config['file-manager']['thumbnail-types'])) { return []; } foreach ($config['file-manager']['thumbnail-types'] as $typeName => $type) { if (strpos($entity['mime_type'], 'image') === 0) { $entity['thumbnail_' . $typeName] = Link::factory(array('rel' => 'thumbnail_' . $typeName, 'route' => ['name' => 'kap-file-manager.rpc.file-thumbnail', 'params' => ['filter' => $type['filter'], 'width' => $type['width'], 'height' => $type['height'], 'file_id' => $entity['id']]])); } else { throw new \Exception("TODO what's thumbnail for {$entity['mime_type']}?"); } } }
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 apiEnableAction() { $request = $this->getRequest(); switch ($request->getMethod()) { case $request::METHOD_PUT: $module = $this->bodyParam('module', false); if (!$module) { return new ApiProblemResponse( new ApiProblem( 422, 'Module parameter not provided', 'https://tools.ietf.org/html/rfc4918', 'Unprocessable Entity' ) ); } $result = $this->moduleModel->updateModule($module); if (!$result) { return new ApiProblemResponse( new ApiProblem(500, 'Unable to Apigilify the module') ); } $metadata = new ModuleEntity($module); $entity = new Entity($metadata, $module); $entity->getLinks()->add(Link::factory(array( 'rel' => 'self', 'route' => array( 'name' => 'zf-apigility/api/module', 'params' => array('module' => $module), ), ))); return new ViewModel(array('payload' => $entity)); default: return new ApiProblemResponse( new ApiProblem(405, 'Only the method PUT is allowed for this URI') ); } }
public function authorizationAction() { $request = $this->getRequest(); $version = $request->getQuery('version', 1); $model = $this->getModel(); switch ($request->getMethod()) { case $request::METHOD_GET: $entity = $model->fetch($version); break; case $request::METHOD_PUT: $entity = $model->update($this->bodyParams(), $version); break; default: return new ApiProblemResponse(new ApiProblem(405, 'Only the methods GET and PUT are allowed for this URI')); } $entity = new Entity($entity, null); $entity->getLinks()->add(Link::factory(array('rel' => 'self', 'route' => array('name' => 'zf-apigility/api/module/authorization', 'params' => array('name' => $this->moduleName), 'options' => array('query' => array('version' => $version)))))); return new ViewModel(array('payload' => $entity)); }
public function authorizationAction() { $request = $this->getRequest(); $version = $request->getQuery('version', 1); $model = $this->getModel(); switch ($request->getMethod()) { case $request::METHOD_GET: $entity = $model->fetch($version); break; case $request::METHOD_PUT: $this->getResponse()->getHeaders()->addHeaderLine('X-Deprecated', 'This service has deprecated the PUT method; please use PATCH'); // intentionally fall through // intentionally fall through case $request::METHOD_PATCH: $entity = $model->update($this->bodyParams(), $version); break; default: return new ApiProblemResponse(new ApiProblem(405, 'Only the methods GET and PUT are allowed for this URI')); } $entity = new Entity($entity, null); $entity->getLinks()->add(Link::factory(['rel' => 'self', 'route' => ['name' => 'zf-apigility/api/module/authorization', 'params' => ['name' => $this->moduleName], 'options' => ['query' => ['version' => $version]]]])); return new ViewModel(['payload' => $entity]); }
/** * Inject service links * * @param string $type "rpc" | "rest" | "authorization" * @param array|\Traversable $services * @param LinkCollection $links * @param null|string $module */ protected function injectLinksForServicesByType($type, $services, LinkCollection $links, $module = null) { $urlHelper = $this->urlHelper; $linkType = $type; if (in_array($type, array('rpc', 'rest'))) { $linkType .= '-service'; } $routeName = sprintf('zf-apigility/api/module/%s', $linkType); $routeParams = array(); $routeOptions = array(); if (null !== $module) { $routeParams['name'] = $module; } $url = call_user_func($urlHelper, $routeName, $routeParams, $routeOptions, false); $url .= '{?version}'; $spec = array('rel' => $type, 'url' => $url, 'props' => array('templated' => true)); $link = Link::factory($spec); $links->add($link); }
/** * Create and return an entity view model * * @param mixed $entity * @return ViewModel */ private function createEntity($entity) { $halEntity = new Entity($entity, 'name'); $halEntity->getLinks()->add(Link::factory(['rel' => 'self', 'route' => ['name' => 'zf-apigility/api/authentication', 'params' => ['authentication_adapter' => $entity['name']]]])); return new ViewModel(['payload' => $halEntity]); }
/** * Inject any links found in the metadata into the resource's link collection * * @param Metadata $metadata * @param LinkCollection $links */ public function marshalMetadataLinks(Metadata $metadata, LinkCollection $links) { foreach ($metadata->getLinks() as $linkData) { $link = Link::factory($linkData); $links->add($link); } }
/** * 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)); }
/** * 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]); }
public function injectEntitySelfLink($links, $route, $module, $controller, $inputFilterName) { $links->add(Link::factory(array('rel' => 'self', 'route' => array('name' => $route, 'params' => array('name' => $module, 'controller_service_name' => $controller, 'input_filter_name' => $inputFilterName))))); }
private function createPaginationLink($relation, Collection $halCollection, $page = null) { $options = ArrayUtils::merge($halCollection->getCollectionRouteOptions(), ['query' => ['page' => $page]]); return Link::factory(['rel' => $relation, 'route' => ['name' => $halCollection->getCollectionRoute(), 'params' => $halCollection->getCollectionRouteParams(), 'options' => $options]]); }
public function testFactoryCanGenerateLinkWithArbitraryProperties() { $rel = 'describedby'; $url = 'http://example.org/api/foo?version=2'; $link = Link::factory(array( 'rel' => $rel, 'url' => $url, 'props' => array( 'version' => 2, 'latest' => true, ) )); $this->assertInstanceOf('ZF\Hal\Link\Link', $link); $this->assertEquals('describedby', $link->getRelation()); $props = $link->getProps(); $this->assertEquals(array( 'version' => 2, 'latest' => true, ), $props); }
/** * @group 79 */ public function testCreateHalEntityDoesNotInjectExistingEntityWithSelfRelationalLinkIfAlreadyPresent() { $entity = ['id' => 1, 'foo' => 'bar']; $halEntity = new HalEntity($entity, 1); $self = Link::factory(['rel' => 'self', 'url' => 'http://example.com/foo/1']); $halEntity->getLinks()->add($self); $r = new ReflectionMethod($this->controller, 'createHalEntity'); $r->setAccessible(true); $result = $r->invoke($this->controller, $halEntity); $this->assertSame($result, $halEntity); $this->assertTrue($result->getLinks()->has('self')); $this->assertSame($self, $result->getLinks()->get('self')); }
public function testFromLinkShouldComposeAnyPropertiesInLink() { $link = Link::factory(array( 'rel' => 'resource', 'url' => 'http://api.example.com/foo?version=2', 'props' => array( 'version' => 2, 'latest' => true, ), )); $result = $this->plugin->fromLink($link); $expected = array( 'href' => 'http://api.example.com/foo?version=2', 'version' => 2, 'latest' => true, ); $this->assertEquals($expected, $result); }
private function buildGadgetEntity($gadget) { $entity = new HalEntity(new GadgetEntity($gadget), $gadget->getId()); $entity->getLinks()->add(Link::factory(['rel' => 'gadget-container', 'route' => ['name' => 'zource-application.rest.gadget-container', 'params' => ['gadget_container_id' => $gadget->getGadgetContainer()->getId()]]])); return $entity; }