48 lines
1.2 KiB
PHP
Executable File
48 lines
1.2 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Rules;
|
|
|
|
use Closure;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
use Illuminate\Contracts\Validation\ValidatorAwareRule;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
/**
|
|
* Validation rule for arithmetic captcha.
|
|
*
|
|
* Validates against session-stored answer (preferred) or input-hidden fallback.
|
|
* Expected input field: captcha (4 characters from arithmetic expression).
|
|
*/
|
|
class Captcha implements ValidationRule, ValidatorAwareRule
|
|
{
|
|
/**
|
|
* Run the validation rule.
|
|
*
|
|
* @param Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail
|
|
*/
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
{
|
|
$expected = session('captcha_answer');
|
|
|
|
if ($expected === null) {
|
|
$fail('O captcha expirou. Recarregue a página.');
|
|
return;
|
|
}
|
|
|
|
if ((string) $value !== (string) $expected) {
|
|
$fail('O captcha informado está incorreto.');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set the validator instance.
|
|
*/
|
|
public function setValidator(Validator $validator): void
|
|
{
|
|
// No-op: rule does not require validator instance.
|
|
}
|
|
}
|