1. Go to this page and download the library: Download unzeroun/sorter 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/ */
// 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);
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(),
],
),
);
}
}
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(),
],
),
);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.