Skip to content

Commit

Permalink
Add generic paginator
Browse files Browse the repository at this point in the history
  • Loading branch information
akDeveloper committed Nov 8, 2024
1 parent fbc5592 commit 35711c2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Larium\Search\Doctrine\Dbal;
namespace Larium\Search;

use Larium\Search\Criteria\Paginating;
use Larium\Search\Paginator;
use Larium\Search\Result;
use ArrayIterator;
use Traversable;
use function intval;
use ArrayIterator;
use Larium\Search\Result;
use Larium\Search\Paginator;
use Larium\Search\Criteria\Paginating;

use function ceil;
use function intval;

class DoctrineDbalPaginator implements Paginator
class ResultPaginator implements Paginator
{
private $result;

Expand Down Expand Up @@ -76,7 +77,7 @@ public function getNextPage(): ?int
public function getPreviousPage(): ?int
{
return $this->paginating->currentPage > 1
? $this->paginating->currentPage - 1
? $this->paginating->currentPage - 1
: null;
}
}
23 changes: 23 additions & 0 deletions tests/MongoDb/MongoDbSearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use MongoDB\Collection;
use Larium\Search\Criteria;
use PHPUnit\Framework\TestCase;
use Larium\Search\ResultPaginator;

class MongoDbSearchEngineTest extends TestCase
{
Expand Down Expand Up @@ -101,6 +102,28 @@ public function testSearchEngineWithMultipleQueries(): void
self::assertEquals(3, $result->count());
}

public function testPaginator(): void
{
$criteria = Criteria::fromQueryParams('fruits', ['name' => 'a', 'color' => 'yellow', 'page' => 1, 'limit' => 2]);

$c = $this->collection;
$b = new FilterBuilder();
$e = new MongoDbSearchEngine($b, $c);

$e->add(new FruitNameBuilder())
->add(new FruitColorBuilder())
;

$result = $e->match($criteria);

$paginator = new ResultPaginator($result, $criteria->paginating);

self::assertEquals(3, $paginator->count());
self::assertEquals(2, $paginator->getTotalPages());
self::assertTrue($paginator->hasMore());
self::assertEquals(2, $paginator->getNextPage());
}

private function createClient(): Client
{
$client = new Client(
Expand Down

0 comments on commit 35711c2

Please sign in to comment.