/** * 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; }
$resource->addEmbedded(array('ea:order' => $embeddedResource)); // Get the data back out. print_r($resource->getInternalData()); // Generate a hal+json representation of the resource. // Well, not really HAL, but that's the idea. $representation = new RepresentationHalJson(); $json = $representation->render($resource); echo 'Representation as application/hal+json' . "\n"; echo $json . "\n"; // Generate a hal+xml representation of the resource. // Again, not really HAL, but the idea is there. $representation = new RepresentationHalXml(); $xml = $representation->render($resource); echo "\n"; echo 'Representation as application/hal+json' . "\n"; echo $xml . "\n"; /** * Example 2: Build a resource object by parsing an external representation * and then getting the data out in its internal format. */ // Start by creating a new, empty, resource. $resource = new ResourceItem($profile); // Show that it's empty. print_r($resource->getData()); // Parse an external representation. $data = array('name' => 'Chris Davenport', 'address' => 'Shrewsbury', 'size' => 10, 'show' => 'yes', 'notused' => 23, 'state' => 'trashed'); $representation = new RepresentationHalJson(); $representation->build($resource, json_encode($data)); // Pull the internal data out. Note that the properties have been transformed. print_r($resource->getInternalData()); // That's all folks!
/** * 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); }