Last active
August 29, 2015 14:13
-
-
Save adammbalogh/3b07d40339e4eb589d9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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