Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 25 additions & 0 deletions src/Alterway/Bundle/RestHalBundle/ApiResource/ProxyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Alterway\Bundle\RestHalBundle\ApiResource;

use Alterway\Bundle\RestHalBundle\Renderer\ProxyRenderer;
use Nocarrier\Hal;

class ProxyResource extends Hal
Expand All @@ -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);
}
}
7 changes: 7 additions & 0 deletions src/Alterway/Bundle/RestHalBundle/ApiResource/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions src/Alterway/Bundle/RestHalBundle/Renderer/ProxyRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Alterway\Bundle\RestHalBundle\Renderer;

use Nocarrier\HalJsonRenderer;

class ProxyRenderer extends HalJsonRenderer
{
/**
* Return an array (compatible with the hal+json format) representing associated resources
*
* @param mixed $resources
* @return array
*/
protected function resourcesForJson($resources)
{
if(!is_array($resources)) {
return $this->arrayForJson($resources);

}

return parent::resourcesForJson($resources);
}
}