Skip to content

Commit

Permalink
Merge pull request #6 from baldurrensch/empty_self
Browse files Browse the repository at this point in the history
Allow empty self link
  • Loading branch information
baldurrensch committed Jan 9, 2013
2 parents f51e2c7 + 642a090 commit 4da5969
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
7 changes: 6 additions & 1 deletion library/Hal/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,12 @@ public function setTemplated($templated)
*/
public function toArray()
{
$link = array('href' => $this->getHref());
$href = $this->getHref();
if (empty($href)) {
return array();
}

$link = array('href' => $href);
if($this->getTitle()){
$link['title'] = $this->getTitle();
}
Expand Down
10 changes: 7 additions & 3 deletions library/Hal/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function setLink(Link $link, $singular=false, $plural=false)

/**
* Convenience function to set multiple links at once
*
*
* @see Resource::setLink()
* @param array $links Array of Link objects
* @param boolean $singular
Expand Down Expand Up @@ -155,7 +155,11 @@ public function toArray()
{
$data = array();
foreach ($this->_links as $rel => $link) {
$data['_links'][$rel] = $this->_recurseLinks($link);
$links = $this->_recurseLinks($link);

if (!empty($links)) {
$data['_links'][$rel] = $links;
}
}
foreach ($this->_data as $key => $value) {
$data[$key] = $value;
Expand All @@ -172,7 +176,7 @@ public function toArray()
protected function _recurseEmbedded($embeded)
{
$result = array();
if($embeded instanceof self){
if($embeded instanceof self){
$result = $embeded->toArray();
} else {
foreach ($embeded as $embed) {
Expand Down
46 changes: 44 additions & 2 deletions tests/library/Hal/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,53 @@ public function testEmptyResource()

$parent = new Resource('/dogs');
$parent->setLink(new Link('/dogs?q={text}', 'search'));

$parent->setEmbedded('dog', null);

$this->assertEquals(
json_decode($fixture), json_decode((string)$parent)
);
}

/**
* This tests adding a resource with no self link
*/
public function testEmptySelfLink()
{
$fixture = <<<'EOF'
{
"data": "value"
}
EOF;

$parent = new Resource('', array('data' => 'value'));

$this->assertEquals(
json_decode($fixture), json_decode((string) $parent)
);
}

/**
* This tests adding a resource with no self link
*/
public function testEmptySelfLinkWithOthers()
{
$fixture = <<<'EOF'
{
"_links":{
"search":{
"href":"/dogs?q={text}"
}
},
"data": "value"
}
EOF;

$parent = new Resource('', array('data' => 'value'));
$parent->setLink(new Link('/dogs?q={text}', 'search'));

$this->assertEquals(
json_decode($fixture), json_decode((string) $parent)
);
}
}

0 comments on commit 4da5969

Please sign in to comment.