-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(`QueryBuilderAdapter`): add `indexBy` parameter * remove flex
- Loading branch information
Showing
11 changed files
with
268 additions
and
8 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
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
28 changes: 28 additions & 0 deletions
28
packages/rekapager-doctrine-orm-adapter/src/Exception/CannotResolveIndexException.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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/rekapager package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Rekapager\Doctrine\ORM\Exception; | ||
|
||
use Rekalogika\Contracts\Rekapager\Exception\LogicException; | ||
|
||
class CannotResolveIndexException extends LogicException | ||
{ | ||
public function __construct(mixed $row, string $indexBy, \Throwable $previous) | ||
{ | ||
parent::__construct(sprintf( | ||
'Unable to resolve the index "%s" from the result row of type "%s".', | ||
$indexBy, | ||
get_debug_type($row), | ||
), 0, $previous); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/rekapager-doctrine-orm-adapter/src/Exception/IncompatibleKeyTypeException.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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/rekapager package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Rekapager\Doctrine\ORM\Exception; | ||
|
||
use Rekalogika\Contracts\Rekapager\Exception\UnexpectedValueException; | ||
|
||
class IncompatibleKeyTypeException extends UnexpectedValueException | ||
{ | ||
public function __construct(mixed $row, string $indexBy, mixed $key) | ||
{ | ||
parent::__construct(sprintf( | ||
'Trying to get the index "%s" from the result row of type "%s", but the resulting index has the type of "%s". The resulting index must be an integer, string, or a "Stringable" object.', | ||
$indexBy, | ||
get_debug_type($row), | ||
get_debug_type($key), | ||
)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...ges/rekapager-doctrine-orm-adapter/src/Exception/RowNotCompatibleWithIndexByException.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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/rekapager package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Rekapager\Doctrine\ORM\Exception; | ||
|
||
use Rekalogika\Contracts\Rekapager\Exception\UnexpectedValueException; | ||
|
||
class RowNotCompatibleWithIndexByException extends UnexpectedValueException | ||
{ | ||
public function __construct(mixed $row, string $indexBy) | ||
{ | ||
parent::__construct(sprintf('Your query returns rows of type "%s", but it is not compatible with the index by "%s". The row must be an array or an object with a property named "%s".', get_debug_type($row), $indexBy, $indexBy)); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
packages/rekapager-doctrine-orm-adapter/src/Internal/Utils.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,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/rekapager package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Rekapager\Doctrine\ORM\Internal; | ||
|
||
use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor; | ||
use Rekalogika\Rekapager\Doctrine\ORM\Exception\CannotResolveIndexException; | ||
use Rekalogika\Rekapager\Doctrine\ORM\Exception\IncompatibleKeyTypeException; | ||
use Rekalogika\Rekapager\Doctrine\ORM\Exception\RowNotCompatibleWithIndexByException; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class Utils | ||
{ | ||
private function __construct() | ||
{ | ||
} | ||
|
||
public static function resolveIndex(mixed $row, string $indexBy): int|string | ||
{ | ||
if (!\is_array($row) && !\is_object($row)) { | ||
throw new RowNotCompatibleWithIndexByException($row, $indexBy); | ||
} | ||
|
||
try { | ||
/** @var mixed */ | ||
$key = ClosureExpressionVisitor::getObjectFieldValue($row, $indexBy); | ||
} catch (\Throwable $e) { | ||
throw new CannotResolveIndexException($row, $indexBy, $e); | ||
} | ||
|
||
if (!\is_string($key) && !\is_int($key) && !$key instanceof \Stringable) { | ||
throw new IncompatibleKeyTypeException($row, $indexBy, $key); | ||
} | ||
|
||
if ($key instanceof \Stringable) { | ||
$key = (string) $key; | ||
} | ||
|
||
return $key; | ||
} | ||
} |
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
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
119 changes: 119 additions & 0 deletions
119
tests/src/IntegrationTests/Index/QueryBuilderAdapterIndexByTest.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,119 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/rekapager package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Rekapager\Tests\IntegrationTests\Index; | ||
|
||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\QueryBuilder; | ||
use Rekalogika\Rekapager\Doctrine\ORM\Exception\IncompatibleKeyTypeException; | ||
use Rekalogika\Rekapager\Doctrine\ORM\Exception\RowNotCompatibleWithIndexByException; | ||
use Rekalogika\Rekapager\Doctrine\ORM\QueryBuilderAdapter; | ||
use Rekalogika\Rekapager\Keyset\KeysetPageable; | ||
use Rekalogika\Rekapager\Tests\App\Entity\Post; | ||
use Rekalogika\Rekapager\Tests\App\Repository\PostRepository; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
|
||
class QueryBuilderAdapterIndexByTest extends KernelTestCase | ||
{ | ||
protected function getQueryBuilder(): QueryBuilder | ||
{ | ||
$postRepository = self::getContainer()->get(PostRepository::class); | ||
|
||
return $postRepository | ||
->createQueryBuilder('p') | ||
->where('p.setName = :setName') | ||
->setParameter('setName', 'large') | ||
->addOrderBy('p.date', 'DESC') | ||
->addOrderBy('p.title', 'ASC') | ||
->addOrderBy('p.id', 'ASC'); | ||
} | ||
|
||
public function testIndexBy(): void | ||
{ | ||
$queryBuilder = $this->getQueryBuilder(); | ||
|
||
$adapter = new QueryBuilderAdapter( | ||
queryBuilder: $queryBuilder, | ||
typeMapping: [ | ||
'p.date' => Types::DATE_MUTABLE | ||
], | ||
indexBy: 'id' | ||
); | ||
|
||
$pageable = new KeysetPageable( | ||
adapter: $adapter, | ||
itemsPerPage: 1000, | ||
); | ||
|
||
/** @var Post $post */ | ||
foreach ($pageable->getFirstPage() as $key => $post) { | ||
static::assertInstanceOf(Post::class, $post); | ||
static::assertEquals($key, $post->getId()); | ||
} | ||
} | ||
|
||
public function testInvalidIndexBy(): void | ||
{ | ||
$queryBuilder = $this->getQueryBuilder(); | ||
|
||
$adapter = new QueryBuilderAdapter( | ||
queryBuilder: $queryBuilder, | ||
typeMapping: [ | ||
'p.date' => Types::DATE_MUTABLE | ||
], | ||
indexBy: 'foo' | ||
); | ||
|
||
$pageable = new KeysetPageable( | ||
adapter: $adapter, | ||
itemsPerPage: 1000, | ||
); | ||
|
||
$this->expectException(IncompatibleKeyTypeException::class); | ||
|
||
iterator_to_array($pageable->getFirstPage()); | ||
} | ||
|
||
public function testIncompatibleRow(): void | ||
{ | ||
$entityManager = self::getContainer()->get(EntityManagerInterface::class); | ||
|
||
$queryBuilder = $entityManager | ||
->createQueryBuilder() | ||
->from(Post::class, 'p') | ||
->select('p.id') | ||
->where('p.setName = :setName') | ||
->setParameter('setName', 'large') | ||
->addOrderBy('p.date', 'DESC') | ||
->addOrderBy('p.title', 'ASC') | ||
->addOrderBy('p.id', 'ASC'); | ||
|
||
$adapter = new QueryBuilderAdapter( | ||
queryBuilder: $queryBuilder, | ||
typeMapping: [ | ||
'p.date' => Types::DATE_MUTABLE | ||
], | ||
indexBy: 'foo' | ||
); | ||
|
||
$pageable = new KeysetPageable( | ||
adapter: $adapter, | ||
itemsPerPage: 1000, | ||
); | ||
|
||
$this->expectException(RowNotCompatibleWithIndexByException::class); | ||
|
||
iterator_to_array($pageable->getFirstPage()); | ||
} | ||
} |