ldc-gen-game-web/app/Http/Requests/Bo/Codes/ExportCodesRequest.php
2026-10-20 12:27:49 +02:00

69 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests\Bo\Codes;
use App\Models\Code;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
class ExportCodesRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return Gate::check('export', Code::class) && $this->json();
}
/**
* Prepare the data for validation.
*/
protected function prepareForValidation(): void
{
$codesAvailableToExport = Code::query()->where('code_exported', false);
/** @var int|null $lastExportedId */
$lastExportedId = Code::query()->where('code_exported', true)->max('id');
if ($lastExportedId) {
$codesAvailableToExport->where('id', '>', $lastExportedId);
}
$this->merge([
'available_export' => $codesAvailableToExport->count(),
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string,string>
*/
public function rules(): array
{
return [
'nb_code_tot' => 'required|integer|min:1|lte:available_export',
'nb_code_fic' => 'required|integer|min:1',
'nb_code_ligne' => 'required|integer|min:1',
'available_export' => 'required|integer',
];
}
/**
* Get custom attributes for validator errors.
*
* @return array<string,string>
*/
public function attributes(): array
{
return [
'nb_code_fic' => trans('nombre total de codes à exporter'),
'nb_code_tot' => trans('nombre de codes par fichier'),
'nb_code_ligne' => trans('nombre de codes par ligne'),
'available_export' => trans('nombre de codes restant à exporté(s)'),
];
}
}