<?php
namespace App\Doctrine\Repository;
use App\Model\Searching\AbstractSearch;
use App\Util\Pagination;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Tools\Pagination\Paginator;
/**
* Basic implementation of the SearchableRepositoryInterface.
*/
trait SearchableRepositoryTrait
{
/**
* {@inheritDoc}
*/
public function findBySearch(
AbstractSearch $search,
?int $limit = null,
?int $offset = null,
?QueryBuilder $qb = null
): Paginator
{
// force interface
if (!$this instanceof SearchableRepositoryInterface) {
throw new \RuntimeException();
}
// default limit and offset
if (!$limit) {
$limit = $search->getLimit() ?: Pagination::PAGE_LIMIT;
}
if (!$offset) {
$offset = $search->getOffset() ?: 0;
}
// creat the raw query builder
$qb = $this->qbBySearch(
$search,
$qb,
);
return Pagination::paginateThenQuery(
$qb,
$limit,
$offset,
);
}
}