This commit is contained in:
2026-02-03 15:31:29 +05:00
commit 326c677e8d
2800 changed files with 1489388 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class SortableColumnRule implements ValidationRule
{
/**
* Allowed columns for sorting
*
* @var array<string>
*/
protected array $allowedColumns;
/**
* Sortable column rule
*/
public function __construct(array $allowedColumns)
{
$this->allowedColumns = $allowedColumns;
}
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
// Format should be "coloumn-sort", example: 'created_at-descending'
$request_data = explode('-', $value);
if (count($request_data) !== 2) {
$fail(__('Format should be: "coloumn-sort"'));
return;
}
[$column, $sort] = $request_data;
if (! in_array($column, $this->allowedColumns)) {
$fail(__("Sorting by this {$column} is not allowed"));
return;
}
if (! in_array($sort, ['descending', 'ascending'])) {
$fail(__('You can only sort by ascending or descending!'));
return;
}
}
}