Skip to content

Commit

Permalink
Implement Linkable.
Browse files Browse the repository at this point in the history
  • Loading branch information
romeOz committed Mar 17, 2015
1 parent 1a337e1 commit b7e26cb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<file>./src/Behavior.php</file>
<file>./src/ComponentsTrait.php</file>
<file>./src/ModelEvent.php</file>
<file>./src/Linkable.php</file>
</blacklist>
</filter>
<logging>
Expand Down
7 changes: 4 additions & 3 deletions src/ArrayableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace rock\components;

use rock\helpers\ArrayHelper;
use rock\helpers\Link;
use rock\helpers\ObjectHelper;

/**
Expand Down Expand Up @@ -112,9 +113,9 @@ public function toArray(array $only = [], array $exclude = [], array $expand = [
$data[$field] = is_string($definition) ? $this->$definition : call_user_func($definition, $field, $this);
}

// if ($this instanceof Linkable) {
// $data['_links'] = Link::serialize($this->getLinks());
// }
if ($this instanceof Linkable) {
$data['_links'] = Link::serialize($this->getLinks());
}

return $recursive ? static::convert($data) : $data;
}
Expand Down
34 changes: 34 additions & 0 deletions src/Linkable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace rock\components;


/**
* Linkable is the interface that should be implemented by classes that typically represent locatable resources.
*/
interface Linkable
{
/**
* Returns a list of links.
*
* Each link is either a URI or a {@see \rock\helpers\Link} object. The return value of this method should
* be an array whose keys are the relation names and values the corresponding links.
*
* If a relation name corresponds to multiple links, use an array to represent them.
*
* For example,
*
* ```php
* [
* 'self' => 'http://example.com/users/1',
* 'friends' => [
* 'http://example.com/users/2',
* 'http://example.com/users/3',
* ],
* ]
* ```
*
* @return array the links
*/
public function getLinks();
}

0 comments on commit b7e26cb

Please sign in to comment.