コード例 #1
0
ファイル: JsonHal.php プロジェクト: bigpoint/slim-bootstrap
 /**
  * This function outputs the given $data as valid HAL+JSON to the client
  * and sets the HTTP Response Code to the given $statusCode.
  *
  * @param array|SlimBootstrap\DataObject $data       The data to output to
  *                                                   the client
  * @param int                            $statusCode The status code to set
  *                                                   in the reponse
  */
 public function write($data, $statusCode = 200)
 {
     $path = $this->_request->getPath();
     $hal = new hal\Hal($path);
     if (true === is_array($data)) {
         $pathData = explode('/', $path);
         unset($pathData[0]);
         $endpointName = end($pathData);
         $endpointUri = '/' . implode('/', $pathData) . '/';
         foreach ($data as $entry) {
             /** @var SlimBootstrap\DataObject $entry */
             $identifiers = $entry->getIdentifiers();
             $resourceName = $endpointUri . implode('/', array_values($identifiers));
             $resource = new hal\Hal($resourceName, $entry->getData() + $entry->getIdentifiers());
             $this->_addAdditionalLinks($resource, $entry->getLinks());
             $hal->addLink($endpointName, $resourceName);
             $hal->addResource($endpointName, $resource);
         }
     } else {
         $hal->setData($data->getData() + $data->getIdentifiers());
         $this->_addAdditionalLinks($hal, $data->getLinks());
     }
     $body = $this->_jsonEncode($hal);
     if (false === $body) {
         $this->_response->setStatus(500);
         $this->_response->setBody("Error encoding requested data.");
         return;
     }
     $this->_headers->set('Content-Type', 'application/hal+json; charset=UTF-8');
     $this->_response->setStatus($statusCode);
     $this->_response->setBody($hal->asJson());
 }
コード例 #2
0
 /**
  * @param $id
  * @return \Illuminate\Http\JsonResponse
  */
 public function show($id)
 {
     $input = \Input::get('format', 'json');
     $array = [];
     $recipe = $this->recipe->getRecipe($id);
     if ($recipe) {
         $category = $this->category->getCategory($recipe->category_id);
         $array = ['id' => $recipe->recipe_id, 'title' => $recipe->title, 'problem' => $recipe->problem, 'category' => ['description' => $category->description, 'name' => $category->name]];
     }
     // render for hypermedia
     if ($input == 'hal') {
         $this->hal->setUri(route('home.recipe', ['one' => $id]));
         $this->hal->setData($array);
         $array = $this->hal;
     }
     return $this->render($array, $input);
 }
コード例 #3
0
 /**
  * {@inheritdoc}
  */
 public function abort($statusCode, array $attributes = [])
 {
     $response = new Hal(null);
     $response->setData($attributes);
     switch ($statusCode) {
         case 401:
             throw new UnauthorizedHttpException('Unauthorized');
         case 404:
             throw new NotFoundHttpException();
         case 409:
             throw new ConflictHttpException($response->asJson());
         default:
             throw new HttpException($statusCode, $response->asJson(), null, ['content-type' => 'application/json']);
     }
 }
コード例 #4
0
ファイル: Rest.php プロジェクト: jaeger-app/rest-server
 /**
  * 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);
     }
 }
コード例 #5
0
 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;
 }
コード例 #6
0
ファイル: VndErrorResponse.php プロジェクト: jsor/stack-hal
 public static function fromException(\Exception $exception, $prettyPrint = true, $debug = false)
 {
     $statusCode = self::extractStatus($exception);
     $headers = self::extractHeaders($exception);
     $message = self::extractMessage($exception, $debug);
     if ($exception instanceof HalException) {
         $hal = $exception->getHal();
     } else {
         $hal = new Hal(null, ['message' => $message]);
     }
     $data = $hal->getData();
     if (!isset($data['message']) || '' === $data['message']) {
         if ($message) {
             $data['message'] = $message;
         } elseif (isset(Response::$statusTexts[$statusCode])) {
             $data['message'] = Response::$statusTexts[$statusCode];
         }
         $hal->setData($data);
     }
     return new static($hal, $statusCode, $headers, $prettyPrint);
 }