Skip to content

Commit

Permalink
Implemented simple, string-based link relation types on routing
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Gaigalas committed Aug 27, 2012
1 parent b0ccd89 commit 67915d9
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
24 changes: 22 additions & 2 deletions library/Respect/Rest/Routines/Rel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
}
57 changes: 57 additions & 0 deletions tests/library/Respect/Rest/Routines/RelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')

This comment has been minimized.

Copy link
@nickl-

nickl- Aug 29, 2012

Member

How about :

<?php

    $router->get('/', function() {
        return array();
    })->rel(array(
        'item' => array(
                'href' => '/foo',
                'title' => 'Foo guy',
                'type' => 'application/json',
                'hreflang' => 'en-US',
            ),
        'self' => array(
                'template' => '/bar/{:id}',
                'title' => 'Foo guy',
                'type' => 'application/json',
            ),
        )
    );

I'm not quite feeling the list of urls? Would I be on the right track, or have you something else in mind.

Awesome stretch you covered, well done! =)

This comment has been minimized.

Copy link
@alganet

alganet Aug 29, 2012

Member

Maybe something equivalent to a new SimpleXmlElemnt('<link rel="item" href="/foo"' etc=etc); so we can query it uniformly. Not sure about SimpleXml thought, even it seems to be the best PHP native API to represent a link.

This comment has been minimized.

Copy link
@nickl-

nickl- Aug 29, 2012

Member

Anchors and links can be represented in xml, 2 out of the 3 standard types, but they are not difficult to produce, is this enough motivation to store and transport them as such. What about the overhead, or is using the PHP DOM only expensive on my well being.

You have a point about native support and I guess it's not too much to pay for header links. Coming te think of it, are we not blurring the lines now.

We need to be able to define the data and the render it, the type of link comes in at accept. The question is when do we want to define the meta information. I seem to think there's value in supplying it once and have it rendered in many ways.

Still great advance! I like it, a lot! =)

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

1 comment on commit 67915d9

@augustohp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't seen that. How did I miss that?!

Fucking awesome.

Please sign in to comment.