Skip to content

Instantly share code, notes, and snippets.

@adammbalogh
Last active August 29, 2015 14:13
Show Gist options
  • Save adammbalogh/3b07d40339e4eb589d9e to your computer and use it in GitHub Desktop.
Save adammbalogh/3b07d40339e4eb589d9e to your computer and use it in GitHub Desktop.
<?php
use Phprest\Util\Controller;
use Symfony\Component\HttpFoundation\Request;
use Phprest\Response;
use League\Fractal\Manager;
use League\Fractal\Resource\Item;
class Temperature extends Controller
{
public function get(Request $request, $version, $id)
{
# you should create a service for Fractal
$fractal = new Manager();
# result data from a datasource
$data = [
'id' => '1',
'value' => '30',
'created' => '2015-01-10 10:10:10'
];
$resource = new Item($data, function(array $temperature) {
return [
'id' => (int)$temperature['id'],
'value' => (int)$temperature['value'],
'created' => new \DateTime($temperature['created'], new \DateTimeZone('UTC')),
'links' => [
[
'rel' => 'self',
'uri' => '/temperatures/' . $temperature['id'],
]
]
];
});
return new Response\Ok(
$fractal->createData($resource)->toArray()
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment