1. Go to this page and download the library: Download isma/api-filters-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Isma\ApiFiltersBundle\Attribute\ApiFilter;
use Isma\ApiFiltersBundle\ValueObject\Filters;
use Isma\ApiFiltersBundle\ValueObject\FilterType;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
final class UserController
{
#[Route('/api/users', methods: ['GET'])]
#[ApiFilter(name: 'firstname', allowedTypes: [FilterType::Eq->value, FilterType::Like->value])]
#[ApiFilter(name: 'lastname')]
#[ApiFilter(name: 'status', enumClass: UserStatus::class)]
public function list(Filters $filters): JsonResponse
{
// $filters is automatically resolved from the query string
}
}
use Isma\ApiFiltersBundle\Filter\FilterApplierInterface;
final class UserRepository
{
public function __construct(
private FilterApplierInterface $filterApplier,
) {
}
public function findByFilters(Filters $filters): array
{
$qb = $this->createQueryBuilder('u');
$this->filterApplier->apply($qb, $filters, [
'firstname' => 'u.firstname',
'lastname' => 'u.lastname',
'status' => 'u.status',
]);
return $qb->getQuery()->getResult();
}
}
// Only allow exact match on the "role" field
#[ApiFilter(name: 'role', allowedTypes: [FilterType::Eq->value])]
use Isma\ApiFiltersBundle\Filter\DBAL\DbalFilterApplierInterface;
final class UserRepository
{
public function __construct(
private DbalFilterApplierInterface $filterApplier,
private Connection $connection,
) {
}
public function findByFilters(Filters $filters): array
{
$qb = $this->connection->createQueryBuilder()
->select('t.*')
->from('users', 't');
$this->filterApplier->apply($qb, $filters, [
'firstname' => 't.firstname',
'lastname' => 't.lastname',
'status' => 't.status',
]);
return $qb->executeQuery()->fetchAllAssociative();
}
}
use Isma\ApiFiltersBundle\Filter\SQL\SqlFilterApplierInterface;
use Isma\ApiFiltersBundle\Filter\SQL\SqlQueryContext;
final class UserRepository
{
public function __construct(
private SqlFilterApplierInterface $filterApplier,
private \PDO $pdo,
) {
}
public function findByFilters(Filters $filters): array
{
$context = new SqlQueryContext();
$this->filterApplier->apply($context, $filters, [
'firstname' => 'firstname',
'lastname' => 'lastname',
'status' => 'status',
]);
$sql = 'SELECT * FROM users';
if ($where = $context->getWhereClause()) {
$sql .= ' WHERE ' . $where;
}
if ($orderBy = $context->getOrderByClause()) {
$sql .= ' ORDER BY ' . $orderBy;
}
$stmt = $this->pdo->prepare($sql);
foreach ($context->getParameters() as $name => $value) {
$stmt->bindValue(':' . $name, $value);
}
$stmt->execute();
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
}
use Doctrine\ORM\QueryBuilder;
use Isma\ApiFiltersBundle\Filter\FilterStrategyInterface;
final class BetweenFilterStrategy implements FilterStrategyInterface
{
public function getType(): string
{
return 'between';
}
public function apply(QueryBuilder $queryBuilder, string $column, mixed $value, string $parameterName): void
{
// Expects value as [min, max]
$queryBuilder->andWhere(\sprintf('%s BETWEEN :%s_min AND :%s_max', $column, $parameterName, $parameterName))
->setParameter($parameterName.'_min', $value[0])
->setParameter($parameterName.'_max', $value[1]);
}
}
use Doctrine\DBAL\Query\QueryBuilder;
use Isma\ApiFiltersBundle\Filter\DBAL\DbalFilterStrategyInterface;
final class BetweenFilterStrategy implements DbalFilterStrategyInterface
{
public function getType(): string
{
return 'between';
}
public function apply(QueryBuilder $queryBuilder, string $column, mixed $value, string $parameterName): void
{
// Expects value as [min, max]
$queryBuilder->andWhere(\sprintf('%s BETWEEN :%s_min AND :%s_max', $column, $parameterName, $parameterName))
->setParameter($parameterName.'_min', $value[0])
->setParameter($parameterName.'_max', $value[1]);
}
}
use Isma\ApiFiltersBundle\Filter\SQL\SqlFilterStrategyInterface;
use Isma\ApiFiltersBundle\Filter\SQL\SqlQueryContext;
final class BetweenFilterStrategy implements SqlFilterStrategyInterface
{
public function getType(): string
{
return 'between';
}
public function apply(SqlQueryContext $context, string $column, mixed $value, string $parameterName): void
{
// Expects value as [min, max]
$context->andWhere(\sprintf('%s BETWEEN :%s_min AND :%s_max', $column, $parameterName, $parameterName))
->setParameter($parameterName.'_min', $value[0])
->setParameter($parameterName.'_max', $value[1]);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.