Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
263 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/Bridge/Elasticsearch/DataProvider/PaginatorFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Bridge\Elasticsearch\DataProvider; | ||
|
||
use ApiPlatform\Core\DataProvider\PaginatorFactoryInterface; | ||
use ApiPlatform\Core\DataProvider\PaginatorInterface; | ||
use ApiPlatform\Core\Exception\RuntimeException; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
|
||
final class PaginatorFactory implements PaginatorFactoryInterface | ||
{ | ||
private $denormalizer; | ||
|
||
public function __construct(DenormalizerInterface $denormalizer) | ||
{ | ||
$this->denormalizer = $denormalizer; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function createPaginator($subject, int $limit, int $offset, array $context = []): PaginatorInterface | ||
{ | ||
$resourceClass = $context['resourceClass'] ?? null; | ||
|
||
if (null === $resourceClass) { | ||
throw new RuntimeException('The given context array is missing the "resourceClass" key.'); | ||
} | ||
|
||
return new Paginator( | ||
$this->denormalizer, | ||
$subject, | ||
$resourceClass, | ||
$limit, | ||
$offset, | ||
$context | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\DataProvider; | ||
|
||
use ApiPlatform\Core\Exception\ExceptionInterface; | ||
|
||
interface PaginatorFactoryInterface | ||
{ | ||
/** | ||
* Creates a new {@see PaginatorInterface} concrete instance. | ||
* | ||
* @param mixed $subject The subject to paginate (array, ORM query, etc.) | ||
* @param int $limit The maximum number of records to fetch | ||
* @param int $offset The starting index from which to fetch the records | ||
* @param array<string, mixed> $context The associative array context for the paginator | ||
* | ||
* @throws ExceptionInterface Whenever something wrong occurs | ||
*/ | ||
public function createPaginator($subject, int $limit, int $offset, array $context = []): PaginatorInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
tests/Bridge/Elasticsearch/DataProvider/PaginatorFactoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Tests\Bridge\Elasticsearch\DataProvider; | ||
|
||
use ApiPlatform\Core\Bridge\Elasticsearch\DataProvider\Paginator; | ||
use ApiPlatform\Core\Bridge\Elasticsearch\DataProvider\PaginatorFactory; | ||
use ApiPlatform\Core\DataProvider\PaginatorFactoryInterface; | ||
use ApiPlatform\Core\Exception\RuntimeException; | ||
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Foo; | ||
use ApiPlatform\Core\Tests\ProphecyTrait; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
|
||
final class PaginatorFactoryTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
/** | ||
* @var PaginatorFactory | ||
*/ | ||
private $paginatorFactory; | ||
|
||
public function testConstruct() | ||
{ | ||
$this->assertInstanceOf(PaginatorFactoryInterface::class, $this->paginatorFactory); | ||
} | ||
|
||
public function testCreatePaginator() | ||
{ | ||
$paginator = $this->paginatorFactory->createPaginator([], 10, 0, ['resourceClass' => Foo::class]); | ||
|
||
$this->assertInstanceOf(Paginator::class, $paginator); | ||
} | ||
|
||
public function testCreatePaginatorFailsWhenResourceClassAttributeIsMissing() | ||
{ | ||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage('The given context array is missing the "resourceClass" key.'); | ||
|
||
$this->paginatorFactory->createPaginator([], 10, 0, ['resourceClass' => null]); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->paginatorFactory = new PaginatorFactory($this->prophesize(DenormalizerInterface::class)->reveal()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters