public function execute(Request $request, Response $response, callable $next = null) { $pathParams = $request->getAttribute('pathParams'); $params = $request->getQueryParams(); $body = $request->getParsedBody(); \Assert\Assertion::keyExists($pathParams, 'name'); \Assert\Assertion::keyExists($pathParams, 'id'); $collection = $pathParams['name']; $id = $pathParams['id']; $item = $this->boot()->db->table($collection)->fetchRow("`id` = '{$id}'"); $hal = new Hal("/collection/{$collection}/{$id}", $item->toArray()); $response = $response->withHeader('Content-Type', 'application/json')->withBody($this->toStream($hal->asJson())); if ($next) { $response = $next($request, $response); } return $response; }
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 execute(Request $request, Response $response, callable $next = null) { $pathParams = $request->getAttribute('pathParams'); $params = $request->getQueryParams(); $body = $request->getParsedBody(); $body = array_merge($body, $params); \Assert\Assertion::keyExists($pathParams, 'name'); \Assert\Assertion::keyExists($pathParams, 'id'); $collection = $pathParams['name']; $id = $pathParams['id']; $this->boot()->db->table($collection)->update(array($body['name'] => $body['value'], 'add_date' => date("Y-m-d H:i:s")), "`id` = {$id}"); $item = $this->boot()->db->table($collection)->fetchRow("`id` = {$id}"); $hal = new Hal("/collection/{$collection}/{$id}", $item->toArray()); $response = $response->withBody($this->toStream($hal->asJson())); if ($next) { $response = $next($request, $response); } return $response; }
public function execute(Request $request, Response $response, callable $next = null) { $pathParams = $request->getAttribute('pathParams'); $params = $request->getQueryParams(); $body = $request->getBody(); \Assert\Assertion::keyExists($pathParams, 'name'); \Assert\Assertion::keyExists($pathParams, 'id'); \Assert\Assertion::isJsonString($body); $collection = $pathParams['name']; $id = $pathParams['id']; $data = json_decode($body, true); $this->boot()->db->table($collection)->update($data, "`id` = {$id}"); $item = $this->boot()->db->table($collection)->fetchRow("`id` = {$id}"); $hal = new Hal("/collection/{$collection}/{$id}", $item->toArray()); $response = $response->withBody($this->toStream($hal->asJson())); if ($next) { $response = $next($request, $response); } return $response; }
public function execute(Request $request, Response $response, callable $next = null) { try { $pathParams = $request->getAttribute('pathParams'); \Assert\Assertion::keyExists($pathParams, 'name'); $body = $request->getBody(); \Assert\Assertion::isJsonString($body); $collection = $pathParams['name']; $data = json_decode($body, true); $id = $this->boot()->db->table($collection)->insert($data); $item = $this->boot()->db->table($collection)->fetchRow("`id` = {$id}"); $hal = new Hal("/collection/{$collection}/{$id}", $item->toArray()); $response = $response->withBody($this->toStream($hal->asJson())); } 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())); } if ($next) { $response = $next($request, $response); } return $response; }
//DELETE if ($method == "DELETE") { header("HTTP/1.0 404 Not Found"); echo "The person's record has been deleted successfully."; //ADD your SITE URL and KEYS here in the REST code below $ch = curl_init('<your CiviCRM site>/sites/all/modules/civicrm/extern/rest.php?entity=Contact&action=delete&json={"sequential":1,"id":' . $id . '}&api_key=yourkey&key=sitekey'); curl_setopt_array($ch, array(CURLOPT_CUSTOMREQUEST => "DELETE", CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array('Content-Type: application/json'))); $response = curl_exec($ch); if ($response === FALSE) { die(curl_error($ch)); } } elseif ($method == "PUT") { //ADD your SITE URL and KEYS here in the REST code below $ch = curl_init('<your CiviCRM site>/sites/all/modules/civicrm/extern/rest.php?entity=Contact&action=create&json={"sequential":1,"id":' . $id . ',"contact_type":"Individual","first_name":"' . $given_name . '","middle_name":"' . $additional_name . '","last_name":"' . $family_name . '","gender_id":"' . $gender . '","api.Address.create":{"location_type_id":"' . $location_type_id . '","street_address":"' . $postal_addresses . '"},"api.Email.create":{"email":"' . $email . '"},"api.Phone.create":{"phone":' . $phone . '}}&api_key=yourkey&key=sitekey'); curl_setopt_array($ch, array(CURLOPT_CUSTOMREQUEST => "PUT", CURLOPT_HEADER => FALSE, CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_HTTPHEADER => array('Content-Type: application/json'), CURLOPT_POSTFIELDS, http_build_query($data))); $response = curl_exec($ch); if ($response === FALSE) { die(curl_error($ch)); } } //ADD your SITE URL and KEYS here in the REST code below $json = file_get_contents('<your CiviCRM site>/sites/all/modules/civicrm/extern/rest.php?entity=People&action=get&json={"sequential":1,"id":' . $id . '}&api_key=yourkey&key=sitekey'); //ADD your SITE URL and KEYS here in the REST code below $json3 = file_get_contents('<your CiviCRM site>/sites/all/modules/civicrm/extern/rest.php?entity=Address&action=get&json={"sequential":1,"id":' . $id . '}&api_key=yourkey&key=sitekey'); $array = json_decode($json, true); $array3 = json_decode($json3, true); foreach ($array['values'] as $key => $value) { $hal = new \Nocarrier\Hal('/sites/default/ext/org.civicrm.osdi/api/v3/People/person.php' . '?id=' . $id, ['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:' . $id), 'id' => $array['values'][$key]['contact_id'], 'created_date' => date("c", strtotime($array2['values'][$i]['created_date'])), 'modified_date' => date("c", strtotime($array2['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']))]); } echo $hal->asJson();
/** * {@inheritdoc} */ public function done(Hal $response = null, $statusCode = HttpResponse::HTTP_OK, $headers = []) { $headers = array_merge(['content-type' => 'application/json'], $headers); $json = $response ? $response->asJson($this) : ''; return new HttpResponse($json, $statusCode, $headers); }
/** * Returns the data for output and sets the appropriate headers * @param \Nocarrier\Hal $hal * @return string */ public function renderOutput(\Nocarrier\Hal $hal) { $this->sendHeaders(); if ($this->getSystemErrors()) { $system_errors = array(); foreach ($this->getSystemErrors() as $key => $value) { $system_errors[$key] = $this->m62Lang($key); } $hal->setData($hal->getData() + array('_system_errors' => $system_errors)); } if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos(strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'xml') !== false) { header('Content-Type: application/hal+xml'); return $hal->asXml(true); } else { header('Content-Type: application/hal+json'); return $hal->asJson(true); } }
public function notFoundAction(Application $app, Request $request) { $response = new Hal($request->getRequestUri(), array("http status code" => 404, "http status description" => "Not Found", "message" => "document does not exist")); return new Response($response->asJson(), 404, array("Content-Type" => $app['request']->getMimeType('json'))); }
private function halResponse(Hal $resource) { return new Response($resource->asJson(true), 200, ['Content-Type' => 'application/hal+json']); }
/** * {@inheritdoc} * * We need to render the HAL object before we actually prepare the response. */ public function prepare(Request $request) { $this->setContent($this->hal->asJson($this->pretty)); return parent::prepare($request); }
/** * @param hal\Hal $hal * * @return string * * @codeCoverageIgnore */ protected function _jsonEncode(hal\Hal $hal) { return $hal->asJson(); }
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); }
public function testEmbeddingResourceWithSingleElement() { $hal = new Hal(); $hal->setResource('foo', (new Hal())->setResource('bar', new Hal())); $json = json_decode($hal->asJson()); $this->assertInternalType('array', $json->_embedded->foo->_embedded->bar); }
/** * @param $data * @param int $status * @param $header * @return \Illuminate\Http\Response */ public function render(Hal $data, $status = 200, $header) { $header = array_merge($this->headers, $header); return \Response::make($data->asJson(), $status, $header); }
public function testAttributesInXmlRepresentation() { $hal = new Hal('/', array('error' => array('@id' => 6, '@xml:lang' => 'en', 'message' => 'This is a message'))); $xml = new \SimpleXMLElement($hal->asXml()); $this->assertEquals(6, (string) $xml->error->attributes()->id); $this->assertEquals('en', (string) $xml->error->attributes()->lang); $this->assertEquals('This is a message', (string) $xml->error->message); $json = json_decode($hal->asJson(true)); $this->markTestIncomplete(); $this->assertEquals(6, $json->error->id); }