ldc-le-gaulois-pro/app/Http/Requests/Bo/UpdateOrderRequest.php
2026-03-25 10:45:26 +01:00

72 lines
2 KiB
PHP

<?php
namespace App\Http\Requests\Bo;
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',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
function ($attribute, $value, $fail) {
if ($this->orderable && intval($value) < 1) {
$fail(trans('validation.custom.min_priority'));
}
},
sprintf('max:%s', $this->orderable->max('priority_order')),
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
function ($attribute, $value, $fail) {
if ($this->orderable && intval($value) === $this->orderable->priority_order) {
$fail(trans('validation.custom.same_priority'));
}
},
],
];
}
}