ldc-gen-game-web/app/Http/Requests/Bo/Codes/ExportCodesRequest.php

70 lines
1.9 KiB
PHP
Raw Permalink Normal View History

<?php
2026-10-02 12:09:46 +02:00
declare(strict_types=1);
2023-12-05 12:33:25 +01:00
namespace App\Http\Requests\Bo\Codes;
use App\Models\Code;
use Illuminate\Foundation\Http\FormRequest;
2023-12-05 12:33:25 +01:00
use Illuminate\Support\Facades\Gate;
class ExportCodesRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
2023-12-05 12:33:25 +01:00
public function authorize(): bool
{
2026-10-02 12:09:46 +02:00
return Gate::check('export', Code::class) && $this->json();
2023-12-05 12:33:25 +01:00
}
/**
* Prepare the data for validation.
*/
protected function prepareForValidation(): void
{
2023-11-23 17:31:28 +01:00
$codesAvailableToExport = Code::query()->where('code_exported', false);
2026-10-02 12:09:46 +02:00
/** @var int|null $lastExportedId */
$lastExportedId = Code::query()->where('code_exported', true)->max('id');
if ($lastExportedId) {
2026-10-02 12:09:46 +02:00
$codesAvailableToExport->where('id', '>', $lastExportedId);
}
2026-10-02 12:09:46 +02:00
$this->merge([
2026-10-02 12:09:46 +02:00
'available_export' => $codesAvailableToExport->count(),
]);
}
/**
* Get the validation rules that apply to the request.
*
2023-12-05 12:33:25 +01:00
* @return array<string,string>
*/
2023-12-05 12:33:25 +01:00
public function rules(): array
{
return [
2026-10-02 12:09:46 +02:00
'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.
*
2023-12-05 12:33:25 +01:00
* @return array<string,string>
*/
public function attributes(): array
{
return [
2026-10-02 12:09:46 +02:00
'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)'),
];
}
}