Skip to content

Commit

Permalink
Add an EmptyAdapter (#55)
Browse files Browse the repository at this point in the history
Unlike the NullAdapter, this adapter is always empty instead of allowing
it to paginate an array of null values. Thanks to that, its generic type
can fullfil types like `AdapterInterface<Foo>` with a non-nullable `Foo`
type.
  • Loading branch information
stof committed Mar 24, 2024
1 parent 90ea5ba commit a2f5f91
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/Core/Adapter/EmptyAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

namespace Pagerfanta\Adapter;

/**
* An adapter that is always empty.
*
* @template-implements AdapterInterface<never>
*/
class EmptyAdapter implements AdapterInterface
{
public function getNbResults(): int
{
return 0;
}

public function getSlice(int $offset, int $length): iterable
{
return [];
}
}
23 changes: 23 additions & 0 deletions lib/Core/Tests/Adapter/EmptyAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace Adapter;

use Pagerfanta\Adapter\EmptyAdapter;
use PHPUnit\Framework\TestCase;

final class EmptyAdapterTest extends TestCase
{
public function testGetNbResults(): void
{
$adapter = new EmptyAdapter();

self::assertSame(0, $adapter->getNbResults());
}

public function testGetSliceShouldReturnAnEmptyArray(): void
{
$adapter = new EmptyAdapter();

self::assertSame([], $adapter->getSlice(10, 5));
}
}

0 comments on commit a2f5f91

Please sign in to comment.