ldc-le-gaulois-pro/app/Http/Requests/Bo/UpdateOrderRequest.php
2026-05-11 14:21:12 +02:00

68 lines
1.7 KiB
PHP

<?php
namespace App\Http\Requests\Bo;
use App\Interfaces\OrderableInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Http\FormRequest;
class UpdateOrderRequest extends FormRequest
{
/**
* Attribute model needed.
*
* @var Model $orderable
*/
private Model $orderable;
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize(): bool
{
return auth('backend')->check();
}
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation(): void
{
$this->orderable = collect($this->route()->parameters())
->first(fn($param) => $param instanceof Model);
$groupField = $this->orderable->getOrderGroupField();
if ($groupField) {
$this->orderable::query()->where($groupField, $this->orderable->{$groupField});
}
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
return [
'priority_order' => [
'required',
'integer',
'min:1',
sprintf('max:%s', $this->orderable->max('priority_order')),
function ($value, $fail) {
// phpcs:ignore Generic.Files.LineLength.TooLong
if ($this->orderable instanceof OrderableInterface && intval($value) === $this->orderable->getPriorityOrder()) {
$fail(trans('validation.custom.same_priority'));
}
},
],
];
}
}