ldc-ovoteam/app/Http/Requests/Back/Concerns/ResolvesBoTableParams.php
Jérémy Munsch bc69c9f64d
feat(products): implement GetBoPaginatedProductsQuery and input/output classes
feat(recipes): implement GetBoPaginatedRecipesQuery and input/output classes

feat(requests): add table parameter resolution for products and recipes
2026-11-05 12:17:58 +01:00

82 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests\Back\Concerns;
use Domain\Models\ValueObjects\Pagination;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Str;
use SortDirection;
trait ResolvesBoTableParams
{
public function getTableSortCol(string $default = 'name'): string
{
$routeName = $this->route()?->getName() ?? 'default';
if ($this->has('rst')) {
Session::forget([
"{$routeName}.sort_col",
"{$routeName}.sort_way",
"{$routeName}.sorted",
]);
}
if ($this->filled('sort_col')) {
$col = $this->string('sort_col')->value();
Session::put("{$routeName}.sort_col", $col);
Session::put("{$routeName}.sorted", true);
return $col;
}
/** @var string|null $cachedCol */
$cachedCol = Session::get("{$routeName}.sort_col");
return $cachedCol ?? $default;
}
public function getTableSortDirection(SortDirection $default = SortDirection::Ascending): SortDirection
{
$routeName = $this->route()?->getName() ?? 'default';
if ($this->filled('sort_way')) {
$way = strtolower($this->string('sort_way')->value()) === 'desc'
? SortDirection::Descending
: SortDirection::Ascending;
Session::put("{$routeName}.sort_way", $way === SortDirection::Descending ? 'desc' : 'asc');
Session::put("{$routeName}.sorted", true);
return $way;
}
/** @var string|null $cachedWay */
$cachedWay = Session::get("{$routeName}.sort_way");
if ($cachedWay !== null) {
return $cachedWay === 'desc' ? SortDirection::Descending : SortDirection::Ascending;
}
return $default;
}
public function getTablePagination(int $defaultPerPage = 12): Pagination
{
$routeName = Str::slug($this->route()?->getName() ?? 'unknown');
$cacheKey = "pagination.{$routeName}";
if ($this->filled('pagination')) {
$perPage = $this->integer('pagination');
Session::put($cacheKey, $perPage);
} else {
/** @var int|null $cachedPerPage */
$cachedPerPage = Session::get($cacheKey);
$perPage = $cachedPerPage ?? $defaultPerPage;
}
$page = $this->integer('page', 1);
return Pagination::ofPage(max(1, $page), max(1, $perPage));
}
}