Skip to content

Commit d044530

Browse files
authored
Merge pull request #362 from Farrser/doctrine-paginator-adapter
Add DoctrinePaginatorAdapter to interface with Doctrine pagination
2 parents f3aacbd + 4b7dd0b commit d044530

File tree

3 files changed

+183
-1
lines changed

3 files changed

+183
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"illuminate/contracts": "~5.0",
2727
"squizlabs/php_codesniffer": "~1.5",
2828
"pagerfanta/pagerfanta": "~1.0.0",
29-
"zendframework/zend-paginator":"~2.3"
29+
"zendframework/zend-paginator":"~2.3",
30+
"doctrine/orm": "^2.5"
3031
},
3132
"suggest": {
3233
"illuminate/pagination": "The Illuminate Pagination component.",
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
/*
3+
* This file is part of the League\Fractal package.
4+
*
5+
* (c) Phil Sturgeon <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace League\Fractal\Pagination;
12+
13+
use Doctrine\ORM\Tools\Pagination\Paginator;
14+
15+
/**
16+
* A paginator adapter for doctrine pagination.
17+
*
18+
* @author Fraser Stockley <[email protected]>
19+
*/
20+
class DoctrinePaginatorAdapter implements PaginatorInterface
21+
{
22+
/**
23+
* The paginator instance.
24+
* @var Paginator
25+
*/
26+
private $paginator;
27+
28+
/**
29+
* The route generator.
30+
*
31+
* @var callable
32+
*/
33+
private $routeGenerator;
34+
35+
/**
36+
* Create a new DoctrinePaginatorAdapter.
37+
* @param Paginator $paginator
38+
* @param callable $routeGenerator
39+
*
40+
*/
41+
public function __construct(Paginator $paginator, callable $routeGenerator)
42+
{
43+
$this->paginator = $paginator;
44+
$this->routeGenerator = $routeGenerator;
45+
}
46+
47+
/**
48+
* Get the current page.
49+
*
50+
* @return int
51+
*/
52+
public function getCurrentPage()
53+
{
54+
return ($this->paginator->getQuery()->getFirstResult() / $this->paginator->getQuery()->getMaxResults()) + 1;
55+
}
56+
57+
/**
58+
* Get the last page.
59+
*
60+
* @return int
61+
*/
62+
public function getLastPage()
63+
{
64+
return (int) ceil($this->getTotal() / $this->paginator->getQuery()->getMaxResults());
65+
}
66+
67+
/**
68+
* Get the total.
69+
*
70+
* @return int
71+
*/
72+
public function getTotal()
73+
{
74+
return count($this->paginator);
75+
}
76+
77+
/**
78+
* Get the count.
79+
*
80+
* @return int
81+
*/
82+
public function getCount()
83+
{
84+
return $this->paginator->getIterator()->count();
85+
}
86+
87+
/**
88+
* Get the number per page.
89+
*
90+
* @return int
91+
*/
92+
public function getPerPage()
93+
{
94+
return $this->paginator->getQuery()->getMaxResults();
95+
}
96+
97+
/**
98+
* Get the url for the given page.
99+
*
100+
* @param int $page
101+
*
102+
* @return string
103+
*/
104+
public function getUrl($page)
105+
{
106+
return call_user_func($this->getRouteGenerator(), $page);
107+
}
108+
109+
/**
110+
* Get the the route generator.
111+
*
112+
* @return callable
113+
*/
114+
private function getRouteGenerator()
115+
{
116+
return $this->routeGenerator;
117+
}
118+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
namespace League\Fractal\Test\Pagination;
3+
4+
use Doctrine\ORM\Query;
5+
use League\Fractal\Pagination\DoctrinePaginatorAdapter;
6+
use Mockery;
7+
8+
class DoctrinePaginatorAdapterTest extends \PHPUnit_Framework_TestCase
9+
{
10+
public function testPaginationAdapter()
11+
{
12+
$total = 50;
13+
$count = 5;
14+
$perPage = 5;
15+
$currentPage = 2;
16+
$lastPage = 10;
17+
18+
//Mock the doctrine paginator
19+
$paginator = Mockery::mock('Doctrine\ORM\Tools\Pagination\Paginator')->makePartial();
20+
$paginator->shouldReceive('count')->andReturn($total);
21+
22+
23+
//Mock the query that the paginator is acting on
24+
$query = Mockery::mock('Doctrine\ORM\AbstractQuery');
25+
$query->shouldReceive('getFirstResult')->andReturn(($currentPage - 1) * $perPage);
26+
$query->shouldReceive('getMaxResults')->andReturn($perPage);
27+
$paginator->shouldReceive('getQuery')->andReturn($query);
28+
29+
//Mock the iterator of the paginator
30+
$iterator = Mockery::mock('IteratorAggregate');
31+
$iterator->shouldReceive('count')->andReturn($count);
32+
$paginator->shouldReceive('getIterator')->andReturn($iterator);
33+
34+
35+
$adapter = new DoctrinePaginatorAdapter($paginator, function ($page) {
36+
return 'http://example.com/foo?page='.$page;
37+
});
38+
39+
$this->assertInstanceOf(
40+
'League\Fractal\Pagination\PaginatorInterface',
41+
$adapter
42+
);
43+
44+
$this->assertSame($currentPage, $adapter->getCurrentPage());
45+
$this->assertSame($lastPage, $adapter->getLastPage());
46+
$this->assertSame($count, $adapter->getCount());
47+
$this->assertSame($total, $adapter->getTotal());
48+
$this->assertSame($perPage, $adapter->getPerPage());
49+
$this->assertSame(
50+
'http://example.com/foo?page=1',
51+
$adapter->getUrl(1)
52+
);
53+
$this->assertSame(
54+
'http://example.com/foo?page=3',
55+
$adapter->getUrl(3)
56+
);
57+
}
58+
59+
public function tearDown()
60+
{
61+
Mockery::close();
62+
}
63+
}

0 commit comments

Comments
 (0)