Warning
This repo is archived and the project now lives here : https://github.com/yohang/sorter
Sorter is a PHP column sorting library that allows you to apply sorts of any kind of data source.
- Sorts any kind of data source
- Sorts by multiple columns
- Factorise sorting logic into definitions classes
- Process HTTP request
- Symfony Bundle
- Twig extension
 $ composer require unzeroun/sorter<?php
return [
    // ...
    UnZeroUn\Sorter\Extension\Symfony\Bundle\UnZeroUnSorterBundle::class => ['all' => true],
];Sorter provides a SorterFactory class that allows you to sort your data source.
The factory require an applier to apply the sort to the data source.
// Create the sorter factory (useless with Symfony)
$factory = new SorterFactory([new DoctrineORMApplier()]);
// Create your sorter definition
$sorter = $factory->createSorter()
    ->add('title', 'p.title')
    ->add('date', 'p.date')
    ->addDefault('date', Sort::ASC);
// Handle takes an array of data and transform it to a Sort object
$sorter->handle([]);
// Apply the sort to the data
$data = $sorter->sort($data);With Symfony, the SorterFactory is available as a service.
class IndexController
{
    public function __construct(
        private SorterFactory $factory,
        private PostRepository $repository,
        private Environment $twig,
    ) {
    }
    
    public function index(Request $request)
    {
        $sorter = $this->factory->createSorter()
            ->add('title', 'p.title')
            ->add('date', 'p.date')
            ->addDefault('date', Sort::ASC);
    
        $sorter->handleRequest($request);
        $qb = $sorter->sort($this->repository->createQueryBuilder('p'));
    
        return new Response(
            $this->twig->render(
                'array-sort.html.twig',
                [
                    'sorter' => $sorter,
                    'data' => $qb->getQuery()->getResult(),
                ],
            ),
        );
    }
}You can factorise your sorting logic into a definition class.
use UnZeroUn\Sorter\Definition;
use UnZeroUn\Sorter\Sorter;
class PostSortDefinition implements Definition
{
    public function buildSorter(Sorter $sorter): void
    {
        $sorter
            ->add('title', 'p.title')
            ->add('date', 'p.date')
            ->addDefault('date', Sort::ASC);
    }
}class IndexController
{
    public function __construct(
        private SorterFactory $factory,
        private PostRepository $repository,
        private Environment $twig,
    ) {
    }
    
    public function index(Request $request)
    {
        $sorter = $this->factory->createSorter(new PostSortDefinition());
        $sorter->handleRequest($request);
        $qb = $sorter->sort($this->repository->createQueryBuilder('p'));
    
        return new Response(
            $this->twig->render(
                'array-sort.html.twig',
                [
                    'sorter' => $sorter,
                    'data' => $qb->getQuery()->getResult(),
                ],
            ),
        );
    }
}