Skip to content

Feature/adds laravel simple paginator #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions src/Pagination/IlluminateSimplePaginatorAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/*
* This file is part of the PHPOpenSourceSaver\Fractal package.
*
* (c) Max Snow <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PHPOpenSourceSaver\Fractal\Pagination;

use Illuminate\Contracts\Pagination\Paginator;

/**
* A paginator adapter for illuminate/pagination.
*
* @author Maxime Beaudoin <[email protected]>
* @author Marc Addeo <[email protected]>
*/
class IlluminateSimplePaginatorAdapter implements PaginatorInterface
{
protected Paginator $paginator;

/**
* Create a new illuminate simple pagination adapter.
*/
public function __construct(Paginator $paginator)
{
$this->paginator = $paginator;
}

/**
* {@inheritDoc}
*/
public function getCurrentPage(): int
{
return $this->paginator->currentPage();
}

/**
* {@inheritDoc}
*/
public function getLastPage(): int
{
return 0;
}

/**
* {@inheritDoc}
*/
public function getTotal(): int
{
return 0;
}

/**
* {@inheritDoc}
*/
public function getCount(): int
{
return $this->paginator->count();
}

/**
* {@inheritDoc}
*/
public function getPerPage(): int
{
return $this->paginator->perPage();
}

/**
* {@inheritDoc}
*/
public function getUrl(int $page): string
{
return $this->paginator->url($page);
}

/**
* Get the paginator instance.
*/
public function getPaginator(): Paginator
{
return $this->paginator;
}
}
45 changes: 45 additions & 0 deletions test/Pagination/IlluminateSimplePaginatorAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace PHPOpenSourceSaver\Fractal\Test\Pagination;

use PHPOpenSourceSaver\Fractal\Pagination\IlluminateSimplePaginatorAdapter;
use Mockery;
use PHPUnit\Framework\TestCase;

class IlluminateSimplePaginatorAdapterTest extends TestCase
{
public function testPaginationAdapter()
{
$total = 0;
$count = 10;
$perPage = 10;
$currentPage = 2;
$lastPage = 0;
$url = 'http://example.com/foo?page=1';

$paginator = Mockery::mock('Illuminate\Contracts\Pagination\Paginator');
$paginator->shouldReceive('currentPage')->andReturn($currentPage);
$paginator->shouldReceive('lastPage')->andReturn($lastPage);
$paginator->shouldReceive('count')->andReturn($count);
$paginator->shouldReceive('total')->andReturn($total);
$paginator->shouldReceive('perPage')->andReturn($perPage);
$paginator->shouldReceive('url')->with(1)->andReturn($url);

$adapter = new IlluminateSimplePaginatorAdapter($paginator);

$this->assertInstanceOf('PHPOpenSourceSaver\Fractal\Pagination\PaginatorInterface', $adapter);
$this->assertInstanceOf('Illuminate\Contracts\Pagination\Paginator', $adapter->getPaginator());

$this->assertSame($currentPage, $adapter->getCurrentPage());
$this->assertSame($lastPage, $adapter->getLastPage());
$this->assertSame($count, $adapter->getCount());
$this->assertSame($total, $adapter->getTotal());
$this->assertSame($perPage, $adapter->getPerPage());
$this->assertSame('http://example.com/foo?page=1', $adapter->getUrl(1));
}

public function tearDown(): void
{
Mockery::close();
}
}
Loading