public function indexAction(Application $app, Request $request) { $memosHal = new Hal($request->getRequestUri(), array('welcome' => "Memos Api", 'hint_1' => "The API talk only json", 'hint_2' => "")); $memosHal->addLink('curies', $request->getRequestUri() . 'rels/{rel}', array('name' => 'm', 'templated' => true)); $memosHal->addLink('m:memos', '/api/memos'); return new Response($memosHal->asJson(), 200, array("Content-Type" => $app['request']->getMimeType('json'))); }
public function get() { $hal = new Hal('/articles'); $hal->addLink('search', '/articles/{order_id}'); foreach ($this->data as $id => $data) { $hal->addResource('article', $this->getOne($id)); } return $hal; }
public function execute(Request $request, Response $response, callable $next = null) { $pathParams = $request->getAttribute('pathParams'); $queryParams = $request->getQueryParams(); try { \Assert\Assertion::keyExists($pathParams, 'name'); $collection = $pathParams['name']; $limit = $queryParams['limit']; $page = $queryParams['page']; $columns = $queryParams['columns']; $groups = $queryParams['groups']; $aggregates = $queryParams['aggregates']; $orders = $this->orders(isset($queryParams['orders']) ? $queryParams['orders'] : array()); $wheres = $this->wheres(isset($queryParams['filters']) ? $queryParams['filters'] : array()); $table = $this->boot()->db->table($collection); $paginator = $table->getPaginator($wheres, $orders, $limit, $page, $columns, $groups, $aggregates); $pages = (array) $paginator->getPages(); $pages['requesttime'] = time(); $pages['sql'] = $table->getSql(); $query = http_build_query(['limit' => $pages['itemCountPerPage'], 'page' => $pages['current'], 'columns' => $queryParams['columns']]); $hal = new Hal("/collection/{$collection}?{$query}", $pages); $query = http_build_query(['limit' => $pages['itemCountPerPage'], 'page' => $pages['next'], 'columns' => $queryParams['columns']]); $hal->addLink('next', "/collection/{$collection}?{$query}"); $query = http_build_query(['limit' => $pages['itemCountPerPage'], 'page' => $pages['previous'], 'columns' => $queryParams['columns']]); $hal->addLink('previous', "/collection/{$collection}?{$query}"); $query = http_build_query(['limit' => $pages['itemCountPerPage'], 'page' => $pages['first'], 'columns' => $queryParams['columns']]); $hal->addLink('first', "/collection/{$collection}?{$query}"); $query = http_build_query(['limit' => $pages['itemCountPerPage'], 'page' => $pages['last'], 'columns' => $queryParams['columns']]); $hal->addLink('last', "/collection/{$collection}?{$query}"); foreach ($paginator as $item) { $resource = new Hal("/collection/{$collection}/{$item->id}", $item->toArray()); $hal->addResource('items', $resource); } } catch (\Exception $ex) { $problem = new \Crell\ApiProblem\ApiProblem($ex->getMessage(), $request->getUri()->getPath()); $problem->setDetail($ex->getTraceAsString())->setStatus($ex->getCode()); return $response->withStatus(500)->withBody($this->toStream($problem->asJson())); } $response = $response->withHeader('Content-Type', 'application/json')->withBody($this->toStream($hal->asJson())); if ($next) { $response = $next($request, $response); } return $response; }
public function getMessage($message) { // Make the HAL object. $self = $url = $this->app['url_generator']->generate('messages.view', ['message' => $message['id']]); $hal = new Hal($self); // Add a link to the author. $author_id = $this->app['users.repository']->findByUsername($message['author'])['id']; $author = $url = $this->app['url_generator']->generate('users.view', ['user' => $author_id]); $hal->addLink('author', $author); // Add a link to the parent, if any. if ($message['parent']) { $up = $url = $this->app['url_generator']->generate('messages.view', ['message' => $message['parent']]); $hal->addLink('up', $up); } // Strip the now-irrelevant data from the object and send it. unset($message['id'], $message['parent'], $message['author']); $hal->setData($message); return $hal; }
/** * @return \Illuminate\Http\JsonResponse */ public function index() { $input = \Input::get('format', 'json'); $array = []; $result = $this->recipe->getRecipesFromCategory(); if ($result) { foreach ($result as $row) { $params = ['id' => $row->recipe_id, 'title' => $row->title, 'category' => ['name' => $row->name]]; $array[] = $params; if ($input == 'hal') { $this->hal->addLink('recipes', route('home.recipe', ['one' => $row->recipe_id]), $params); } } } if ($input == 'hal') { $this->hal->setUri('self'); $this->hal->addLink('self', route('home.index')); $array = $this->hal; } return $this->render($array, $input); }
/** * This function adds the given $links to the $hal object. * * @param hal\Hal $hal The Hal object to add the links to * @param array $links The links to add */ private function _addAdditionalLinks(hal\Hal $hal, array $links) { foreach ($links as $rel => $uri) { $hal->addLink($this->_shortName . ':' . $rel, $uri); } }
public function testResourceWithNullSelfLinkRendersLinksInJson() { $x = new Hal(null); $x->addLink('testrel', 'http://test'); $data = json_decode($x->asJson()); $this->assertEquals('http://test', $data->_links->testrel->href); }
/** * @param array $body * @param array $links * @param Hal $hal * * @internal param Uri $uri */ private function getHalLink(array $body, array $links, Hal $hal) { foreach ($links as $link) { if (!$link instanceof Link) { continue; } $uri = uri_template($link->href, $body); $reverseUri = $this->getReverseMatchedLink($uri); $hal->addLink($link->rel, $reverseUri); } }
<?php header("Content-Type:application/hal+json"); include 'blank.php'; include 'country_codes.php'; //ADD your complete server root path here require_once '<your site root directory path>/sites/all/libraries/vendor/autoload.php'; use Nocarrier\Hal; $count = sizeof($array['values']); $hal = new \Nocarrier\Hal('/sites/default/ext/org.civicrm.osdi/api/v3/People/index.php', ['per_page' => $count, 'page' => 1, 'total_records' => $count]); foreach ($array['values'] as $key => $value) { $i = $array['values'][$key]['id']; $resource = new \Nocarrier\Hal('/People?contact_id=' . $array['values'][$key]['id'], array('given_name' => $array['values'][$key]['given_name'], 'family_name' => $array['values'][$key]['family_name'], 'email_addresses' => array(array('primary' => true, 'address' => $array['values'][$key]['email'])), 'identifiers' => array('civi_crm:' . $i), 'id' => $array['values'][$key]['id'], 'created_date' => date("c", strtotime($array2['values'][$i]['created_date'])), 'modified_date' => date("c", strtotime($array['values'][$i]['modified_date'])), 'custom_fields' => array(), 'postal_addresses' => array(array('address_lines' => array(array($array['values'][$key]['postal_addresses'])), 'locality' => $array['values'][$key]['city'], 'region' => $array['values'][$key]['state_province_name'], 'postal_code' => $array['values'][$key]['postal_code'], 'country' => array_search($array['values'][$key]['country'], $countrycodes), 'primary' => filter_var($array3['values'][$key]['is_primary'], FILTER_VALIDATE_BOOLEAN))), 'phone_numbers' => array(array('number' => $array['values'][$key]['number'])))); $resource->addLink('self', '/sites/default/ext/org.civicrm.osdi/api/v3/People/person.php' . '?id=' . $i); $hal->addResource('osdi-people', $resource); } $hal->addLink('next', '/sites/default/ext/org.civicrm.osdi/api/v3/People' . '?page=' . ($page + 1)); $hal->addLink('previous', '/sites/default/ext/org.civicrm.osdi/api/v3/People' . '?page=' . ($page - 1)); echo $hal->asJson();
public function testAddArrayOfLinksInXml() { $hal = new Hal('/'); $hal->addLink('members', '/member/1', 'Member 1'); $hal->addLink('members', '/member/2', 'Member 2'); $result = new \SimpleXmlElement($hal->asXml()); $this->assertEquals('members', $result->link[0]->attributes()->rel); $this->assertEquals('members', $result->link[1]->attributes()->rel); $this->assertEquals('/member/1', $result->link[0]->attributes()->href); $this->assertEquals('/member/2', $result->link[1]->attributes()->href); $this->assertEquals('Member 1', $result->link[0]->attributes()->title); $this->assertEquals('Member 2', $result->link[1]->attributes()->title); }