Skip to content

Commit

Permalink
refactored for php 5.3 namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
zircote committed May 6, 2012
1 parent 4c06703 commit f73614b
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 93 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.1.0
0.2.0
4 changes: 2 additions & 2 deletions composer.json
@@ -1,7 +1,7 @@
{
"name": "zircote/Hal",
"type": "library",
"version": "0.1.0",
"version": "0.2.0",
"time" : "2012-04-01",
"license" : "Apache2",
"description": "A PHP implementation of HAL http://stateless.co/hal_specification.html",
Expand All @@ -15,7 +15,7 @@
}
],
"require": {
"php": ">=5.2.4"
"php": ">=5.3.3"
},
"autoload": {
"psr-0": {"Hal": "library"}
Expand Down
18 changes: 13 additions & 5 deletions library/Hal/AbstractHal.php
@@ -1,4 +1,12 @@
<?php
/**
*
* @category Hal
* @package Hal
*/
namespace Hal;
use SimpleXMLElement,
Hal\Link;
/**
*
* @license http://www.apache.org/licenses/LICENSE-2.0
Expand All @@ -16,12 +24,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* @category Hal
* @package Hal
*
*
*/
abstract class Hal_AbstractHal
abstract class AbstractHal
{
/**
*
Expand All @@ -30,10 +38,10 @@ abstract class Hal_AbstractHal
protected $_xml;
/**
* @param SimpleXMLElement $xml
* @param Hal_Link $link
* @return Hal_AbstractHal
* @param Link $link
* @return AbstractHal
*/
public function setXMLAttributes(SimpleXMLElement $xml, Hal_Link $link)
public function setXMLAttributes(SimpleXMLElement $xml, Link $link)
{
$xml->addAttribute('href', $link->getHref());
if($link->getRel() && $link->getRel() !== 'self'){
Expand Down
23 changes: 14 additions & 9 deletions library/Hal/Link.php
@@ -1,4 +1,12 @@
<?php
/**
*
* @category Hal
* @package Hal
*/
namespace Hal;
use SimpleXMLElement,
Hal\Resource;
/**
*
* @license http://www.apache.org/licenses/LICENSE-2.0
Expand All @@ -18,7 +26,7 @@
*
* @package Hal
*/
class Hal_Link extends Hal_AbstractHal
class Link extends AbstractHal
{
/**
* For labeling the destination of a link with a human-readable identifier.
Expand Down Expand Up @@ -116,7 +124,7 @@ public function getHreflang ()
}
/**
* @param string $rel
* @return Hal_Link
* @return Link
*/
public function setRel ($rel)
{
Expand All @@ -125,7 +133,7 @@ public function setRel ($rel)
}
/**
* @param string $href
* @return Hal_Link
* @return Link
*/
public function setHref ($href)
{
Expand All @@ -135,7 +143,7 @@ public function setHref ($href)
/**
*
* @param string $name
* @return Hal_Link
* @return Link
*/
public function setName($name)
{
Expand All @@ -144,7 +152,7 @@ public function setName($name)
}
/**
* @param string $title
* @return Hal_Link
* @return Link
*/
public function setTitle ($title)
{
Expand All @@ -154,7 +162,7 @@ public function setTitle ($title)

/**
* @param string $hreflang
* @return Hal_Link
* @return Link
*/
public function setHreflang ($hreflang)
{
Expand All @@ -174,9 +182,6 @@ public function toArray()
if($this->getTitle()){
$link['title'] = $this->getTitle();
}
if($this->getRel()){
// $link['rel'] = $this->getRel();
}
if($this->getName()){
$link['name'] = $this->getName();
}
Expand Down
60 changes: 35 additions & 25 deletions library/Hal/Resource.php
@@ -1,4 +1,13 @@
<?php
/**
*
* @category Hal
* @package Hal
*/
namespace Hal;
use Hal\Link,
Hal\AbstractHal,
SimpleXMLElement;
/**
* @license http://www.apache.org/licenses/LICENSE-2.0
* Copyright [2012] [Robert Allen]
Expand All @@ -15,13 +24,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @category Hal
* @package Hal
*
*/
class Hal_Resource extends Hal_AbstractHal
class Resource extends AbstractHal
{
/**
* Internal storage of `Hal_Link` objects
* Internal storage of `Link` objects
* @var array
*/
protected $_links = array();
Expand All @@ -31,7 +41,7 @@ class Hal_Resource extends Hal_AbstractHal
*/
protected $_data = array();
/**
* Internal storage of `Hal_Resource` objects
* Internal storage of `Resource` objects
* @var array
*/
protected $_embedded = array();
Expand All @@ -43,12 +53,12 @@ class Hal_Resource extends Hal_AbstractHal
public function __construct($href, array $data = array(), $title = null, $name = null, $hreflang = null)
{
$this->setLink(
new Hal_Link($href, 'self', $title, $name, $hreflang)
new Link($href, 'self', $title, $name, $hreflang)
);
$this->setData($data);
}
/**
* @return Hal_Link
* @return Link
*/
public function getSelf()
{
Expand All @@ -64,15 +74,15 @@ public function getLinks()
/**
* Add a link to the resource.
*
* Per the JSON-HAL specification, a link relation can reference a
* single link or an array of links. By default, two or more links with
* Per the JSON-HAL specification, a link relation can reference a
* single link or an array of links. By default, two or more links with
* the same relation will be treated as an array of links. The $singular
* flag will force links with the same relation to be overwritten.
*
* @param Hal_Link $link
* @return Hal_Resource
* @param Link $link
* @return Resource
*/
public function setLink(Hal_Link $link, $singular=false)
public function setLink(Link $link, $singular=false)
{
$rel = $link->getRel();

Expand All @@ -91,7 +101,7 @@ public function setLink(Hal_Link $link, $singular=false)
/**
*
* @param array $data
* @return Hal_Resource
* @return Resource
*/
public function setData($rel, $data = null)
{
Expand All @@ -106,10 +116,10 @@ public function setData($rel, $data = null)
}
/**
*
* @param Hal_Resource $resource
* @return Hal_Resource
* @param Resource $resource
* @return Resource
*/
public function setEmbedded($rel,Hal_Resource $resource, $singular = false)
public function setEmbedded($rel,Resource $resource, $singular = false)
{
if($singular){
$this->_embedded[$rel] = $resource;
Expand Down Expand Up @@ -154,8 +164,8 @@ protected function _recurseEmbedded($embeded)
return $result;
}
/**
*
* @param mixed $links
*
* @param mixed $links
*/
protected function _recurseLinks($links)
{
Expand Down Expand Up @@ -210,25 +220,25 @@ public function getXML($xml = null)
*/
protected function _getEmbedded($embedded, $_rel = null)
{
/* @var $embed Hal_Resource */
/* @var $embed Resource */
foreach ($embedded as $rel => $embed) {
if($embed instanceof Hal_Resource){
if($embed instanceof Resource){
$rel = is_numeric($rel) ? $_rel : $rel;
$this->_getEmbRes($embed)->addAttribute('rel', $rel);
} else {
$this->_getEmbedded($embed,$rel);
}
}
}
protected function _getEmbRes(Hal_Resource $embed)
protected function _getEmbRes(Resource $embed)
{
$resource = $this->_xml->addChild('resource');
return $embed->getXML($resource);
}
/**
*
* @param SimpleXMLElement $xml
* @return Hal_Resource
* @return Resource
*/
public function setXML(SimpleXMLElement $xml)
{
Expand Down Expand Up @@ -271,20 +281,20 @@ protected function _addData(SimpleXMLElement $xml, $data, $key = null)
}
/**
*
* @param Hal_Link $link
* @param Link $link
*/
protected function _addLinks(Hal_Link $link)
protected function _addLinks(Link $link)
{
if($link->getRel() != 'self' && !is_numeric($link->getRel())){
$this->_addLink($link);
}
}
/**
*
* @param Hal_Link $link
* @return Hal_Resource
* @param Link $link
* @return Resource
*/
protected function _addLink(Hal_Link $link)
protected function _addLink(Link $link)
{
$this->setXMLAttributes($this->_xml->addChild('link'), $link);
return $this;
Expand Down
16 changes: 8 additions & 8 deletions package.xml
Expand Up @@ -35,11 +35,11 @@ HAL has two main components: Resources and Links.
<email>zircote@gmail.com</email>
<active>yes</active>
</lead>
<date>2012-04-07</date>
<time>13:27:51</time>
<date>2012-05-06</date>
<time>09:18:44</time>
<version>
<release>0.1.0</release>
<api>0.1.0</api>
<release>0.2.0</release>
<api>0.2.0</api>
</version>
<stability>
<release>beta</release>
Expand Down Expand Up @@ -94,7 +94,7 @@ This is an alpha release, see readme.md for examples.
<dependencies>
<required>
<php>
<min>5.2.1</min>
<min>5.3.3</min>
</php>
<pearinstaller>
<min>1.7.0</min>
Expand All @@ -112,14 +112,14 @@ This is an alpha release, see readme.md for examples.
<changelog>
<release>
<version>
<release>0.1.0</release>
<api>0.1.0</api>
<release>0.2.0</release>
<api>0.2.0</api>
</version>
<stability>
<release>beta</release>
<api>beta</api>
</stability>
<date>2012-04-07</date>
<date>2012-05-06</date>
<license uri="http://www.apache.org/licenses/LICENSE-2.0">Apache 2.0</license>
<notes>
This is an alpha release, see readme.md for examples.
Expand Down
16 changes: 9 additions & 7 deletions readme.md
@@ -1,34 +1,36 @@
# Hal

* [HAL Specification](http://stateless.co/hal_specification.html)
* [Hal Specification on Github](https://github.com/mikekelly/hal_specification)
* [HAL Specification](http://stateless.co/\Hal\specification.html)
* [Hal Specification on Github](https://github.com/mikekelly/\Hal\specification)
* [JSON Linking With HAL](http://blog.stateless.co/post/13296666138/json-linking-with-hal)
* [Linking In Json](http://www.mnot.net/blog/2011/11/25/linking_in_json)
* [Examples of HAL](https://gist.github.com/2289546)
* [HAL on Google Groups](https://groups.google.com/d/forum/hal-discuss)

```php
<?php
use Hal\Resource,
Hal\Link;
/* Create a new Resource Parent */
$parent = new Hal_Resource('/dogs');
$parent = new Resource('/dogs');
/* Add any relevent links */
$parent->setLink(new Hal_Link('/dogs?q={text}', 'search'));
$dogs[1] = new Hal_Resource('/dogs/1');
$parent->setLink(new Link('/dogs?q={text}', 'search'));
$dogs[1] = new Resource('/dogs/1');
$dogs[1]->setData(
array(
'id' => '1',
'name' => 'tiber',
'color' => 'black'
)
);
$dogs[2] = new Hal_Resource(
$dogs[2] = new Resource(
'/dogs/2',array(
'id' => '2',
'name' => 'sally',
'color' => 'white'
)
);
$dogs[3] = new Hal_Resource(
$dogs[3] = new Resource(
'/dogs/3',array(
'id' => '3',
'name' => 'fido',
Expand Down

0 comments on commit f73614b

Please sign in to comment.