ldc-gen-game-web/app/Http/Requests/ParticipateRequest.php
2022-01-10 17:57:52 +01:00

61 lines
1.9 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Models\Codes;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Kwaadpepper\Serial\Exceptions\SerialCasterException;
class ParticipateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize()
{
$this->merge([
'code' => is_string($this->code) ? Str::upper($this->code) : $this->code,
'accept_rules' => $this->accept_rules ? true : false
]);
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'accept_rules' => [
'required', 'boolean',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
if (!$value) {
$fail('Pour participer, vous devez accepter le règlement du jeu.');
}
}
],
'code' => [
'required', 'string',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
try {
$int = Codes::serialToInt($value);
if (!Codes::whereId(Codes::serialToInt($value))->exists()) {
throw new SerialCasterException();
}
} catch (SerialCasterException $e) {
Log::debug($e->getMessage(), $e->getTrace());
$fail('Code incorrect, veuillez réessayer.');
}
}
]
];
}
}