From 9ea57391cb62691a5e9920b1c2568fe72d87b234 Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Sat, 4 Feb 2017 15:03:26 +0300 Subject: [PATCH] pagination split --- .gitignore | 2 +- composer.json | 3 +- .../Spiral/Pagination/CountingInterface.php | 24 -- .../Exceptions/PaginationException.php | 16 -- source/Spiral/Pagination/PagedInterface.php | 84 ------- source/Spiral/Pagination/Paginator.php | 205 ------------------ .../Pagination/PaginatorAwareInterface.php | 46 ---- .../Spiral/Pagination/PaginatorInterface.php | 29 --- .../Spiral/Pagination/PaginatorsInterface.php | 26 --- .../Spiral/Pagination/Traits/LimitsTrait.php | 71 ------ .../Pagination/Traits/PaginatorTrait.php | 135 ------------ tests/Pagination/PaginatorTest.php | 181 ---------------- tests/Pagination/Traits/LimitsTraitTest.php | 48 ---- .../Pagination/Traits/PaginatorTraitTest.php | 98 --------- 14 files changed, 3 insertions(+), 965 deletions(-) delete mode 100644 source/Spiral/Pagination/CountingInterface.php delete mode 100644 source/Spiral/Pagination/Exceptions/PaginationException.php delete mode 100644 source/Spiral/Pagination/PagedInterface.php delete mode 100644 source/Spiral/Pagination/Paginator.php delete mode 100644 source/Spiral/Pagination/PaginatorAwareInterface.php delete mode 100644 source/Spiral/Pagination/PaginatorInterface.php delete mode 100644 source/Spiral/Pagination/PaginatorsInterface.php delete mode 100644 source/Spiral/Pagination/Traits/LimitsTrait.php delete mode 100644 source/Spiral/Pagination/Traits/PaginatorTrait.php delete mode 100644 tests/Pagination/PaginatorTest.php delete mode 100644 tests/Pagination/Traits/LimitsTraitTest.php delete mode 100644 tests/Pagination/Traits/PaginatorTraitTest.php diff --git a/.gitignore b/.gitignore index cb6d323c..7182f88b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,4 @@ build/logs/* build/ *.db clover.xml -coveralls.json \ No newline at end of file +clover.json \ No newline at end of file diff --git a/composer.json b/composer.json index 3bf4f812..42a21f26 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ ], "require": { "php": ">=7.0", - "spiral/common": "^0.9" + "spiral/common": "^0.9", + "spiral/pagination": "~1.0" }, "require-dev": { "phpunit/phpunit": "~5.0", diff --git a/source/Spiral/Pagination/CountingInterface.php b/source/Spiral/Pagination/CountingInterface.php deleted file mode 100644 index 1df6f9a8..00000000 --- a/source/Spiral/Pagination/CountingInterface.php +++ /dev/null @@ -1,24 +0,0 @@ -limit = $limit; - $this->count = $count; - } - - /** - * {@inheritdoc} - * - * @return self - */ - public function withLimit(int $limit): PagedInterface - { - $paginator = clone $this; - $paginator->limit = $limit; - - return $paginator; - } - - /** - * {@inheritdoc} - */ - public function getLimit(): int - { - return $this->limit; - } - - /** - * {@inheritdoc} - */ - public function withPage(int $number): PagedInterface - { - $paginator = clone $this; - $paginator->pageNumber = max($number, 0); - - //Real page number - return $paginator; - } - - /** - * {@inheritdoc} - */ - public function getPage(): int - { - if ($this->pageNumber < 1) { - return 1; - } - - if ($this->pageNumber > $this->countPages) { - return $this->countPages; - } - - return $this->pageNumber; - } - - /** - * {@inheritdoc} - */ - public function getOffset(): int - { - return ($this->getPage() - 1) * $this->limit; - } - - /** - * {@inheritdoc} - * - * @return self - */ - public function withCount(int $count): CountingInterface - { - $paginator = clone $this; - - return $paginator->setCount($count); - } - - /** - * Alias for count. - * - * @return int - */ - public function getCount(): int - { - return $this->count; - } - - /** - * @return int - */ - public function count() - { - return $this->count; - } - - /** - * {@inheritdoc} - */ - public function countPages(): int - { - return $this->countPages; - } - - /** - * {@inheritdoc} - */ - public function countDisplayed(): int - { - if ($this->getPage() == $this->countPages) { - return $this->count - $this->getOffset(); - } - - return $this->limit; - } - - /** - * {@inheritdoc} - */ - public function isRequired(): bool - { - return ($this->countPages > 1); - } - - /** - * {@inheritdoc} - */ - public function nextPage() - { - if ($this->getPage() != $this->countPages) { - return $this->getPage() + 1; - } - - return null; - } - - /** - * {@inheritdoc} - */ - public function previousPage() - { - if ($this->getPage() > 1) { - return $this->getPage() - 1; - } - - return null; - } - - /** - * Non-Immutable version of withCount. - * - * @param int $count - * - * @return self|$this - */ - private function setCount(int $count): self - { - $this->count = max($count, 0); - if ($this->count > 0) { - $this->countPages = (int)ceil($this->count / $this->limit); - } else { - $this->countPages = 1; - } - - return $this; - } -} \ No newline at end of file diff --git a/source/Spiral/Pagination/PaginatorAwareInterface.php b/source/Spiral/Pagination/PaginatorAwareInterface.php deleted file mode 100644 index 51bc462a..00000000 --- a/source/Spiral/Pagination/PaginatorAwareInterface.php +++ /dev/null @@ -1,46 +0,0 @@ -limit = $limit; - - return $this; - } - - /** - * @return int - */ - public function getLimit(): int - { - return $this->limit; - } - - /** - * Set selection offset. Attention, this value does not affect associated paginator but only - * changes pagination window. - * - * @param int $offset - * - * @return mixed - */ - public function offset(int $offset = 0) - { - $this->offset = $offset; - - return $this; - } - - /** - * @return int - */ - public function getOffset(): int - { - return $this->offset; - } -} \ No newline at end of file diff --git a/source/Spiral/Pagination/Traits/PaginatorTrait.php b/source/Spiral/Pagination/Traits/PaginatorTrait.php deleted file mode 100644 index 062c7db5..00000000 --- a/source/Spiral/Pagination/Traits/PaginatorTrait.php +++ /dev/null @@ -1,135 +0,0 @@ -paginator instanceof PaginatorInterface; - } - - /** - * Manually set paginator instance for specific object. - * - * @param PaginatorInterface $paginator - * - * @return $this - */ - public function setPaginator(PaginatorInterface $paginator) - { - $this->paginator = $paginator; - - return $this; - } - - /** - * Get paginator for the current selection. Paginate method should be already called. - * - * @see hasPaginator() - * @see paginate() - * - * @return PaginatorInterface - */ - public function getPaginator(): PaginatorInterface - { - if (!$this->hasPaginator()) { - throw new PaginationException("Unable to get paginator, no paginator were set"); - } - - return $this->paginator; - } - - /** - * Paginate current selection using Paginator class. - * - * @param int $limit Pagination limit. - * @param string $parameter Name of parameter to associate paginator with, by default query - * parameter of active request to be used. - * - * @return $this - * - * @throws ScopeException - */ - public function paginate(int $limit = 25, string $parameter = 'page') - { - //We are required to fetch paginator from associated container or shared container - $container = $this->iocContainer(); - - if (empty($container) || !$container->has(PaginatorsInterface::class)) { - throw new ScopeException( - 'Unable to create paginator, PaginatorsInterface binding is missing or container not set' - ); - } - - /** - * @var PaginatorsInterface $factory - */ - $factory = $container->get(PaginatorsInterface::class); - - //Now we can create new instance of paginator using factory - $this->paginator = $factory->createPaginator($parameter, $limit); - - return $this; - } - - /** - * Get paginator instance configured for a given count. Must not affect already associated - * paginator instance. - * - * Attention: this method MUST be called from a child class in order to properly set paginator - * counts IF paginator support it. - * - * @param int|null $count Can be skipped. - * - * @return PaginatorInterface - */ - protected function configurePaginator(int $count = null): PaginatorInterface - { - $paginator = $this->getPaginator(); - - if (!empty($count) && $paginator instanceof CountingInterface) { - $paginator = $paginator->withCount($count); - } else { - $paginator = clone $paginator; - } - - return $paginator; - } - - /** - * @return ContainerInterface - */ - abstract protected function iocContainer(); -} diff --git a/tests/Pagination/PaginatorTest.php b/tests/Pagination/PaginatorTest.php deleted file mode 100644 index 9e629f7f..00000000 --- a/tests/Pagination/PaginatorTest.php +++ /dev/null @@ -1,181 +0,0 @@ -assertInstanceOf(PaginatorInterface::class, $paginator); - $this->assertInstanceOf(CountingInterface::class, $paginator); - $this->assertInstanceOf(PagedInterface::class, $paginator); - } - - public function testLimit() - { - $paginator = new Paginator(25); - - $this->assertSame(25, $paginator->getLimit()); - $newPaginator = $paginator->withLimit(50); - $this->assertSame(25, $paginator->getLimit()); - $this->assertSame(50, $newPaginator->getLimit()); - } - - public function testCountsAndPages() - { - $paginator = new Paginator(25); - - $this->assertSame(0, $paginator->getCount()); - $this->assertSame($paginator->count(), $paginator->getCount()); - $this->assertSame($paginator->count(), count($paginator)); - - $this->assertSame(1, $paginator->getPage()); - $this->assertSame(0, $paginator->getOffset()); - $this->assertSame(1, $paginator->countPages()); - $this->assertSame(0, $paginator->countDisplayed()); - } - - public function testFirstPage() - { - $paginator = new Paginator(25); - $paginator = $paginator->withCount(100); - - $this->assertSame(1, $paginator->getPage()); - - $this->assertSame(null, $paginator->previousPage()); - $this->assertSame(2, $paginator->nextPage()); - - $this->assertSame(100, $paginator->getCount()); - $this->assertSame($paginator->count(), $paginator->getCount()); - $this->assertSame($paginator->count(), count($paginator)); - - $this->assertSame(0, $paginator->getOffset()); - $this->assertSame(4, $paginator->countPages()); - $this->assertSame(25, $paginator->countDisplayed()); - } - - - public function testSecondPage() - { - $paginator = new Paginator(25); - $paginator = $paginator->withCount(110); - - $this->assertSame(110, $paginator->getCount()); - $this->assertSame($paginator->count(), $paginator->getCount()); - $this->assertSame($paginator->count(), count($paginator)); - - $this->assertSame(1, $paginator->getPage()); - - $paginator = $paginator->withPage(2); - - $this->assertSame(1, $paginator->previousPage()); - $this->assertSame(3, $paginator->nextPage()); - - $this->assertSame(2, $paginator->getPage()); - $this->assertSame(25, $paginator->getOffset()); - $this->assertSame(5, $paginator->countPages()); - $this->assertSame(25, $paginator->countDisplayed()); - } - - public function testLastPage() - { - $paginator = new Paginator(25); - $paginator = $paginator->withCount(100); - - $this->assertSame(1, $paginator->getPage()); - - $this->assertSame(null, $paginator->previousPage()); - $this->assertSame(2, $paginator->nextPage()); - - $paginator = $paginator->withPage(100); - - $this->assertSame(4, $paginator->getPage()); - $this->assertSame(3, $paginator->previousPage()); - $this->assertSame(null, $paginator->nextPage()); - - $this->assertSame(100, $paginator->getCount()); - $this->assertSame($paginator->count(), $paginator->getCount()); - $this->assertSame($paginator->count(), count($paginator)); - - $this->assertSame(75, $paginator->getOffset()); - $this->assertSame(4, $paginator->countPages()); - $this->assertSame(25, $paginator->countDisplayed()); - } - - public function testNegativePage() - { - $paginator = new Paginator(25); - $paginator = $paginator->withCount(100); - $paginator = $paginator->withPage(-1); - - $this->assertSame(1, $paginator->getPage()); - - $this->assertSame(100, $paginator->getCount()); - $this->assertSame($paginator->count(), $paginator->getCount()); - $this->assertSame($paginator->count(), count($paginator)); - } - - public function testNegativeCount() - { - $paginator = new Paginator(25); - $paginator = $paginator->withCount(-100); - - $paginator = $paginator->withPage(-10); - $this->assertSame(1, $paginator->getPage()); - - $this->assertSame(null, $paginator->previousPage()); - $this->assertSame(null, $paginator->nextPage()); - - $this->assertSame(0, $paginator->getCount()); - $this->assertSame(0, $paginator->getOffset()); - $this->assertSame(1, $paginator->countPages()); - $this->assertSame(0, $paginator->countDisplayed()); - } - - public function testLastPageNumber() - { - $paginator = new Paginator(25); - $paginator = $paginator->withCount(110); - - $this->assertSame(110, $paginator->getCount()); - $this->assertSame(1, $paginator->getPage()); - - $paginator = $paginator->withPage(100); - - $this->assertSame($paginator->countPages(), $paginator->getPage()); - $this->assertSame( - ($paginator->getPage() - 1) * $paginator->getLimit(), - $paginator->getOffset() - ); - - $this->assertSame(5, $paginator->countPages()); - $this->assertSame(10, $paginator->countDisplayed()); - } - - public function testIsRequired() - { - $paginator = new Paginator(25); - - $paginator = $paginator->withCount(24); - $this->assertFalse($paginator->isRequired()); - - $paginator = $paginator->withCount(25); - $this->assertFalse($paginator->isRequired()); - - $paginator = $paginator->withCount(26); - $this->assertTrue($paginator->isRequired()); - } -} \ No newline at end of file diff --git a/tests/Pagination/Traits/LimitsTraitTest.php b/tests/Pagination/Traits/LimitsTraitTest.php deleted file mode 100644 index 53b54fc5..00000000 --- a/tests/Pagination/Traits/LimitsTraitTest.php +++ /dev/null @@ -1,48 +0,0 @@ - - */ - -namespace Spiral\Tests\Pagination\Traits; - - -use Spiral\Pagination\Traits\LimitsTrait; - -/** - * Class LimitTraitTest - * - * @package Spiral\Tests\Pagination\Traits - */ -class LimitsTraitTest extends \PHPUnit_Framework_TestCase -{ - const DEFAULT_LIMIT = 0; - const DEFAULT_OFFSET = 0; - const LIMIT = 10; - const OFFSET = 15; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject|LimitsTrait - */ - private $trait; - - public function setUp() - { - $this->trait = $this->getMockForTrait(LimitsTrait::class); - } - - public function testLimit() - { - $this->assertEquals(static::DEFAULT_LIMIT, $this->trait->getLimit()); - $this->assertEquals($this->trait, $this->trait->limit(static::LIMIT)); - $this->assertEquals(static::LIMIT, $this->trait->getLimit()); - } - - public function testOffset() - { - $this->assertEquals(static::DEFAULT_OFFSET, $this->trait->getOffset()); - $this->assertEquals($this->trait, $this->trait->offset(static::OFFSET)); - $this->assertEquals(static::OFFSET, $this->trait->getOffset()); - } -} \ No newline at end of file diff --git a/tests/Pagination/Traits/PaginatorTraitTest.php b/tests/Pagination/Traits/PaginatorTraitTest.php deleted file mode 100644 index f5677358..00000000 --- a/tests/Pagination/Traits/PaginatorTraitTest.php +++ /dev/null @@ -1,98 +0,0 @@ - - */ - -namespace Spiral\Tests\Pagination\Traits; - - -use Interop\Container\ContainerInterface; -use Spiral\Core\Exceptions\ScopeException; -use Spiral\Pagination\Exceptions\PaginationException; -use Spiral\Pagination\Paginator; -use Spiral\Pagination\PaginatorsInterface; -use Spiral\Pagination\Traits\PaginatorTrait; - -/** - * Class PaginatorTraitTest - * - * @package Spiral\Tests\Pagination\Traits - */ -class PaginatorTraitTest extends \PHPUnit_Framework_TestCase -{ - const PAGINATOR_LIMIT = 10; - const PAGINATOR_COUNT = 15; - const PAGINATOR_PARAMETER = 'test'; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject|PaginatorTrait - */ - private $trait; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject|Paginator - */ - private $paginator; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject|ContainerInterface - */ - private $container; - - public function setUp() - { - $this->trait = $this->getMockForTrait(PaginatorTrait::class); - $this->container = $this->createMock(ContainerInterface::class); - $this->paginator = $this->createMock(Paginator::class); - - $this->trait->method('iocContainer')->willReturn($this->container); - } - - public function testSetPaginator() - { - $this->assertFalse($this->trait->hasPaginator()); - $this->assertEquals($this->trait, $this->trait->setPaginator($this->paginator)); - $this->assertTrue($this->trait->hasPaginator()); - $this->assertEquals($this->paginator, $this->trait->getPaginator()); - } - - public function testGetPaginatorWasNotSetException() - { - $this->expectException(PaginationException::class); - $this->trait->getPaginator(); - } - - public function testPaginate() - { - $paginators = $this->createMock(PaginatorsInterface::class); - $paginators->method('createPaginator') - ->with(static::PAGINATOR_PARAMETER, static::PAGINATOR_LIMIT) - ->willReturn($this->paginator); - - $this->container->method('has')->with(PaginatorsInterface::class)->willReturn(true); - $this->container->method('get')->with(PaginatorsInterface::class)->willReturn($paginators); - - $this->assertEquals($this->trait, - $this->trait->paginate(static::PAGINATOR_LIMIT, static::PAGINATOR_PARAMETER)); - } - - public function testPaginateScopeExceptionNoContainer() - { - $this->expectException(ScopeException::class); - $this->trait->paginate(); - } - - public function testConfigurePaginator() - { - $reflection = new \ReflectionObject($this->trait); - $method = $reflection->getMethod('configurePaginator'); - $method->setAccessible(true); - - $this->trait->setPaginator($this->paginator); - $this->assertNotSame($this->paginator, $method->invoke($this->trait)); - $this->assertNotSame($this->paginator, - $method->invoke($this->trait, self::PAGINATOR_COUNT)); - } -} \ No newline at end of file