Ejemplo n.º 1
0
 /**
  * render
  *
  * @param Hal $resource
  * @param bool $pretty
  * @return string
  */
 public function render(Hal $resource, $pretty)
 {
     $doc = new \SimpleXMLElement('<resource></resource>');
     $doc->addAttribute('href', $resource->getUri());
     $this->linksForXml($doc, $resource->getLinks());
     $this->arrayToXml($resource->getData(), $doc);
     foreach ($resource->getResources() as $rel => $resources) {
         $this->resourcesForXml($doc, $rel, $resources);
     }
     $dom = dom_import_simplexml($doc);
     if ($pretty) {
         $dom->ownerDocument->preserveWhiteSpace = false;
         $dom->ownerDocument->formatOutput = true;
     }
     return $dom->ownerDocument->saveXML();
 }
Ejemplo n.º 2
0
 /**
  * Return an array (compatible with the hal+json format) representing the
  * complete response
  *
  * @param Hal $resource
  * @return array
  */
 protected function arrayForJson(Hal $resource)
 {
     $data = $resource->getData();
     $data['_links'] = $this->linksForJson($resource->getUri(), $resource->getLinks());
     foreach ($resource->getResources() as $rel => $resources) {
         $data['_embedded'][$rel] = $this->resourcesForJson($resources);
     }
     return $data;
 }
Ejemplo n.º 3
0
 /**
  * Return an array (compatible with the hal+json format) representing the
  * complete response.
  *
  * @param \Nocarrier\Hal $resource
  * @return array
  */
 protected function arrayForJson(Hal $resource = null)
 {
     if ($resource == null) {
         return array();
     }
     $data = $resource->getData();
     if ($resource->getShouldStripAttributes()) {
         $data = $this->stripAttributeMarker($data);
     }
     $links = $this->linksForJson($resource->getUri(), $resource->getLinks(), $resource->getArrayLinkRels());
     if (count($links)) {
         $data['_links'] = $links;
     }
     foreach ($resource->getRawResources() as $rel => $resources) {
         if (count($resources) === 1 && !in_array($rel, $resource->getArrayResourceRels())) {
             $resources = $resources[0];
         }
         $data['_embedded'][$rel] = $this->resourcesForJson($resources);
     }
     return $data;
 }
Ejemplo n.º 4
0
 /**
  * Return an array (compatible with the hal+json format) representing the
  * complete response.
  *
  * @param \Nocarrier\Hal $resource
  * @return array
  */
 protected function arrayForJson(Hal $resource = null)
 {
     if ($resource == null) {
         return array();
     }
     $data = $resource->getData();
     $data = $this->stripAttributeMarker($data);
     $links = $this->linksForJson($resource->getUri(), $resource->getLinks());
     if (count($links)) {
         $data['_links'] = $links;
     }
     foreach ($resource->getResources() as $rel => $resources) {
         $data['_embedded'][$rel] = $this->resourcesForJson($resources);
     }
     return $data;
 }