Skip to content

Commit 8b12126

Browse files
committed
Support iterable type fully in newer pagerfanta versions
1 parent afda5ee commit 8b12126

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

src/Pagination/DoctrinePaginatorAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
class DoctrinePaginatorAdapter implements PaginatorInterface
2121
{
22-
use TraversableCountTrait;
22+
use PaginatorCountTrait;
2323

2424
/**
2525
* The paginator instance.

src/Pagination/PagerfantaPaginatorAdapter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
class 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
/**

src/Pagination/TraversableCountTrait.php renamed to src/Pagination/PaginatorCountTrait.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,20 @@
22

33
namespace 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
*/
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

0 commit comments

Comments
 (0)