Skip to content

Commit 21300d5

Browse files
Merge pull request #2 from PHP-Open-Source-Saver/feature/adds-laravel-simple-paginator
Feature/adds laravel simple paginator
2 parents 92b9ee0 + 8630339 commit 21300d5

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHPOpenSourceSaver\Fractal package.
5+
*
6+
* (c) Max Snow <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace PHPOpenSourceSaver\Fractal\Pagination;
13+
14+
use Illuminate\Contracts\Pagination\Paginator;
15+
16+
/**
17+
* A paginator adapter for illuminate/pagination.
18+
*
19+
* @author Maxime Beaudoin <[email protected]>
20+
* @author Marc Addeo <[email protected]>
21+
*/
22+
class IlluminateSimplePaginatorAdapter implements PaginatorInterface
23+
{
24+
protected Paginator $paginator;
25+
26+
/**
27+
* Create a new illuminate simple pagination adapter.
28+
*/
29+
public function __construct(Paginator $paginator)
30+
{
31+
$this->paginator = $paginator;
32+
}
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
public function getCurrentPage(): int
38+
{
39+
return $this->paginator->currentPage();
40+
}
41+
42+
/**
43+
* {@inheritDoc}
44+
*/
45+
public function getLastPage(): int
46+
{
47+
return 0;
48+
}
49+
50+
/**
51+
* {@inheritDoc}
52+
*/
53+
public function getTotal(): int
54+
{
55+
return 0;
56+
}
57+
58+
/**
59+
* {@inheritDoc}
60+
*/
61+
public function getCount(): int
62+
{
63+
return $this->paginator->count();
64+
}
65+
66+
/**
67+
* {@inheritDoc}
68+
*/
69+
public function getPerPage(): int
70+
{
71+
return $this->paginator->perPage();
72+
}
73+
74+
/**
75+
* {@inheritDoc}
76+
*/
77+
public function getUrl(int $page): string
78+
{
79+
return $this->paginator->url($page);
80+
}
81+
82+
/**
83+
* Get the paginator instance.
84+
*/
85+
public function getPaginator(): Paginator
86+
{
87+
return $this->paginator;
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace PHPOpenSourceSaver\Fractal\Test\Pagination;
4+
5+
use PHPOpenSourceSaver\Fractal\Pagination\IlluminateSimplePaginatorAdapter;
6+
use Mockery;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class IlluminateSimplePaginatorAdapterTest extends TestCase
10+
{
11+
public function testPaginationAdapter()
12+
{
13+
$total = 0;
14+
$count = 10;
15+
$perPage = 10;
16+
$currentPage = 2;
17+
$lastPage = 0;
18+
$url = 'http://example.com/foo?page=1';
19+
20+
$paginator = Mockery::mock('Illuminate\Contracts\Pagination\Paginator');
21+
$paginator->shouldReceive('currentPage')->andReturn($currentPage);
22+
$paginator->shouldReceive('lastPage')->andReturn($lastPage);
23+
$paginator->shouldReceive('count')->andReturn($count);
24+
$paginator->shouldReceive('total')->andReturn($total);
25+
$paginator->shouldReceive('perPage')->andReturn($perPage);
26+
$paginator->shouldReceive('url')->with(1)->andReturn($url);
27+
28+
$adapter = new IlluminateSimplePaginatorAdapter($paginator);
29+
30+
$this->assertInstanceOf('PHPOpenSourceSaver\Fractal\Pagination\PaginatorInterface', $adapter);
31+
$this->assertInstanceOf('Illuminate\Contracts\Pagination\Paginator', $adapter->getPaginator());
32+
33+
$this->assertSame($currentPage, $adapter->getCurrentPage());
34+
$this->assertSame($lastPage, $adapter->getLastPage());
35+
$this->assertSame($count, $adapter->getCount());
36+
$this->assertSame($total, $adapter->getTotal());
37+
$this->assertSame($perPage, $adapter->getPerPage());
38+
$this->assertSame('http://example.com/foo?page=1', $adapter->getUrl(1));
39+
}
40+
41+
public function tearDown(): void
42+
{
43+
Mockery::close();
44+
}
45+
}

0 commit comments

Comments
 (0)