/** * Render a representation of a ResourceItem object. * * @param ResourceItem $resource A resource item object. * * @return A representation of the object. */ public function renderResourceItem(ResourceItem $resource) { // Initialise. $xml = '<resource>'; $links = $resource->getLinks()->getLinks(); // Does the resource have a self link? if (isset($links['self'])) { $self = $links['self']; $xml = '<resource rel="self" href="' . $self->getUri() . '">'; unset($links['self']); } // Iterate through the links and add them at the top. if (!empty($links)) { $xml .= $this->render($resource->getLinks()); } // Iterate through the metadata properties and add them to the top-level array. if (!empty($resource->getMetadata())) { $xml .= $this->render($resource->getMetadata()); } // Iterate through the data properties and add them to the top-level array. if (!empty($resource->getData())) { $xml .= $this->render($resource->getData()); } // Iterate through the embedded resources and add them to the _embedded element. if (!empty($resource->getEmbedded())) { $xml .= $this->render($resource->getEmbedded()); } $xml .= '</resource>'; return $xml; }
/** * Render a representation of a ResourceItem object. * * @param ResourceItem $resource A resource item object. * * @return A representation of the object. */ public function renderResourceItem(ResourceItem $resource) { $properties = array(); // Iterate through the metadata properties and add them to the top-level array. if (!empty($resource->getMetadata())) { foreach ($this->render($resource->getMetadata()) as $name => $property) { $properties[$name] = $property; } } // Iterate through the links and add them to the _links element. if (!empty($resource->getLinks())) { foreach ($this->render($resource->getLinks()) as $rel => $link) { $properties['_links'][$rel] = $link; } } // Iterate through the data properties and add them to the top-level array. if (!empty($resource->getData())) { foreach ($this->render($resource->getData()) as $name => $property) { $properties[$name] = $property; } } // Iterate through the embedded resources and add them to the _embedded element. if (!empty($resource->getEmbedded())) { foreach ($this->render($resource->getEmbedded()) as $rel => $embedded) { $properties['_embedded'][$rel] = $embedded; } } return json_encode($properties, JSON_PRETTY_PRINT); }