unzeroun/sorter

This package is abandoned and no longer maintained. The author suggests using the yohang/sorter package instead.

A library to dynamically sort collections / queries

1.0.0 2025-03-22 21:05 UTC

README

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.

tests Mutation testing badge Coverage Status

Features

  • Sorts any kind of data source
  • Sorts by multiple columns
  • Factorise sorting logic into definitions classes
  • Process HTTP request
  • Symfony Bundle
  • Twig extension

Installation

 $ composer require unzeroun/sorter

Optionnal : enable symfony bundle

<?php

return [
    // ...
    UnZeroUn\Sorter\Extension\Symfony\Bundle\UnZeroUnSorterBundle::class => ['all' => true],
];

Usage

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.

Basic sorting

// 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);

Symfony usage

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(),
                ],
            ),
        );
    }
}

Definition class

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(),
                ],
            ),
        );
    }
}