Exemplo n.º 1
0
 /**
  * Render a HAL document in JSON format
  *
  * @param   array  $options  Rendering options. You can currently only set json_options (json_encode options)
  *
  * @return  string  The JSON representation of the HAL document
  */
 public function render($options = array())
 {
     if (isset($options['data_key'])) {
         $this->_dataKey = $options['data_key'];
     }
     if (isset($options['json_options'])) {
         $jsonOptions = $options['json_options'];
     } else {
         $jsonOptions = 0;
     }
     $serialiseThis = new stdClass();
     // Add links
     $collection = $this->_document->getLinks();
     $serialiseThis->_links = new stdClass();
     foreach ($collection as $rel => $links) {
         if (!is_array($links)) {
             $serialiseThis->_links->{$rel} = $this->_getLink($links);
         } else {
             $serialiseThis->_links->{$rel} = array();
             foreach ($links as $link) {
                 array_push($serialiseThis->_links->{$rel}, $this->_getLink($link));
             }
         }
     }
     // Add embedded documents
     $collection = $this->_document->getEmbedded();
     if (!empty($collection)) {
         $serialiseThis->_embedded->{$rel} = new stdClass();
         foreach ($collection as $rel => $embeddeddocs) {
             if (!is_array($embeddeddocs)) {
                 $embeddeddocs = array($embeddeddocs);
             }
             foreach ($embeddeddocs as $embedded) {
                 $renderer = new FOFHalRenderJson($embedded);
                 array_push($serialiseThis->_embedded->{$rel}, $renderer->render($options));
             }
         }
     }
     // Add data
     $data = $this->_document->getData();
     if (is_object($data)) {
         if ($data instanceof FOFTable) {
             $data = $data->getData();
         } else {
             $data = (array) $data;
         }
         if (!empty($data)) {
             foreach ($data as $k => $v) {
                 $serialiseThis->{$k} = $v;
             }
         }
     } elseif (is_array($data)) {
         $serialiseThis->{$this->_dataKey} = $data;
     }
     return json_encode($serialiseThis, $jsonOptions);
 }
Exemplo n.º 2
0
 /**
  * Creates a FOFHalDocument using the provided data
  *
  * @param   array     $data   The data to put in the document
  * @param   FOFModel  $model  The model of this view
  *
  * @return  FOFHalDocument  A HAL-enabled document
  */
 protected function _createDocumentWithHypermedia($data, $model = null)
 {
     // Create a new HAL document
     if (is_array($data)) {
         $count = count($data);
     } else {
         $count = null;
     }
     if ($count == 1) {
         reset($data);
         $document = new FOFHalDocument(end($data));
     } else {
         $document = new FOFHalDocument($data);
     }
     // Create a self link
     $uri = (string) JUri::getInstance();
     $uri = $this->_removeURIBase($uri);
     $uri = JRoute::_($uri);
     $document->addLink('self', new FOFHalLink($uri));
     // Create relative links in a record list context
     if (is_array($data) && $model instanceof FOFModel) {
         $pagination = $model->getPagination();
         if ($pagination->get('pages.total') > 1) {
             // Try to guess URL parameters and create a prototype URL
             // NOTE: You are better off specialising this method
             $protoUri = $this->_getPrototypeURIForPagination();
             // The "first" link
             $uri = clone $protoUri;
             $uri->setVar('limitstart', 0);
             $uri = JRoute::_((string) $uri);
             $document->addLink('first', new FOFHalLink($uri));
             // Do we need a "prev" link?
             if ($pagination->get('pages.current') > 1) {
                 $prevPage = $pagination->get('pages.current') - 1;
                 $limitstart = ($prevPage - 1) * $pagination->limit;
                 $uri = clone $protoUri;
                 $uri->setVar('limitstart', $limitstart);
                 $uri = JRoute::_((string) $uri);
                 $document->addLink('prev', new FOFHalLink($uri));
             }
             // Do we need a "next" link?
             if ($pagination->get('pages.current') < $pagination->get('pages.total')) {
                 $nextPage = $pagination->get('pages.current') + 1;
                 $limitstart = ($nextPage - 1) * $pagination->limit;
                 $uri = clone $protoUri;
                 $uri->setVar('limitstart', $limitstart);
                 $uri = JRoute::_((string) $uri);
                 $document->addLink('next', new FOFHalLink($uri));
             }
             // The "last" link?
             $lastPage = $pagination->get('pages.total');
             $limitstart = ($lastPage - 1) * $pagination->limit;
             $uri = clone $protoUri;
             $uri->setVar('limitstart', $limitstart);
             $uri = JRoute::_((string) $uri);
             $document->addLink('last', new FOFHalLink($uri));
         }
     }
     return $document;
 }