Skip to content

Commit

Permalink
add generics.
Browse files Browse the repository at this point in the history
  • Loading branch information
Warxcell committed Apr 15, 2022
1 parent 7c770b0 commit 35f62f0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Arxy\DoctrineORMFilters;

use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\QueryBuilder;
use InvalidArgumentException;

Expand All @@ -12,10 +14,17 @@
use function is_array;
use function is_callable;

/**
* @template T of string
* @template V of object
*/
trait Filters
{
private array $filters;

/**
* @return array<T, Closure(QueryBuilder, string, mixed=): void>
*/
abstract public function getFilters(): array;

private function createFilters(): array
Expand All @@ -27,7 +36,10 @@ private function createFilters(): array
return $this->filters;
}

private function getFilter($name): callable
/**
* @param T $name
*/
private function getFilter(string $name): callable
{
$this->createFilters();

Expand All @@ -41,6 +53,9 @@ private function getFilter($name): callable
/** @return QueryBuilder */
abstract public function createQueryBuilder($alias, $indexBy = null);

/**
* @param iterable<T, mixed> $filterBy
*/
public function createQueryBuilderByFilters(string $alias, iterable $filterBy, string $indexBy = null): QueryBuilder
{
$queryBuilder = $this->createQueryBuilder($alias, $indexBy);
Expand All @@ -56,6 +71,9 @@ public function createQueryBuilderByFilters(string $alias, iterable $filterBy, s
return $queryBuilder;
}

/**
* @param T $filterName
*/
public function appendFilter(QueryBuilder $queryBuilder, string $alias, string $filterName, ...$values): bool
{
if (isset($queryBuilder->filters[$filterName])) {
Expand All @@ -72,21 +90,39 @@ public function appendFilter(QueryBuilder $queryBuilder, string $alias, string $
return $queryBuilder->filters[$filterName] = true;
}

/**
* @param iterable<T, mixed> $filterBy
* @return V | null
* @throws NonUniqueResultException
*/
public function findOneByFilters(iterable $filterBy): ?object
{
return $this->createQueryBuilderByFilters('entity', $filterBy)->getQuery()->getOneOrNullResult();
}

/**
* @param iterable<T, mixed> $filterBy
* @return V[]
*/
public function findByFilters(iterable $filterBy): array
{
return $this->createQueryBuilderByFilters('entity', $filterBy)->getQuery()->getResult();
}

/**
* @param iterable<T, mixed> $filterBy
* @return V
* @throws NonUniqueResultException
* @throws NoResultException
*/
public function getSingleResultByFilters(iterable $filterBy): object
{
return $this->createQueryBuilderByFilters('entity', $filterBy)->getQuery()->getSingleResult();
}

/**
* @param iterable<T, mixed> $filterBy
*/
public function countByFilters(iterable $filterBy): int
{
return (int)$this
Expand Down
8 changes: 8 additions & 0 deletions src/HasFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\QueryBuilder;

/**
* @template V of object
*/
interface HasFilters
{
public function createQueryBuilderByFilters(
Expand All @@ -19,13 +22,18 @@ public function createQueryBuilderByFilters(
public function appendFilter(QueryBuilder $queryBuilder, string $alias, string $filterName, ...$values): bool;

/**
* @return V | null
* @throws NonUniqueResultException
*/
public function findOneByFilters(iterable $filterBy): ?object;

/**
* @return V[]
*/
public function findByFilters(iterable $filterBy): array;

/**
* @return V
* @throws NoResultException
* @throws NonUniqueResultException
*/
Expand Down

0 comments on commit 35f62f0

Please sign in to comment.