/** * Parse a serialisation into a ResourceItem object. * * @param Resource $resource A resource object to build. * @param string $serialisation A serialisation for parsing. * * @return void */ public function parseResourceItem(ResourceItem $resource, $serialisation) { $properties = array(); $data = json_decode($serialisation); foreach ($data as $name => $value) { $properties[$name] = $value; } // Add the data to the resource. $resource->addData($properties, false); }
// Add some metadata. Can be anything as it's not checked against the profile. $resource->addMetadata(array('date' => date('Y-m-d H:i:s'))); // Add some links. $links = array('self' => new ResourceLink('/orders'), 'next' => new ResourceLink('/orders?page=2'), 'ea:find' => new ResourceLink('/orders{?id}', true), 'ea:admin' => array(new ResourceLink('/admins/2', false, 'Fred'), new ResourceLink('/admins/5', false, 'Kate'))); $resource->addLinks($links); // Add some curies. $curies = array(new ResourceCurie('ea', 'http://example.com/docs/rels/{rel}', true), new ResourceCurie('joomla', 'http://docs.joomla.org/rels/{rel}', true)); $resource->addCuries($curies); // Now create a resource that will be embedded in the main resource. $embeddedResource = new ResourceItem(new Profile('profile/order.json')); $embeddedResource->addData(array('total' => '30.00', 'currency' => 'USD', 'status' => 'shipped'))->addLinks(array('self' => new ResourceLink('/orders/123'), 'ea:basket' => new ResourceLink('/baskets/98712'), 'ea:customer' => new ResourceLink('/customers/7809'))); // Embed the resource into the main resource. $resource->addEmbedded(array('ea:order' => $embeddedResource)); // And create another resource that will be embedded in the main resource. $embeddedResource = new ResourceItem(new Profile('profile/contact.json')); $embeddedResource->addData(array('total' => '20.00', 'currency' => 'USD', 'status' => 'processing'))->addLinks(array('self' => new ResourceLink('/orders/124'), 'ea:basket' => new ResourceLink('/baskets/97213'), 'ea:customer' => new ResourceLink('/customers/12369'))); // Embed the resource into the main resource. $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";
// Add some metadata. Can be anything as it's not checked against the profile. $resource->addMetadata(array('date' => date('Y-m-d H:i:s'))); // Add some links. $links = array('self' => new ResourceLink('/orders'), 'next' => new ResourceLink('/orders?page=2'), 'ea:find' => new ResourceLink('/orders{?id}', true), 'ea:admin' => array(new ResourceLink('/admins/2', false, 'Fred'), new ResourceLink('/admins/5', false, 'Kate'))); $resource->addLinks($links); // Add some curies. $curies = array(new ResourceCurie('ea', 'http://example.com/docs/rels/{rel}', true), new ResourceCurie('joomla', 'http://docs.joomla.org/rels/{rel}', true)); $resource->addCuries($curies); // Now create a resource that will be embedded in the main resource. $embeddedResource = new ResourceItem(new Profile('profile/order.json')); $embeddedResource->addData(array('total' => '30.00', 'currency' => 'USD', 'status' => 'shipped'))->addLinks(array('self' => new ResourceLink('/orders/123'), 'ea:basket' => new ResourceLink('/baskets/98712'), 'ea:customer' => new ResourceLink('/customers/7809'))); // Embed the resource into the main resource. $resource->addEmbedded(array('ea:order' => $embeddedResource)); // And create another resource that will be embedded in the main resource. $embeddedResource = new ResourceItem(new Profile('profile/order.json')); $embeddedResource->addData(array('total' => '50.00', 'currency' => 'GBP', 'status' => 'pending'))->addLinks(array('self' => new ResourceLink('/orders/125'), 'ea:basket' => new ResourceLink('/baskets/35131'), 'ea:customer' => new ResourceLink('/customers/55646'))); // Embed the resource into the main resource. $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";