8000 Merge pull request #15 from BigZ/fix/cast-collection · wizardstechnologies/php-rest-api@9fa61ac · GitHub
[go: up one dir, main page]

Skip to content

Commit 9fa61ac

Browse files< 8000 /span>
authored
Merge pull request #15 from BigZ/fix/cast-collection
fix: collection can be an instance of Collection
2 parents 3ca4696 + 06d1ad1 commit 9fa61ac

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

src/WizardsRest/Paginator/ArrayPagerfantaPaginator.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace WizardsRest\Paginator;
44

5+
use Doctrine\Common\Collections\Collection;
56
use League\Fractal\Pagination\PagerfantaPaginatorAdapter;
67
use League\Fractal\Pagination\PaginatorInterface as FractalPaginatorInterface;
78
use Pagerfanta\Adapter\ArrayAdapter;
@@ -10,13 +11,11 @@
1011
use Symfony\Component\Routing\RouterInterface;
1112
use WizardsRest\Parser\RestQueryParser;
1213

14+
/**
15+
* Paginate an array or an ArrayCollection using PagerFanta.
16+
*/
1317
class ArrayPagerfantaPaginator implements PaginatorInterface
1418
{
15-
/**
16-
* @var Pagerfanta|null
17-
*/
18-
private $paginator;
19-
2019
/**
2120
* @var RouterInterface
2221
*/
@@ -26,9 +25,13 @@ public function __construct(RouterInterface $router)
2625
{
2726
$this->router = $router;
2827
}
29-
28+
3029
private function getPaginator($collection, ServerRequestInterface $request)
3130
{
31+
if ($collection instanceof Collection) {
32+
$collection = $collection->toArray();
33+
}
34+
3235
$parameters = new RestQueryParser($request);
3336
$adapter = new ArrayAdapter($collection);
3437
$paginator = new Pagerfanta($adapter);

src/WizardsRest/Paginator/DoctrineOrmPagerFantaPaginator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Symfony\Component\Routing\RouterInterface;
1111
use WizardsRest\Parser\RestQueryParser;
1212

13+
/**
14+
* Paginates a Doctrine ORM Query or a QueryBuilder according to an http request.
15+
*/
1316
class DoctrineOrmPagerFantaPaginator implements PaginatorInterface
1417
{
1518
/**

src/WizardsRest/Paginator/PaginatorInterface.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,32 @@
55
use League\Fractal\Pagination\PaginatorInterface as FractalPaginatorInterface;
66
use Psr\Http\Message\ServerRequestInterface;
77

8+
/**
9+
* Paginate a collection of resources.
10+
* The Collection could be anything, as it is the job of the implementation to be able to select a slice from it.
11+
*
12+
* @package WizardsRest\Paginator
13+
*/
814
interface PaginatorInterface
915
{
16+
/**
17+
* Get a slice of the results according to an http request.
18+
*
19+
* @param \Traversable $collection
20+
* @param ServerRequestInterface $request
21+
*
22+
* @return mixed
23+
*/
1024
public function paginate($collection, ServerRequestInterface $request);
1125

26+
/**
27+
* Get the informations on the pagination (current page, total results...) according to the current collection
28+
* and an http request.
29+
*
30+
* @param \Traversable $collection
31+
* @param ServerRequestInterface $request
32+
*
33+
* @return FractalPaginatorInterface
34+
*/
1235
public function getPaginationAdapter($collection, ServerRequestInterface $request): FractalPaginatorInterface;
1336
}

tests/WizardsTest/Paginator/ArrayPagerfantaPaginatorTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace WizardsTest\Paginator;
44

5+
use Doctrine\Common\Collections\ArrayCollection;
56
use PHPUnit\Framework\TestCase;
67
use Symfony\Component\Routing\Router;
78
use WizardsRest\Paginator\ArrayPagerfantaPaginator;
@@ -35,4 +36,18 @@ public function testPaginate()
3536
);
3637
$this->assertEquals(5, count($paginator->paginate($collection, $request2)));
3738
}
39+
40+
public function testPaginateCollection()
41+
{
42+
$routerMock = $this->createMock(Router::class);
43+
$paginator = new ArrayPagerfantaPaginator($routerMock);
44+
$collection = new ArrayCollection([1, 2, 3, 4, 5]);
45+
$request = new ServerRequest(
46+
[],
47+
[],
48+
'/posts',
49+
'GET'
50+
);
51+
$this->assertEquals(5, count($paginator->paginate($collection, $request)));
52+
}
3853
}

tests/WizardsTest/Parser/RestQueryParserTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ public function testGetDefaults()
5252
$this->assertEquals(RestQueryParser::DEFAULT_PAGE, $parser->get(RestQueryParser::PARAMETER_PAGE));
5353
$this->assertEquals(RestQueryParser::DEFAULT_SORT, $parser->get(RestQueryParser::PARAMETER_SORT));
5454
$this->assertEquals(RestQueryParser::DEFAULT_FILTER, $parser->get(RestQueryParser::PARAMETER_FILTER));
55-
$this->assertEquals(RestQueryParser::DEFAULT_FILTER_OPERATOR, $parser->get(RestQueryParser::PARAMETER_FILTER_OPERATOR));
55+
$this->assertEquals(
56+
RestQueryParser::DEFAULT_FILTER_OPERATOR,
57+
$parser->get(RestQueryParser::PARAMETER_FILTER_OPERATOR)
58+
);
5659
$this->assertEquals(null, $parser->get('unused'));
5760
}
5861

0 commit comments

Comments
 (0)
0