From 4fa376e12c0e10c120db7b962e1ded7547c4c4d1 Mon Sep 17 00:00:00 2001 From: yacinebenfaid Date: Wed, 9 Oct 2013 14:13:28 +0200 Subject: [PATCH 1/3] add singleResource --- .../ApiResource/ProxyResource.php | 25 +++++++++++++++++++ .../RestHalBundle/ApiResource/Resource.php | 7 ++++++ .../RestHalBundle/Renderer/ProxyRenderer.php | 24 ++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 src/Alterway/Bundle/RestHalBundle/Renderer/ProxyRenderer.php 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..86131d3 100644 --- a/src/Alterway/Bundle/RestHalBundle/ApiResource/Resource.php +++ b/src/Alterway/Bundle/RestHalBundle/ApiResource/Resource.php @@ -35,6 +35,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); 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 From ba3d7f0e6c480e2bc96b087d6f957a240828ecfb Mon Sep 17 00:00:00 2001 From: yacinebenfaid Date: Tue, 15 Oct 2013 11:23:01 +0200 Subject: [PATCH 2/3] add link --- composer.json | 2 +- .../Bundle/RestHalBundle/ApiResource/Resource.php | 10 ++++++++-- .../RestHalBundle/ApiResource/ResourceInterface.php | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) 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/Resource.php b/src/Alterway/Bundle/RestHalBundle/ApiResource/Resource.php index 86131d3..e2b258f 100644 --- a/src/Alterway/Bundle/RestHalBundle/ApiResource/Resource.php +++ b/src/Alterway/Bundle/RestHalBundle/ApiResource/Resource.php @@ -23,9 +23,10 @@ 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); + $this->hal->addLink($rel, $this->generate($route, $routeParams), null, + array_merge($attributes, array('method' => reset($this->router->getRouteCollection()->get($route)->getMethods())))); return $this; } @@ -66,6 +67,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); From 0417c34154506143d954284a0469e23ac9c900f0 Mon Sep 17 00:00:00 2001 From: yacinebenfaid Date: Tue, 15 Oct 2013 14:29:34 +0200 Subject: [PATCH 3/3] bug fix --- src/Alterway/Bundle/RestHalBundle/ApiResource/Resource.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Alterway/Bundle/RestHalBundle/ApiResource/Resource.php b/src/Alterway/Bundle/RestHalBundle/ApiResource/Resource.php index e2b258f..daaec55 100644 --- a/src/Alterway/Bundle/RestHalBundle/ApiResource/Resource.php +++ b/src/Alterway/Bundle/RestHalBundle/ApiResource/Resource.php @@ -25,8 +25,9 @@ public function __construct(RouterInterface $router) public function addLink($rel, $route, array $routeParams = array(), array $attributes = array()) { + $methods = $this->router->getRouteCollection()->get($route)->getMethods(); $this->hal->addLink($rel, $this->generate($route, $routeParams), null, - array_merge($attributes, array('method' => reset($this->router->getRouteCollection()->get($route)->getMethods())))); + array_merge($attributes, array('method' => reset($methods)))); return $this; } @@ -74,4 +75,4 @@ public function generate($title, $params) abstract protected function prepare(); abstract protected function generateUri(); -} \ No newline at end of file +}