File tree Expand file tree Collapse file tree 4 files changed +77
-3
lines changed Expand file tree Collapse file tree 4 files changed +77
-3
lines changed Original file line number Diff line number Diff line change 1919 */
2020class DoctrinePaginatorAdapter implements PaginatorInterface
2121{
22- use TraversableCountTrait ;
22+ use PaginatorCountTrait ;
2323
2424 /**
2525 * The paginator instance.
Original file line number Diff line number Diff line change 2020 */
2121class PagerfantaPaginatorAdapter implements PaginatorInterface
2222{
23+ use PaginatorCountTrait;
24+
2325 protected Pagerfanta $ paginator ;
2426
2527 /**
@@ -64,7 +66,7 @@ public function getTotal(): int
6466 */
6567 public function getCount (): int
6668 {
67- return count ($ this ->paginator ->getCurrentPageResults ());
69+ return $ this -> getIterableCount ($ this ->paginator ->getCurrentPageResults ());
6870 }
6971
7072 /**
Original file line number Diff line number Diff line change 22
33namespace League \Fractal \Pagination ;
44
5- trait TraversableCountTrait
5+ trait PaginatorCountTrait
66{
7+ /**
8+ * Safely get the count from an iterable
9+ */
10+ private function getIterableCount (iterable $ iterable ): int
11+ {
12+ if ($ iterable instanceof \Traversable) {
13+ return $ this ->getTraversableCount ($ iterable );
14+ }
15+
16+ return count ($ iterable );
17+ }
18+
719 /**
820 * Safely get the count from a traversable
921 */
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace League \Fractal \Test \Pagination ;
4+
5+ use League \Fractal \Pagination \PaginatorCountTrait ;
6+ use League \Fractal \Test \Stub \SimpleTraversable ;
7+ use PHPUnit \Framework \TestCase ;
8+
9+ class PaginatorCountTraitTest extends TestCase
10+ {
11+ protected $ instance ;
12+
13+ /** @before */
14+ public function before ()
15+ {
16+ $ this ->instance = new class () {
17+ use PaginatorCountTrait;
18+
19+ public function getIterableCountPublic (iterable $ iterable )
20+ {
21+ return $ this ->getIterableCount ($ iterable );
22+ }
23+
24+ public function getTraversableCountPublic (\Traversable $ traversable )
25+ {
26+ return $ this ->getTraversableCount ($ traversable );
27+ }
28+ };
29+ }
30+
31+ /**
32+ * @dataProvider arrayProvider
33+ */
34+ public function testSupportsIterables (array $ data )
35+ {
36+ $ count = count ($ data );
37+ $ this ->assertEquals ($ count , $ this ->instance ->getIterableCountPublic ($ data ));
38+ $ this ->assertEquals ($ count , $ this ->instance ->getIterableCountPublic (new \ArrayIterator ($ data )));
39+ $ this ->assertEquals ($ count , $ this ->instance ->getIterableCountPublic (new SimpleTraversable ($ data )));
40+ }
41+
42+ /**
43+ * @dataProvider arrayProvider
44+ */
45+ public function testSupportsTraversables (array $ data )
46+ {
47+ $ count = count ($ data );
48+ $ this ->assertEquals ($ count , $ this ->instance ->getTraversableCountPublic (new \ArrayIterator ($ data )));
49+ $ this ->assertEquals ($ count , $ this ->instance ->getTraversableCountPublic (new SimpleTraversable ($ data )));
50+ }
51+
52+ public function arrayProvider ()
53+ {
54+ return [
55+ [[]],
56+ [[1 , 2 , 3 ]],
57+ [range (1 , 100 )],
58+ ];
59+ }
60+ }
You can’t perform that action at this time.
0 commit comments