From 67fe3b2afe76f14e835cec8b8185b978dcd42c52 Mon Sep 17 00:00:00 2001 From: William Date: Tue, 2 Aug 2016 12:57:23 -0700 Subject: [PATCH] better field + tests --- src/ItemPaginator.php | 16 ++++++++++++++-- tests/ItemPaginatorTest.php | 22 +++++++++++----------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/ItemPaginator.php b/src/ItemPaginator.php index 7091c6c..ded7262 100644 --- a/src/ItemPaginator.php +++ b/src/ItemPaginator.php @@ -130,7 +130,7 @@ public function firstItem() return; } - return $this->items[0][$this->field]; + return $this->items[0][$this->getField()]; } /** @@ -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]; } /** diff --git a/tests/ItemPaginatorTest.php b/tests/ItemPaginatorTest.php index b1465cf..b0413c4 100644 --- a/tests/ItemPaginatorTest.php +++ b/tests/ItemPaginatorTest.php @@ -6,7 +6,7 @@ class ItemPaginatorTest extends BaseTestCase { - public function testThatFilterCanUseNl2Br() + public function testPaginator() { $limit = 2; @@ -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']); } }