Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions Relay/Connection/Paginator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Overblog\GraphQLBundle\Relay\Connection;

use Overblog\GraphQLBundle\Definition\Argument;
use Overblog\GraphQLBundle\Relay\Connection\Output\Connection;
use Overblog\GraphQLBundle\Relay\Connection\Output\ConnectionBuilder;

class Paginator
{
/**
* @var callable
*/
private $fetcher;

/**
* @param callable $fetcher
*/
public function __construct(callable $fetcher)
{
$this->fetcher = $fetcher;
}

/**
* @param Argument|array $args
* @param int|callable $total
*
* @return Connection
*/
public function backward($args, $total)
{
$args = $this->protectArgs($args);
$limit = $args['last'];
$offset = max(0, ConnectionBuilder::getOffsetWithDefault($args['before'], $total) - $limit);

$entities = call_user_func($this->fetcher, $offset, $limit);

return ConnectionBuilder::connectionFromArraySlice($entities, $args, [
'sliceStart' => $offset,
'arrayLength' => $total,
]);
}

/**
* @param Argument|array $args
*
* @return Connection
*/
public function forward($args)
{
$args = $this->protectArgs($args);
$limit = $args['first'];
$offset = ConnectionBuilder::getOffsetWithDefault($args['after'], 0);

// The extra fetched element is here to determine if there is a next page.
$entities = call_user_func($this->fetcher, $offset, $limit + 1);

return ConnectionBuilder::connectionFromArraySlice($entities, $args, [
'sliceStart' => $offset,
'arrayLength' => $offset + count($entities),
]);
}

/**
* @param Argument|array $args
* @param int|callable $total
*
* @return Connection
*/
public function auto($args, $total)
{
$args = $this->protectArgs($args);

if ($args['last']) {
return $this->backward($args, is_callable($total) ? call_user_func($total) : $total);
} else {
return $this->forward($args);
}
}

/**
* @param Argument|array $args
*
* @return Argument
*/
private function protectArgs($args)
Copy link
Contributor

Choose a reason for hiding this comment

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

toArgument would be a better name

Copy link
Contributor

Choose a reason for hiding this comment

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

private method we can renames it easily before next release without BC :trollface:

{
return $args instanceof Argument ? $args : new Argument($args);
}
}
97 changes: 97 additions & 0 deletions Tests/Relay/Connection/PaginatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Overblog\GraphQLBundle\Tests\Relay\Connection;

use Overblog\GraphQLBundle\Definition\Argument;
use Overblog\GraphQLBundle\Relay\Connection\Paginator;

class PaginatorTest extends \PHPUnit_Framework_TestCase
{
public function testForward()
{
$paginator = new Paginator(function ($offset, $limit) {
$this->assertSame(0, $offset);
$this->assertSame(6, $limit); // Includes the extra element to check if next page is available

return array_fill(0, 6, 'item');
});

$this->assertCount(5, $paginator->forward(new Argument(['first' => 5]))->edges);
}

public function testForwardAfter()
{
$paginator = new Paginator(function ($offset, $limit) {
$this->assertSame(5, $offset);
$this->assertSame(6, $limit); // Includes the extra element to check if next page is available

return array_fill(0, 6, 'item');
});

$this->assertCount(5, $paginator->forward(new Argument(['first' => 5, 'after' => base64_encode('arrayconnection:5') ]))->edges);
}

public function testBackward()
{
$paginator = new Paginator(function ($offset, $limit) {
$this->assertSame(5, $offset);
$this->assertSame(5, $limit);

return array_fill(0, 5, 'item');
});

$this->assertCount(5, $paginator->backward(new Argument(['last' => 5]), 10)->edges);
}

public function testBackwardBefore()
{
$paginator = new Paginator(function ($offset, $limit) {
$this->assertSame(0, $offset);
$this->assertSame(5, $limit);

return array_fill(0, 5, 'item');
});

$this->assertCount(5, $paginator->backward(new Argument(['last' => 5, 'before' => base64_encode('arrayconnection:5')]), 10)->edges);
}

public function testAuto()
{
// Backward
$paginator = new Paginator(function ($offset, $limit) {
$this->assertSame(5, $offset);
$this->assertSame(5, $limit);

return array_fill(0, 5, 'item');
});

$this->assertCount(5, $paginator->auto(new Argument(['last' => 5]), 10)->edges);

// Forward
$paginator = new Paginator(function ($offset, $limit) {
$this->assertSame(0, $offset);
$this->assertSame(6, $limit); // Includes the extra element to check if next page is available

return array_fill(0, 5, 'item');
});

$this->assertCount(5, $paginator->auto(new Argument(['first' => 5]), 10)->edges);

// Backward + callable
$paginator = new Paginator(function ($offset, $limit) {
$this->assertSame(5, $offset);
$this->assertSame(5, $limit);

return array_fill(0, 5, 'item');
});

$countCalled = false;
$result = $paginator->auto(new Argument(['last' => 5]), function () use (&$countCalled) {
$countCalled = true;
return 10;
});

$this->assertTrue($countCalled);
$this->assertCount(5, $result->edges);
}
}