diff --git a/library/Respect/Rest/Routines/Rel.php b/library/Respect/Rest/Routines/Rel.php index c0bcbf0..7cf26fe 100644 --- a/library/Respect/Rest/Routines/Rel.php +++ b/library/Respect/Rest/Routines/Rel.php @@ -2,9 +2,29 @@ namespace Respect\Rest\Routines; -use Respect\Rest\Routes\AbstractSyncedRoutine; +use ArrayObject; use Respect\Rest\Request; -class Rel extends AbstractCallbackMediator implements ProxyableThrough +class Rel extends ArrayObject implements Routinable, ProxyableThrough { + public function __construct(array $list) + { + $this->setFlags(self::ARRAY_AS_PROPS); + $this->exchangeArray($list); + } + + public function through(Request $request, $params) + { + $rels = $this; + return function ($data) use ($rels) { + + if (!isset($data['links'])) { + $data['links'] = array(); + } + + $data['links'] = array_merge_recursive($data['links'], $rels->getArrayCopy()); + + return $data; + }; + } } diff --git a/tests/library/Respect/Rest/Routines/RelTest.php b/tests/library/Respect/Rest/Routines/RelTest.php index d7b6c91..d6b4091 100644 --- a/tests/library/Respect/Rest/Routines/RelTest.php +++ b/tests/library/Respect/Rest/Routines/RelTest.php @@ -10,4 +10,61 @@ */ class RelTest extends PHPUnit_Framework_TestCase { + public function testSimpleTextRelationPassesThroughData() + { + $router = new \Respect\Rest\Router; + $router->get('/', function() { + return array(); + })->rel(array( + 'item' => '/foo' + )); + $response = $router->dispatch('GET', '/')->response(); + + $this->assertArrayHasKey( + 'links', + $response, + 'An array of links should be returned when a rel succeeds' + ); + + $this->assertArrayHasKey( + 'item', + $response['links'], + 'The links array should contain the related link' + ); + + $this->assertContains( + '/foo', + $response['links']['item'], + 'The related link key should contain the specified rel value' + ); + } + + public function testMultipleTextRelationPassesThroughData() + { + $router = new \Respect\Rest\Router; + $router->get('/', function() { + return array(); + })->rel(array( + 'item' => array('/foo', '/bar') + )); + $response = $router->dispatch('GET', '/')->response(); + + $this->assertCount( + 2, + $response['links']['item'], + 'The related link key should contain the exact number of related items' + ); + + $this->assertContains( + '/foo', + $response['links']['item'], + 'The related link key should contain the specified rel value' + ); + + $this->assertContains( + '/bar', + $response['links']['item'], + 'The related link key should contain the specified rel value' + ); + } } \ No newline at end of file