bartlomiejbeta/filter-sorter-bundle

v2.1.0 2018-08-31 11:35 UTC

README

This Symfony bundle aims to provide structurized filtering and sorting with usage of the specification pattern (see doctrine-specification). It was design to use in the api applications but it could be also used with standard web apps.

Provides feature like:
  • FilterQueryManager - it collects all filter services and gives you queryBuilder with applied specifications.
  • FilterParamConverter - provide filter object by the data in query string
  • SortParamConverter - provide sort object by the data in query string

Build Status

Installation

1. Install via composer:
composer require bartlomiejbeta/filter-sorter-bundle
2. Register bundle in AppKernel:
public function registerBundles()
{
    $bundles = array(
        // ...
        new BartB\FilterSorterBundle\FilterSorterBundle(),
    );
}

Usage

I assume that you already have entities, and so one.

1. Change parent of the entities to AbstractEntitySpecificationAwareRepository:

example:

class CarRepository extends AbstractEntitySpecificationAwareRepository
{
}
2. Add specifications (example)
3. Add Collection filter (example)
4. Add Sort Enum (example)
5. Build and register your first service adapter (adapter example, register adapter example)
5. Run (standard, api with paramConverters)
$carCollectionFilter = (new CarCollectionFilter())->setGearboxType('automatic');

$carSorter     = new CarSort(CarSort::FUEL_TYPE);
$sortDirection = new SortDirectionType(SortDirectionType::DESC);
		
/** @var FilterQueryManager $filterQueryManager */
$filterQueryManager = $this->filterQueryManager;
$carRepository      = $this->repostioryCar;
$sort 	            = new Sort($sortDirection, $carSorter);
$limit              = new Limit(1);

$basicRepositoryDTO = new BasicRepositoryDTO($carRepository);
$queryAttributesDTO = new QueryAttributesDTO($carCollectionFilter, $sort, $limit);

$queryBuilder = $filterQueryManager->getQueryBuilder($basicRepositoryDTO, $queryAttributesDTO);
$query        = $queryBuilder->getQuery();
$result       = $query->execute();

TODO: Upgrade example to symfony 3.4 Also I prepared full stand alone example