Add DatatableRequest and it's argument resolver
parent
7813c93e9b
commit
7a09e3f303
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\ArgumentResolver;
|
||||
|
||||
use App\Request\DatatableRequest;
|
||||
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
|
||||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
|
||||
|
||||
/**
|
||||
* A value resolver for {@see DatatableRequest}
|
||||
*/
|
||||
#[AutoconfigureTag('controller.argument_value_resolver', ['priority' => 110])]
|
||||
class DatatableRequestArgumentResolver implements ValueResolverInterface {
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function resolve (Request $request, ArgumentMetadata $argument): iterable {
|
||||
//region Check if parameter is supported (
|
||||
if ($argument->getType() !== DatatableRequest::class) {
|
||||
return [];
|
||||
}
|
||||
//endregion
|
||||
|
||||
return [new DatatableRequest($request)];
|
||||
}
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\Request;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* A Datatables.net request (AJAX)
|
||||
*/
|
||||
class DatatableRequest {
|
||||
/**
|
||||
* @var string|null The searched value, Null if none
|
||||
*/
|
||||
private ?string $search;
|
||||
/**
|
||||
* @var array{column: string, direction: string}[] The sorts : “column” = column name, “direction” = the sort direction (“asc” / “desc”)
|
||||
*/
|
||||
private array $sorts;
|
||||
/**
|
||||
* @var int Paging: start position (aka offset)
|
||||
*/
|
||||
private int $pagingStart;
|
||||
/**
|
||||
* @var int Paging: length (aka number of elements)
|
||||
*/
|
||||
private int $pagingLength;
|
||||
|
||||
/**
|
||||
* Initialization from an HTTP request
|
||||
*
|
||||
* @param Request $request The HTTP request
|
||||
*/
|
||||
public function __construct (Request $request) {
|
||||
$this->search = $request->request->all('search')['value'] ?? null;
|
||||
|
||||
//region Sorts
|
||||
$this->sorts = [];
|
||||
foreach (($request->request->all('order') ?? []) as $orderPosition => $order) {
|
||||
$this->sorts[$orderPosition] = [
|
||||
'column' => $order['name'] ?? $order['column'],
|
||||
'direction' => $order['dir'] ?? 'asc',
|
||||
];
|
||||
}
|
||||
//endregion
|
||||
|
||||
$this->pagingStart = $request->request->get('start');
|
||||
$this->pagingLength = $request->request->get('length');
|
||||
}
|
||||
|
||||
/**
|
||||
* The searched value, Null if none
|
||||
*
|
||||
* @return string|null The searched value, Null if none
|
||||
*/
|
||||
public function getSearch (): ?string {
|
||||
return $this->search;
|
||||
}
|
||||
|
||||
/**
|
||||
* The sorts : “column” = column name, “direction” = the sort direction (“asc” / “desc”)
|
||||
*
|
||||
* @return array{column: string, direction: string}[] The sorts : “column” = column name, “direction” = the sort direction (“asc” / “desc”)
|
||||
*/
|
||||
public function getSorts (): array {
|
||||
return $this->sorts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Paging: start position (aka offset)
|
||||
*
|
||||
* @return int Paging: start position (aka offset)
|
||||
*/
|
||||
public function getPagingStart (): int {
|
||||
return $this->pagingStart;
|
||||
}
|
||||
/**
|
||||
* Paging: length (aka number of elements)
|
||||
*
|
||||
* @return int Paging: length (aka number of elements)
|
||||
*/
|
||||
public function getPagingLength (): int {
|
||||
return $this->pagingLength;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue