Skip to content

Commit

Permalink
Merge pull request #3 from baldurrensch/templated_links
Browse files Browse the repository at this point in the history
Templated links and empty resources
  • Loading branch information
zircote committed Oct 29, 2012
2 parents 82e0c3f + 9723bd3 commit 83d4845
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"require": {
"php": ">=5.3.3"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
},
"autoload": {
"psr-0": {"Hal": "library"}
}
Expand Down
47 changes: 47 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 35 additions & 2 deletions library/Hal/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,31 @@ class Link extends AbstractHal
* @var string
*/
protected $_hreflang;

/**
* Whether this link is "templated"
* @var boolean
* @link https://tools.ietf.org/html/rfc6570
*/
protected $_templated;

/**
*
* @param string $href
* @param string $rel
* @param string $title
* @param string $name
* @param string $hreflang
* @param boolean $templated
*/
public function __construct($href, $rel = 'self', $title = null, $name = null, $hreflang = null)
public function __construct($href, $rel = 'self', $title = null, $name = null, $hreflang = null, $templated = false)
{
$this->setHref($href)
->setRel($rel)
->setName($name)
->setTitle($title)
->setHreflang($hreflang);
->setHreflang($hreflang)
->setTemplated($templated);
}
/**
* @return string
Expand Down Expand Up @@ -122,6 +132,15 @@ public function getHreflang ()
{
return $this->_hreflang;
}

/**
* @return boolean
*/
public function getTemplated()
{
return $this->_templated;
}

/**
* @param string $rel
* @return Link
Expand Down Expand Up @@ -169,6 +188,17 @@ public function setHreflang ($hreflang)
$this->_hreflang = $hreflang;
return $this;
}

/**
* @param boolean $templated
* @return Link
*/
public function setTemplated($templated)
{
$this->_templated = $templated;
return $this;
}

/**
*
* @return array
Expand All @@ -188,6 +218,9 @@ public function toArray()
if($this->getHreflang()){
$link['hreflang'] = $this->getHreflang();
}
if ($this->getTemplated()) {
$link['templated'] = $this->getTemplated();
}
return $link;
}
}
2 changes: 1 addition & 1 deletion library/Hal/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function setData($rel, $data = null)
* @param Resource $resource
* @return Resource
*/
public function setEmbedded($rel,Resource $resource, $singular = false)
public function setEmbedded($rel,Resource $resource = null, $singular = false)
{
if($singular){
$this->_embedded[$rel] = $resource;
Expand Down
77 changes: 77 additions & 0 deletions tests/library/Hal/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,5 +405,82 @@ public function testSimple()
json_decode($fixture), json_decode((string)$parent)
);
}

public function testTemplatedLink()
{
$fixture = <<<'EOF'
{
"_links":{
"self":{
"href":"/dogs"
},
"search":{
"href":"/dogs?q={text}",
"templated": true
}
}, "_embedded":{
"dog":[
{
"_links":{
"self":{
"href":"/dogs\/1"
}
},
"id":"1",
"name":"tiber",
"color":"black"
},
{
"_links":{
"self":{
"href":"/dogs\/2"
}
},
"id":"2",
"name":"sally",
"color":"white"
},
{
"_links":{
"self":{
"href":"/dogs\/3"
}
},
"id":"3",
"name":"fido",
"color":"gray"
}
]
}
}
EOF;

$parent = new Resource('/dogs');
/* Add any relevent links */
$parent->setLink(new Link('/dogs?q={text}', 'search', null, null, null, true));
$dogs[1] = new Resource('/dogs/1');
$dogs[1]->setData(
array('id' => '1', 'name' => 'tiber', 'color' => 'black')
);
$dogs[2] = new Resource('/dogs/2', array(
'id' => '2',
'name' => 'sally',
'color' => 'white'
));
$dogs[3] = new Resource('/dogs/3', array(
'id' => '3',
'name' => 'fido',
'color' => 'gray'
));
/* Add the embedded resources */
foreach ($dogs as $dog) {
$parent->setEmbedded('dog', $dog);
}

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

}
}

30 changes: 30 additions & 0 deletions tests/library/Hal/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,34 @@ public function testSetLinksMultipleSingluar()
$expected = json_decode($JSON);
$this->assertEquals($expected, $actual);
}

/**
* This tests adding an empty resource
*/
public function testEmptyResource()
{
$fixture = <<<'EOF'
{
"_links":{
"self":{
"href":"/dogs"
},
"search":{
"href":"/dogs?q={text}"
}
}, "_embedded":{
"dog":[]
}
}
EOF;

$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)
);
}
}

0 comments on commit 83d4845

Please sign in to comment.