Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactoring of namespace, and incorporated the changes made by @hradtke

  • Loading branch information...
commit bc59106e0af0ea14ce71d1c8aaef89b43aecf569 1 parent 5472967
@zircote authored
View
11 library/Zircote/Hal/AbstractHal.php → library/Hal/AbstractHal.php
@@ -17,12 +17,11 @@
* limitations under the License.
*
*
- * @package
- * @subpackage
+ * @package Hal
*
*
*/
-abstract class Zircote_Hal_AbstractHal
+abstract class Hal_AbstractHal
{
/**
*
@@ -31,10 +30,10 @@
protected $_xml;
/**
* @param SimpleXMLElement $xml
- * @param Zircote_Hal_Link $link
- * @return Zircote_Hal_AbstractHal
+ * @param Hal_Link $link
+ * @return Hal_AbstractHal
*/
- public function setXMLAttributes(SimpleXMLElement $xml, Zircote_Hal_Link $link)
+ public function setXMLAttributes(SimpleXMLElement $xml, Hal_Link $link)
{
$xml->addAttribute('href', $link->getHref());
if($link->getRel() && $link->getRel() !== 'self'){
View
15 library/Zircote/Hal/Link.php → library/Hal/Link.php
@@ -16,10 +16,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- * @author zircote
- * @package Zircote_Hal
+ * @package Hal
*/
-class Zircote_Hal_Link extends Zircote_Hal_AbstractHal
+class Hal_Link extends Hal_AbstractHal
{
/**
* For labeling the destination of a link with a human-readable identifier.
@@ -117,7 +116,7 @@ public function getHreflang ()
}
/**
* @param string $rel
- * @return Zircote_Hal_Link
+ * @return Hal_Link
*/
public function setRel ($rel)
{
@@ -126,7 +125,7 @@ public function setRel ($rel)
}
/**
* @param string $href
- * @return Zircote_Hal_Link
+ * @return Hal_Link
*/
public function setHref ($href)
{
@@ -136,7 +135,7 @@ public function setHref ($href)
/**
*
* @param string $name
- * @return Zircote_Hal_Link
+ * @return Hal_Link
*/
public function setName($name)
{
@@ -145,7 +144,7 @@ public function setName($name)
}
/**
* @param string $title
- * @return Zircote_Hal_Link
+ * @return Hal_Link
*/
public function setTitle ($title)
{
@@ -155,7 +154,7 @@ public function setTitle ($title)
/**
* @param string $hreflang
- * @return Zircote_Hal_Link
+ * @return Hal_Link
*/
public function setHreflang ($hreflang)
{
View
50 library/Zircote/Hal/Resource.php → library/Hal/Resource.php
@@ -15,14 +15,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- * @author zircote
- * @package Zircote_Hal
+ * @package Hal
*
*/
-class Zircote_Hal_Resource extends Zircote_Hal_AbstractHal
+class Hal_Resource extends Hal_AbstractHal
{
/**
- * Internal storage of `Zircote_Hal_Link` objects
+ * Internal storage of `Hal_Link` objects
* @var array
*/
protected $_links = array();
@@ -32,7 +31,7 @@ class Zircote_Hal_Resource extends Zircote_Hal_AbstractHal
*/
protected $_data = array();
/**
- * Internal storage of `Zircote_Hal_Resource` objects
+ * Internal storage of `Hal_Resource` objects
* @var array
*/
protected $_embedded = array();
@@ -41,14 +40,15 @@ class Zircote_Hal_Resource extends Zircote_Hal_AbstractHal
* @param string $href
* @param string $name
*/
- public function __construct($href, $rel = null, $title = null, $name = null, $hreflang = null)
+ public function __construct($href, array $data = array(), $title = null, $name = null, $hreflang = null)
{
$this->setLink(
- new Zircote_Hal_Link($href, 'self', $title, $name, $hreflang)
+ new Hal_Link($href, 'self', $title, $name, $hreflang)
);
+ $this->setData($data);
}
/**
- * @return Zircote_Hal_Link
+ * @return Hal_Link
*/
public function getSelf()
{
@@ -63,10 +63,10 @@ public function getLinks()
}
/**
*
- * @param Zircote_Hal_Link $link
- * @return Zircote_Hal_Resource
+ * @param Hal_Link $link
+ * @return Hal_Resource
*/
- public function setLink(Zircote_Hal_Link $link)
+ public function setLink(Hal_Link $link)
{
$this->_links[$link->getRel()] = $link;
return $this;
@@ -74,7 +74,7 @@ public function setLink(Zircote_Hal_Link $link)
/**
*
* @param array $data
- * @return Zircote_Hal_Resource
+ * @return Hal_Resource
*/
public function setData($rel, $data = null)
{
@@ -89,10 +89,10 @@ public function setData($rel, $data = null)
}
/**
*
- * @param Zircote_Hal_Resource $resource
- * @return Zircote_Hal_Resource
+ * @param Hal_Resource $resource
+ * @return Hal_Resource
*/
- public function setEmbedded($rel,Zircote_Hal_Resource $resource, $singular = false)
+ public function setEmbedded($rel,Hal_Resource $resource, $singular = false)
{
if($singular){
$this->_embedded[$rel] = $resource;
@@ -177,9 +177,9 @@ public function getXML($xml = null)
*/
protected function _getEmbedded($embedded, $_rel = null)
{
- /* @var $embed Zircote_Hal_Resource */
+ /* @var $embed Hal_Resource */
foreach ($embedded as $rel => $embed) {
- if($embed instanceof Zircote_Hal_Resource){
+ if($embed instanceof Hal_Resource){
$rel = is_numeric($rel) ? $_rel : $rel;
$this->_getEmbRes($embed)->addAttribute('rel', $rel);
} else {
@@ -187,7 +187,7 @@ protected function _getEmbedded($embedded, $_rel = null)
}
}
}
- protected function _getEmbRes(Zircote_Hal_Resource $embed)
+ protected function _getEmbRes(Hal_Resource $embed)
{
$resource = $this->_xml->addChild('resource');
return $embed->getXML($resource);
@@ -195,7 +195,7 @@ protected function _getEmbRes(Zircote_Hal_Resource $embed)
/**
*
* @param SimpleXMLElement $xml
- * @return Zircote_Hal_Resource
+ * @return Hal_Resource
*/
public function setXML(SimpleXMLElement $xml)
{
@@ -217,7 +217,7 @@ protected function _addData(SimpleXMLElement $xml, $data, $key = null)
foreach ($data as $_key => $value) {
if(!is_numeric($_key) && is_array($value)){
foreach ($value as $v) {
- $c = $xml->addChild($_key)->addChild(rtrim($_key, 's'));
+ $c = $xml->addChild($_key)->addChild($_key);
foreach ($v as $name => $value) {
$c->addChild($name, $value);
}
@@ -238,9 +238,9 @@ protected function _addData(SimpleXMLElement $xml, $data, $key = null)
}
/**
*
- * @param Zircote_Hal_Link $link
+ * @param Hal_Link $link
*/
- protected function _addLinks(Zircote_Hal_Link $link)
+ protected function _addLinks(Hal_Link $link)
{
if($link->getRel() != 'self' && !is_numeric($link->getRel())){
$this->_addLink($link);
@@ -248,10 +248,10 @@ protected function _addLinks(Zircote_Hal_Link $link)
}
/**
*
- * @param Zircote_Hal_Link $link
- * @return Zircote_Hal_Resource
+ * @param Hal_Link $link
+ * @return Hal_Resource
*/
- protected function _addLink(Zircote_Hal_Link $link)
+ protected function _addLink(Hal_Link $link)
{
$this->setXMLAttributes($this->_xml->addChild('link'), $link);
return $this;
View
22 readme.md
@@ -2,22 +2,24 @@
* http://stateless.co/hal_specification.html
* http://blog.stateless.co/post/13296666138/json-linking-with-hal
* http://www.mnot.net/blog/2011/11/25/linking_in_json
+ * https://gist.github.com/2289546
+ * https://groups.google.com/d/forum/hal-discuss
```php
<?php
/* Create a new Resource Parent */
-$parent = new Zircote_Hal_Resource('/dogs');
+$parent = new Hal_Resource('/dogs');
/* Add any relevent links */
-$parent->setLink(new Zircote_Hal_Link('/dogs?q={text}', 'search'));
-$dogs[1] = new Zircote_Hal_Resource('/dogs/1');
+$parent->setLink(new Hal_Link('/dogs?q={text}', 'search'));
+$dogs[1] = new Hal_Resource('/dogs/1');
$dogs[1]->setData(array('id' => '1', 'name' => 'tiber', 'color' => 'black'));
-$dogs[2] = new Zircote_Hal_Resource('/dogs/2');
+$dogs[2] = new Hal_Resource('/dogs/2');
$dogs[2]->setData(array('id' => '2', 'name' => 'sally', 'color' => 'white'));
-$dogs[3] = new Zircote_Hal_Resource('/dogs/3');
+$dogs[3] = new Hal_Resource('/dogs/3');
$dogs[3]->setData(array('id' => '3', 'name' => 'fido', 'color' => 'gray'));
/* Add the embedded resources */
foreach ($dogs as $dog) {
- $parent->setEmbedded('dogs', $dog);
+ $parent->setEmbedded('dog', $dog);
}
echo Zend_Json::prettyPrint($parent);
```
@@ -35,7 +37,7 @@ echo Zend_Json::prettyPrint($parent);
}
},
"_embedded":{
- "dogs":[
+ "dog":[
{
"_links":{
"self":{
@@ -81,17 +83,17 @@ echo $parent->getXML()->asXML();
<?xml version="1.0"?>
<resource href="/dogs">
<link href="/dogs?q={text}" rel="search" />
- <resource href="/dogs/1" rel="dogs">
+ <resource href="/dogs/1" rel="dog">
<id>1</id>
<name>tiber</name>
<color>black</color>
</resource>
- <resource href="/dogs/2" rel="dogs">
+ <resource href="/dogs/2" rel="dog">
<id>2</id>
<name>sally</name>
<color>white</color>
</resource>
- <resource href="/dogs/3" rel="dogs">
+ <resource href="/dogs/3" rel="dog">
<id>3</id>
<name>fido</name>
<color>gray</color>
View
7 tests/bootstrap.php
@@ -20,7 +20,6 @@
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../'));
-echo APPLICATION_PATH;
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
@@ -31,5 +30,7 @@
get_include_path(),
)));
-require_once 'Zend/Loader/Autoloader.php';
-Zend_Loader_Autoloader::getInstance()->registerNamespace('Zircote');
+
+require_once 'Hal/AbstractHal.php';
+require_once 'Hal/Resource.php';
+require_once 'Hal/Link.php';
View
61 tests/library/Zircote/Hal/LinkTest.php → tests/library/Hal/LinkTest.php
@@ -15,9 +15,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- * Zircote_Hal_Link test case.
+ * Hal_Link test case.
*/
-class Zircote_Hal_LinkTest extends PHPUnit_Framework_TestCase
+class Hal_LinkTest extends PHPUnit_Framework_TestCase
{
/**
* Prepares the environment before running a test.
@@ -34,22 +34,21 @@ protected function tearDown ()
parent::tearDown();
}
public function testXML()
- {
-
- $parentRes = new Zircote_Hal_Resource('/orders');
+ {
+ $parentRes = new Hal_Resource('/orders');
$parentRes->setLink(
- new Zircote_Hal_Link('/orders?page=2', 'next')
+ new Hal_Link('/orders?page=2', 'next')
)->setLink(
- new Zircote_Hal_Link('/orders?id={order_id}', 'search')
+ new Hal_Link('/orders?id={order_id}', 'search')
);
$data = array(
'total' => '30.00','currency' => 'USD',
'status' => 'shipped','placed' => '2011-01-16',
);
- $embedded1 = new Zircote_Hal_Resource('/orders/123');
+ $embedded1 = new Hal_Resource('/orders/123');
$embedded1->setData($data)
->setLink(
- new Zircote_Hal_Link(
+ new Hal_Link(
'/customer/bob','customer','Bob Jones <bob@jones.com>'
)
);
@@ -57,7 +56,7 @@ public function testXML()
array('sku' => 'ABC123','quantity' => 2,'price' => '9.50'),
array('sku' => 'GFZ111','quantity' => 1,'price' => '11.00')
);
- $basket = new Zircote_Hal_Resource('/orders/123/basket');
+ $basket = new Hal_Resource('/orders/123/basket');
$basket->setData('items', $basketItems);
$embedded1->setEmbedded('basket', $basket, true);
//////////////////////////////////////////////
@@ -65,10 +64,10 @@ public function testXML()
'total' => '20.00','currency' => 'USD',
'status' => 'processing','placed' => '2011-01-16',
);
- $embedded2 = new Zircote_Hal_Resource('/orders/124');
+ $embedded2 = new Hal_Resource('/orders/124');
$embedded2->setData($data)
->setLink(
- new Zircote_Hal_Link(
+ new Hal_Link(
'/customer/jen','customer','Jen Harris <jen@internet.com>'
)
);
@@ -77,7 +76,7 @@ public function testXML()
array('sku' => 'KLM222','quantity' => 1,'price' => "9.00"),
array('sku' => 'HHI50','quantity' => 1,'price' => "11.00")
);
- $basket2 = new Zircote_Hal_Resource('/orders/124/basket');
+ $basket2 = new Hal_Resource('/orders/124/basket');
$basket2->setData('items', $basketItems);
$embedded2->setEmbedded('basket', $basket2, true);
@@ -93,20 +92,20 @@ public function testXML()
}
public function testBuild()
{
- $parentRes = new Zircote_Hal_Resource('/orders');
+ $parentRes = new Hal_Resource('/orders');
$parentRes->setLink(
- new Zircote_Hal_Link('/orders?page=2', 'next')
+ new Hal_Link('/orders?page=2', 'next')
)->setLink(
- new Zircote_Hal_Link('/orders?id={order_id}', 'search')
+ new Hal_Link('/orders?id={order_id}', 'search')
);
$data = array(
'total' => '30.00','currency' => 'USD',
'status' => 'shipped','placed' => '2011-01-16',
);
- $embedded1 = new Zircote_Hal_Resource('/orders/123');
+ $embedded1 = new Hal_Resource('/orders/123');
$embedded1->setData($data)
->setLink(
- new Zircote_Hal_Link(
+ new Hal_Link(
'/customer/bob','customer','Bob Jones <bob@jones.com>'
)
);
@@ -114,7 +113,7 @@ public function testBuild()
array('sku' => 'ABC123','quantity' => 2,'price' => '9.50'),
array('sku' => 'GFZ111','quantity' => 1,'price' => '11.00')
);
- $basket = new Zircote_Hal_Resource('/orders/123/basket');
+ $basket = new Hal_Resource('/orders/123/basket');
$basket->setData('items', $basketItems);
$embedded1->setEmbedded('basket', $basket, true);
//////////////////////////////////////////////
@@ -122,10 +121,10 @@ public function testBuild()
'total' => '20.00','currency' => 'USD',
'status' => 'processing','placed' => '2011-01-16',
);
- $embedded2 = new Zircote_Hal_Resource('/orders/124');
+ $embedded2 = new Hal_Resource('/orders/124');
$embedded2->setData($data)
->setLink(
- new Zircote_Hal_Link(
+ new Hal_Link(
'/customer/jen','customer','Jen Harris <jen@internet.com>'
)
);
@@ -134,14 +133,14 @@ public function testBuild()
array('sku' => 'KLM222','quantity' => 1,'price' => "9.00"),
array('sku' => 'HHI50','quantity' => 1,'price' => "11.00")
);
- $basket2 = new Zircote_Hal_Resource('/orders/124/basket');
+ $basket2 = new Hal_Resource('/orders/124/basket');
$basket2->setData('items', $basketItems);
$embedded2->setEmbedded('basket', $basket2, true);
$parentRes
->setEmbedded('order', $embedded1)
->setEmbedded('order', $embedded2);
- $actual = Zend_Json::decode($parentRes);
+ $actual = json_decode($parentRes);
$JSON =<<<EOF
{
"_links": {
@@ -210,7 +209,7 @@ public function testBuild()
}
}
EOF;
- $expected = Zend_Json::decode($JSON);
+ $expected = json_decode($JSON);
$this->assertEquals($expected, $actual);
}
/**
@@ -218,18 +217,16 @@ public function testBuild()
*/
public function testSimple()
{
- $parent = new Zircote_Hal_Resource('/dogs');
+ $parent = new Hal_Resource('/dogs');
/* Add any relevent links */
- $parent->setLink(new Zircote_Hal_Link('/dogs?q={text}', 'search'));
- $dogs[1] = new Zircote_Hal_Resource('/dogs/1');
+ $parent->setLink(new Hal_Link('/dogs?q={text}', 'search'));
+ $dogs[1] = new Hal_Resource('/dogs/1');
$dogs[1]->setData(array('id' => '1', 'name' => 'tiber', 'color' => 'black'));
- $dogs[2] = new Zircote_Hal_Resource('/dogs/2');
- $dogs[2]->setData(array('id' => '2', 'name' => 'sally', 'color' => 'white'));
- $dogs[3] = new Zircote_Hal_Resource('/dogs/3');
- $dogs[3]->setData(array('id' => '3', 'name' => 'fido', 'color' => 'gray'));
+ $dogs[2] = new Hal_Resource('/dogs/2',array('id' => '2', 'name' => 'sally', 'color' => 'white'));
+ $dogs[3] = new Hal_Resource('/dogs/3',array('id' => '3', 'name' => 'fido', 'color' => 'gray'));
/* Add the embedded resources */
foreach ($dogs as $dog) {
- $parent->setEmbedded('dogs', $dog);
+ $parent->setEmbedded('dog', $dog);
}
echo $parent->getXML()->asXML();
}
View
4 tests/phpunit.xml
@@ -1,6 +1,6 @@
-<phpunit bootstrap="./bootstrap.php">
+<phpunit bootstrap="bootstrap.php">
<testsuite name="Library Test Suite">
- <directory>./library</directory>
+ <directory>library</directory>
</testsuite>
<filter>
Please sign in to comment.
Something went wrong with that request. Please try again.