Skip to content

Commit

Permalink
feat(doctrine)!: rename ObjectRepository::filter() to query()
Browse files Browse the repository at this point in the history
And `ObjectRepository::NONE` to `ALL`
  • Loading branch information
kbond committed Jun 27, 2023
1 parent 90e7307 commit d0ee92a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Collection/Doctrine/ORM/EntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function find(mixed $specification): ?object
*
* @return EntityResult<V>
*/
public function filter(mixed $specification): EntityResult
public function query(mixed $specification): EntityResult
{
$qb = $this->qb();

Expand Down
4 changes: 2 additions & 2 deletions src/Collection/Doctrine/ORM/EntityRepositoryBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public function find($specification, $lockMode = null, $lockVersion = null): ?ob
*
* @return EntityResult<V>
*/
public function filter(mixed $specification): EntityResult
public function query(mixed $specification): EntityResult
{
return $this->collectionRepo()->filter($specification);
return $this->collectionRepo()->query($specification);
}

public function getIterator(): \Traversable
Expand Down
6 changes: 3 additions & 3 deletions src/Collection/Doctrine/ObjectRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
interface ObjectRepository extends \Countable, \IteratorAggregate
{
/**
* Use with {@see self::filter()} to return all results.
* Use with {@see self::query()} to return all results.
*/
public const NONE = [];
public const ALL = [];

/**
* @param mixed|array|Criteria $specification
Expand All @@ -38,5 +38,5 @@ public function find(mixed $specification): ?object;
*
* @return Result<V>
*/
public function filter(mixed $specification): Result;
public function query(mixed $specification): Result;
}
25 changes: 13 additions & 12 deletions stubs/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Zenstruck\Collection\Doctrine\ObjectRepository;
use Zenstruck\Collection\Doctrine\ORM\EntityRepository;
use Zenstruck\Collection\LazyCollection;

use function PHPStan\Testing\assertType;
use function Zenstruck\collect;

Expand All @@ -12,21 +13,21 @@

assertType('Traversable<int, User>', $objectRepository->getIterator());
assertType('User|null', $objectRepository->find(1));
assertType('Zenstruck\Collection\Doctrine\Result<User>', $objectRepository->filter([]));
assertType('array<int, User>', $objectRepository->filter([])->eager()->all());
assertType('User|null', $objectRepository->filter([])->first());
assertType('Zenstruck\Collection\Doctrine\Result<bool|float|int|string>', $objectRepository->filter([])->asScalar());
assertType('Zenstruck\Collection\Doctrine\Result<string>', $objectRepository->filter([])->asString());
assertType('Zenstruck\Collection\Doctrine\Result<int>', $objectRepository->filter([])->asInt());
assertType('Zenstruck\Collection\Doctrine\Result<float>', $objectRepository->filter([])->asFloat());
assertType('Zenstruck\Collection\Doctrine\Result<array<string, mixed>>', $objectRepository->filter([])->asArray());
assertType('Zenstruck\Collection\Doctrine\Result<int>', $objectRepository->filter([])->as(fn(): int => 1));
assertType('Zenstruck\Collection\Page<int, User>', $objectRepository->filter([])->paginate());
assertType('Zenstruck\Collection\Doctrine\Result<User>', $objectRepository->query([]));
assertType('array<int, User>', $objectRepository->query(ObjectRepository::ALL)->eager()->all());
assertType('User|null', $objectRepository->query(ObjectRepository::ALL)->first());
assertType('Zenstruck\Collection\Doctrine\Result<bool|float|int|string>', $objectRepository->query(ObjectRepository::ALL)->asScalar());
assertType('Zenstruck\Collection\Doctrine\Result<string>', $objectRepository->query(ObjectRepository::ALL)->asString());
assertType('Zenstruck\Collection\Doctrine\Result<int>', $objectRepository->query(ObjectRepository::ALL)->asInt());
assertType('Zenstruck\Collection\Doctrine\Result<float>', $objectRepository->query(ObjectRepository::ALL)->asFloat());
assertType('Zenstruck\Collection\Doctrine\Result<array<string, mixed>>', $objectRepository->query(ObjectRepository::ALL)->asArray());
assertType('Zenstruck\Collection\Doctrine\Result<int>', $objectRepository->query(ObjectRepository::ALL)->as(fn(): int => 1));
assertType('Zenstruck\Collection\Page<int, User>', $objectRepository->query(ObjectRepository::ALL)->paginate());

/** @var EntityRepository<User> $ormRepository */

assertType('Zenstruck\Collection\Doctrine\ORM\EntityResult<int>', $ormRepository->filter([])->as(fn(): int => 1));
assertType('Zenstruck\Collection\Doctrine\ORM\EntityResult<Zenstruck\Collection\Doctrine\ORM\EntityWithAggregates<User>>', $ormRepository->filter([])->withAggregates());
assertType('Zenstruck\Collection\Doctrine\ORM\EntityResult<int>', $ormRepository->query(ObjectRepository::ALL)->as(fn(): int => 1));
assertType('Zenstruck\Collection\Doctrine\ORM\EntityResult<Zenstruck\Collection\Doctrine\ORM\EntityWithAggregates<User>>', $ormRepository->query(ObjectRepository::ALL)->withAggregates());

class User
{
Expand Down
14 changes: 7 additions & 7 deletions tests/Doctrine/ORM/EntityRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function detaches_entities_from_em_on_iterate(): void
*/
public function can_filter_with_array(): void
{
$objects = $this->createWithItems(3)->filter(['id' => 2]);
$objects = $this->createWithItems(3)->query(['id' => 2]);

$this->assertCount(1, $objects);
$this->assertSame('value 2', \iterator_to_array($objects)[0]->value);
Expand All @@ -126,7 +126,7 @@ public function can_filter_with_array(): void
*/
public function can_filter_with_none(): void
{
$objects = $this->createWithItems(3)->filter(ObjectRepository::NONE);
$objects = $this->createWithItems(3)->query(ObjectRepository::ALL);

$this->assertCount(3, $objects);
}
Expand All @@ -136,7 +136,7 @@ public function can_filter_with_none(): void
*/
public function can_filter_with_criteria(): void
{
$objects = $this->createWithItems(3)->filter(Criteria::create()->where(Criteria::expr()->eq('id', 2)));
$objects = $this->createWithItems(3)->query(Criteria::create()->where(Criteria::expr()->eq('id', 2)));

$this->assertCount(1, $objects);
$this->assertSame('value 2', \iterator_to_array($objects)[0]->value);
Expand All @@ -147,7 +147,7 @@ public function can_filter_with_criteria(): void
*/
public function can_filter_with_callable(): void
{
$objects = $this->createWithItems(3)->filter(function(QueryBuilder $qb, string $root) {
$objects = $this->createWithItems(3)->query(function(QueryBuilder $qb, string $root) {
$qb->andWhere($root.'.id = :id')->setParameter('id', 2);
});

Expand All @@ -162,9 +162,9 @@ public function can_filter_for_no_results(): void
{
$repo = $this->createWithItems(3);

$this->assertEmpty($repo->filter(['id' => 99]));
$this->assertEmpty($repo->filter(Criteria::create()->where(Criteria::expr()->eq('id', 99))));
$this->assertEmpty($repo->filter(function(QueryBuilder $qb, string $root) {
$this->assertEmpty($repo->query(['id' => 99]));
$this->assertEmpty($repo->query(Criteria::create()->where(Criteria::expr()->eq('id', 99))));
$this->assertEmpty($repo->query(function(QueryBuilder $qb, string $root) {
$qb->andWhere($root.'.id = :id')->setParameter('id', 99);
}));
}
Expand Down

0 comments on commit d0ee92a

Please sign in to comment.