Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Commit

Permalink
Merge pull request #6 from Askedio/dev
Browse files Browse the repository at this point in the history
better field + tests
  • Loading branch information
gcphost authored Aug 2, 2016
2 parents 3de10c0 + 67fe3b2 commit 04f724e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
16 changes: 14 additions & 2 deletions src/ItemPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function firstItem()
return;
}

return $this->items[0][$this->field];
return $this->items[0][$this->getField()];
}

/**
Expand All @@ -144,7 +144,19 @@ public function lastItem()
return;
}

return $this->items[count($this->items) - 1][$this->field];
return $this->items[count($this->items) - 1][$this->getField()];
}

protected function getField() {
if (empty($this->field)) {
return $this->field;
}

if (!preg_match('/\.(.*)/', $this->field, $matches)) {
return $this->field;
}

return $matches[1];
}

/**
Expand Down
22 changes: 11 additions & 11 deletions tests/ItemPaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class ItemPaginatorTest extends BaseTestCase
{
public function testThatFilterCanUseNl2Br()
public function testPaginator()
{
$limit = 2;

Expand Down Expand Up @@ -52,24 +52,24 @@ public function testThatFilterCanUseNl2Br()
$results = $paginated->toArray();

$this->assertInstanceOf('Askedio\ItemPaginator\ItemPaginator', $paginated);
$this->assertEquals($results['from'], 100);
$this->assertEquals($results['to'], 190);
$this->assertEquals($results['next_page_url'], 'http://localhost?from=190');
$this->assertEquals($results['prev_page_url'], null);
$this->assertEquals(100, $results['from']);
$this->assertEquals(190, $results['to']);
$this->assertEquals('http://localhost?from=190', $results['next_page_url']);
$this->assertEquals(null, $results['prev_page_url']);

$paginated = $user->itemPaginate($limit, ['*'], 'from', 100);

$results = $paginated->toArray();

$this->assertEquals($results['from'], 190);
$this->assertEquals($results['to'], 210);
$this->assertEquals($results['next_page_url'], 'http://localhost?from=210');
$this->assertEquals($results['prev_page_url'], 'http://localhost?from=190');
$this->assertEquals(190, $results['from'], 190);
$this->assertEquals(210, $results['to'], 210);
$this->assertEquals('http://localhost?from=210', $results['next_page_url']);
$this->assertEquals('http://localhost?from=190', $results['prev_page_url']);

$paginated = $user->itemPaginate($limit, ['*'], 'from', 10000);

$results = $paginated->toArray();
$this->assertEquals($results['from'], null);
$this->assertEquals($results['to'], null);
$this->assertEquals(null, $results['from']);
$this->assertEquals(null, $results['to']);
}
}

0 comments on commit 04f724e

Please sign in to comment.