ldc-tousenselle/app/Http/Requests/ParticipationRequest.php
Jérémy Munsch f7a3f5d5f9 first draw
2021-06-14 09:34:42 +02:00

63 lines
2.1 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Models\EanCode;
use App\Models\Participation;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Carbon;
class ParticipationRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'magasin' => 'required|exists:magasins,id',
'buydate' => 'required|date_format:Y-m-d|before:' . Carbon::now()->addDay()->format('Y-m-d'),
'ean' => [
'required',
'numeric',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
if (strlen((string)$value) !== 7 and strlen((string)$value) !== 13) {
$fail(trans('Le code ean doit être de 7 ou 13 chiffres'));
}
},
'exists:ean_codes,ean',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
$ean = EanCode::where('ean', $value)->first();
if (
Participation::where('ean_code_id', $ean->id ?? null)
->where('user_id', auth()->user()->id)->exists()
) {
$fail(trans('Vous avez déjà utilisé ce type de produit pour jouer, vous ne pouvez participer une nouvelle fois avec ce même type de produit'));
}
},
]
];
}
public function messages()
{
return [
'buydate.before' => 'Marty dit: Vous ne pouvez indiquer une date dans le futur',
'ean.exists' => 'Le produit donné n\'est pas éligible pour participer à ce jeu',
];
}
}