Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/AllowedSort.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Spatie\QueryBuilder\Enums\SortDirection;
use Spatie\QueryBuilder\Exceptions\InvalidDirection;
use Spatie\QueryBuilder\Sorts\Sort;
use Spatie\QueryBuilder\Sorts\SortsCallback;
use Spatie\QueryBuilder\Sorts\SortsField;

class AllowedSort
Expand Down Expand Up @@ -54,6 +55,11 @@ public static function custom(string $name, Sort $sortClass, ?string $internalNa
return new static($name, $sortClass, $internalName);
}

public static function callback(string $name, $callback, ?string $internalName = null): self
{
return new static($name, new SortsCallback($callback), $internalName);
}

public function getName(): string
{
return $this->name;
Expand Down
25 changes: 25 additions & 0 deletions src/Sorts/SortsCallback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Spatie\QueryBuilder\Sorts;

use Illuminate\Database\Eloquent\Builder;

class SortsCallback implements Sort
{
/**
* @var callable a PHP callback of the following signature:
* `function (\Illuminate\Database\Eloquent\Builder $builder, bool $descending, string $property)`
*/
private $callback;

public function __construct($callback)
{
$this->callback = $callback;
}

/** {@inheritdoc} */
public function __invoke(Builder $query, bool $descending, string $property)
{
return call_user_func($this->callback, $query, $descending, $property);
}
}
77 changes: 77 additions & 0 deletions tests/SortsCallbackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Spatie\QueryBuilder\Tests;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Spatie\QueryBuilder\AllowedSort;
use Spatie\QueryBuilder\QueryBuilder;
use Spatie\QueryBuilder\Tests\Concerns\AssertsCollectionSorting;
use Spatie\QueryBuilder\Tests\TestClasses\Models\TestModel;
use Illuminate\Http\Request;

class SortsCallbackTest extends TestCase
{
use AssertsCollectionSorting;

/** @var \Illuminate\Support\Collection */
protected $models;

public function setUp(): void
{
parent::setUp();

DB::enableQueryLog();

$this->models = factory(TestModel::class, 5)->create();
}

/** @test */
public function it_should_sort_by_closure()
{
$sortedModels = $this
->createQueryFromSortRequest('callback')
->allowedSorts(AllowedSort::callback('callback', function (Builder $query, $descending) {
$query->orderBy('name', $descending ? 'DESC' : 'ASC');
}))
->get();

$this->assertQueryExecuted('select * from `test_models` order by `name` asc');
$this->assertSortedAscending($sortedModels, 'name');
}

/** @test */
public function it_should_sort_by_array_callback()
{
$sortedModels = $this
->createQueryFromSortRequest('callback')
->allowedSorts(AllowedSort::callback('callback', [$this, 'sortCallback']))
->get();

$this->assertQueryExecuted('select * from `test_models` order by `name` asc');
$this->assertSortedAscending($sortedModels, 'name');
}

public function sortCallback(Builder $query, $descending)
{
$query->orderBy('name', $descending ? 'DESC' : 'ASC');
}

protected function createQueryFromSortRequest(string $sort): QueryBuilder
{
$request = new Request([
'sort' => $sort,
]);

return QueryBuilder::for(TestModel::class, $request);
}

protected function assertQueryExecuted(string $query)
{
$queries = array_map(function ($queryLogItem) {
return $queryLogItem['query'];
}, DB::getQueryLog());

$this->assertContains($query, $queries);
}
}