/** * @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; }
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; }
public function indexAction() { $event = $this->getEvent(); $routeMatch = $event->getRouteMatch(); $route = $this->deriveRouteName($routeMatch->getMatchedRouteName()); $request = $this->getRequest(); $module = $this->params()->fromRoute('name', false); $controller = $this->params()->fromRoute('controller_service_name', false); $inputFilterName = $this->params()->fromRoute('input_filter_name', false); if (!$module || !$this->model->moduleExists($module)) { return new ApiProblemResponse(new ApiProblem(404, 'The module specified does not exist')); } if (!$controller || !$this->model->controllerExists($module, $controller)) { return new ApiProblemResponse(new ApiProblem(404, 'The controller specified does not exist')); } switch ($request->getMethod()) { case $request::METHOD_GET: $result = $this->model->fetch($module, $controller, $inputFilterName); if (false === $result) { return new ApiProblemResponse(new ApiProblem(404, 'The input filter specified does not exist')); } if ($result instanceof InputFilterCollection) { $result = new HalCollection($result); $result->setCollectionName('input_filter'); $result->getLinks()->add(Link::factory(array('rel' => 'self', 'route' => array('name' => $route, 'params' => array('name' => $module, 'controller_service_name' => str_replace('\\', '-', $controller)))))); $result->setEntityRoute($route); break; } $name = $result['input_filter_name']; $result = new HalEntity($result, $name); $this->injectEntitySelfLink($result->getLinks(), $route, $module, $controller, $name); break; case $request::METHOD_POST: if ($inputFilterName) { return new ApiProblemResponse(new ApiProblem(400, 'POST requests are not allowed to individual input filters')); } // Intentionally not breaking, as remainder of logic remains the same as PUT // Intentionally not breaking, as remainder of logic remains the same as PUT case $request::METHOD_PUT: $inputFilter = $this->bodyParams(); $result = $this->model->update($module, $controller, $inputFilter); if (!$result) { return new ApiProblemResponse(new ApiProblem(500, 'There was an unexpected error updating the input filter; please verify the module and controller specified are valid')); } $name = $result['input_filter_name']; $result = new HalEntity($result, $name); $this->injectEntitySelfLink($result->getLinks(), $route, $module, $controller, $name); break; case $request::METHOD_DELETE: if (empty($inputFilterName)) { return new ApiProblemResponse(new ApiProblem(400, 'The input filter name has not been specified')); } $result = $this->model->remove($module, $controller, $inputFilterName); if (!$result) { return new ApiProblemResponse(new ApiProblem(404, 'The input filter specified does not exist')); } return $this->getResponse()->setStatusCode(204); } $e = $this->getEvent(); $e->setParam('ZFContentNegotiationFallback', 'HalJson'); return new ViewModel(array('payload' => $result)); }
/** * @group 14 */ public function testRenderingNonPaginatorCollectionRendersCountOfTotalItems() { $embedded = new Entity((object) ['id' => 'foo', 'name' => 'foo'], 'foo'); $links = $embedded->getLinks(); $self = new Link('self'); $self->setRoute('hostname/users', ['id' => 'foo']); $links->add($self); $collection = new Collection([$embedded]); $collection->setCollectionName('users'); $self = new Link('self'); $self->setRoute('hostname/users'); $collection->getLinks()->add($self); $rendered = $this->plugin->renderCollection($collection); $expectedKeys = ['_links', '_embedded', 'total_items']; $this->assertEquals($expectedKeys, array_keys($rendered)); }
/** * 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 testCollectionNameIsMutable() { $hal = new Collection(array(), 'item/route'); $hal->setCollectionName('records'); $this->assertEquals('records', $hal->getCollectionName()); }
/** * 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)); }