Download the PHP package tilleuls/sorter without Composer
On this page you can find all versions of the php package tilleuls/sorter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tilleuls/sorter
More information about tilleuls/sorter
Files in tilleuls/sorter
Package sorter
Short Description A library to dynamically sort collections / queries
License MIT
Informations about the package sorter
Sorter
Sorter is a PHP column sorting library that allows you to apply sorts of any kind of data source.
Features
- Sorts any kind of data source (array, Doctrine ORM, and plain SQL built-in)
- Sorts by multiple columns
- Factorize sorting logic into definitions classes
- Process HTTP request
- Symfony Bundle
- Twig extension
Installation
Optionnal : enable symfony bundle
Usage
Sorter provides a SorterFactory class that allows you to sort your data source.
The factory builds a sorter instance and requires an applier to apply the sort to the data source.
The classic way to use it is the following :
- Create a
SorterFactoryinstance (if you are not using Symfony) - Create a sorter instance using the factory
- Define the sorting columns, the default sort, and eventually the query prefix
Basic sorting
The Sorter\Sorter class providers the following methods to define your sorts :
$sorter->add(string $field, string $path): Adds a new column to the sorter.$fieldis the name of the column, and$pathis the path to the column in your data source. The path can be a SQL expression, a property name, or even an array key index.$sorter->addDefault(string $field, string $direction)/$sorter->removeDefault(string $field): Adds a default sort to the sorter.$fieldis the name of the column, and$directionis the direction of the sort (Sort::ASCorSort::DESC).$sorter->setPrefix(string $prefix): Sets the prefix to be used in the query string. This is useful if you want to use several sorters in the same page.
Symfony usage
With Symfony, the SorterFactory is available as a service.
Definition class
You can factorize your sorting logic into a definition class. Definition classes are useful if you want to reuse the same sorting logic in several places.
Twig extension
You can use the SorterExtension to render the sorting links in your Twig templates.
The twig extension provides the following functions:
sorter_link: Renders a ready to use and request aware sorting link.sorter_url: Renders a URL for the sorting link. This is useful if you want to use your own HTML.sorter_direction: Returns the direction of the sort for a given column. This is useful if you want to use your own HTML.
Example
Object sorting with ArrayApplier
By default, ArrayApplier use the PHP spaceship operator <=> to compare values.
With some objects, you may want to use a custom comparison function.
You can use the Comparable interface to define a custom comparison function for your objects.
php