Skip to content

Instantly share code, notes, and snippets.

@ritalin
Last active August 29, 2015 14:22
Show Gist options
  • Save ritalin/4b784450f1dbbe0b6c56 to your computer and use it in GitHub Desktop.
Save ritalin/4b784450f1dbbe0b6c56 to your computer and use it in GitHub Desktop.
$ php bootstrap/api.php get "/periods/3"
200 OK
content-type: application/hal+json
{
"periods": [
{
"date": {
"date": "2015-06-08 23:27:51.000000",
"timezone_type": 3,
"timezone": "Asia/Tokyo"
},
"year": "2015",
"month": "06",
"day": "08",
"_links": {
"self": {
"href": "/date/2015/06/08"
}
}
},
{
"date": {
"date": "2015-06-09 23:27:51.000000",
"timezone_type": 3,
"timezone": "Asia/Tokyo"
},
"year": "2015",
"month": "06",
"day": "09",
"_links": {
"self": {
"href": "/date/2015/06/09"
}
}
},
{
"date": {
"date": "2015-06-10 23:27:51.000000",
"timezone_type": 3,
"timezone": "Asia/Tokyo"
},
"year": "2015",
"month": "06",
"day": "10",
"_links": {
"self": {
"href": "/date/2015/06/10"
}
}
},
{
"date": {
"date": "2015-06-11 23:27:51.000000",
"timezone_type": 3,
"timezone": "Asia/Tokyo"
},
"year": "2015",
"month": "06",
"day": "11",
"_links": {
"self": {
"href": "/date/2015/06/11"
}
}
},
{
"date": {
"date": "2015-06-12 23:27:51.000000",
"timezone_type": 3,
"timezone": "Asia/Tokyo"
},
"year": "2015",
"month": "06",
"day": "12",
"_links": {
"self": {
"href": "/date/2015/06/12"
}
}
}
],
"to": "3",
"_links": {
"self": {
"href": "/periods/3"
}
}
}
<?php
namespace Embed\Sample\Resource\App;
use Bear\Resource\ResourceObject;
use Bear\Resource\FactoryInterface;
use Bear\Resource\AbstractUri;
class PeriodCollection extends ResourceObject {
/**
* @var FactoryInterface
*/
private $resourceFactory;
public function __construct(FactoryInterface $f) {
$this->resourceFactory = $f;
}
public function onGet($to) {
$this["periods"] = array_map(
function ($n) use($to) {
return new \DateTime("+$n day");
},
range(0, $to+1)
);
$fn = function (\DateTime $content) {
return [
'year' => $content->format('Y'),
'month' => $content->format('m'),
'day' => $content->format('d'),
];
};
$this->body = $this->toItemResource('date', '/periodItem', $this->body, $fn);
return $this;
}
private function toItemResource($rel, $path, $contents, callable $fn) {
$uri = clone $this->uri;
$uri->path = $path;
return array_map(
function ($content) use($rel, $uri, $contents, $fn) {
return is_array($content) ? $this->toItemResourceSub($rel, $uri, $content, $fn) : $content;
},
$contents
);
}
private function toItemResourceSub($rel, $uri, array $content, callable $keySelector) {
return array_map(
function ($body) use($rel, $uri, $keySelector) {
$ro = $this->resourceFactory->newInstance($uri);
$ro[$rel] = $body;
$ro->uri = clone $uri;
$ro->uri->query = $keySelector($body);
return $ro;
},
$content
);
}
public function __toString() {
$this->body = array_map(
function ($resources) {
return $this->renderChildren($resources);
},
$this->body
);
return parent::__toString();
}
protected function renderChildren($resources) {
if (! (current($resources) instanceof ResourceObject)) return $resources;
return array_map(
function (ResourceObject $ro) {
return json_decode((string)$ro);
},
$resources
);
}
}
<?php
namespace Embed\Sample\Resource\App;
use Bear\Resource\ResourceObject;
class PeriodItem extends ResourceObject {
public function onGet($year, $month, $day) {
$this["date"] = new \DateTime("{$year}-{$month}-{$day}");
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment