diff --git a/composer.json b/composer.json index c6b321d..88ff205 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "php": ">=5.3.3", "symfony/symfony": "~2.2", "sensio/framework-extra-bundle": "~2.2", - "nocarrier/hal": "*" + "nocarrier/hal": "0.9.2" }, "require-dev": { "behat/behat": ">=2.2.2", diff --git a/src/Alterway/Bundle/RestHalBundle/ApiResource/ProxyResource.php b/src/Alterway/Bundle/RestHalBundle/ApiResource/ProxyResource.php index 7db32e2..3b90f9e 100644 --- a/src/Alterway/Bundle/RestHalBundle/ApiResource/ProxyResource.php +++ b/src/Alterway/Bundle/RestHalBundle/ApiResource/ProxyResource.php @@ -2,6 +2,7 @@ namespace Alterway\Bundle\RestHalBundle\ApiResource; +use Alterway\Bundle\RestHalBundle\Renderer\ProxyRenderer; use Nocarrier\Hal; class ProxyResource extends Hal @@ -19,4 +20,28 @@ public function setUri($uri) return $this; } + /** + * Add an embedded resource, identified by $rel and represented by $resource. + * + * @param string $rel + * @param Hal $resource + * @return ProxyResource + */ + public function addSingleResource($rel, Hal $resource = null) + { + $this->resources[$rel] = $resource; + return $this; + } + + /** + * Return the current object in a application/hal+json format (links and resources) + * + * @param bool $pretty Enable pretty-printing + * @return string + */ + public function asJson($pretty=false) + { + $renderer = new ProxyRenderer(); + return $renderer->render($this, $pretty); + } } \ No newline at end of file diff --git a/src/Alterway/Bundle/RestHalBundle/ApiResource/Resource.php b/src/Alterway/Bundle/RestHalBundle/ApiResource/Resource.php index 804fb8c..daaec55 100644 --- a/src/Alterway/Bundle/RestHalBundle/ApiResource/Resource.php +++ b/src/Alterway/Bundle/RestHalBundle/ApiResource/Resource.php @@ -23,9 +23,11 @@ public function __construct(RouterInterface $router) $this->hal = new ProxyResource(); } - public function addLink($rel, $uri, $title = null, array $attributes = array()) + public function addLink($rel, $route, array $routeParams = array(), array $attributes = array()) { - $this->hal->addLink($rel, $uri, $title, $attributes); + $methods = $this->router->getRouteCollection()->get($route)->getMethods(); + $this->hal->addLink($rel, $this->generate($route, $routeParams), null, + array_merge($attributes, array('method' => reset($methods)))); return $this; } @@ -35,6 +37,13 @@ public function addResource($rel, ResourceInterface $resource = null) return $this; } + public function addSingleResource($rel, ResourceInterface $resource = null) + { + $this->hal->addSingleResource($rel, $resource->getHal()); + return $this; + } + + public function setData(array $data) { $this->hal->setData($data); @@ -59,6 +68,11 @@ public function getHal() return $this->hal; } + public function generate($title, $params) + { + return $this->router->generate($title, $params); + } + abstract protected function prepare(); abstract protected function generateUri(); -} \ No newline at end of file +} diff --git a/src/Alterway/Bundle/RestHalBundle/ApiResource/ResourceInterface.php b/src/Alterway/Bundle/RestHalBundle/ApiResource/ResourceInterface.php index 69f1d15..5a98fce 100644 --- a/src/Alterway/Bundle/RestHalBundle/ApiResource/ResourceInterface.php +++ b/src/Alterway/Bundle/RestHalBundle/ApiResource/ResourceInterface.php @@ -6,7 +6,7 @@ interface ResourceInterface { - public function addLink($rel, $uri, $title = null, array $attributes = array()); + public function addLink($rel, $route, array $routeParams = array(), array $attributes = array()); public function addResource($rel, ResourceInterface $resource = null); diff --git a/src/Alterway/Bundle/RestHalBundle/Renderer/ProxyRenderer.php b/src/Alterway/Bundle/RestHalBundle/Renderer/ProxyRenderer.php new file mode 100644 index 0000000..91bd533 --- /dev/null +++ b/src/Alterway/Bundle/RestHalBundle/Renderer/ProxyRenderer.php @@ -0,0 +1,24 @@ +arrayForJson($resources); + + } + + return parent::resourcesForJson($resources); + } +} \ No newline at end of file