snipe-it/app/Rules/AlphaEncrypted.php

30 lines
866 B
PHP
Raw Normal View History

<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\Crypt;
class AlphaEncrypted implements ValidationRule
{
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
try {
2024-11-14 09:42:35 -08:00
$attributeName = trim(preg_replace('/_+|snipeit|\d+/', ' ', $attribute));
$decrypted = Crypt::decrypt($value);
if (!ctype_alpha($decrypted) && !is_null($decrypted)) {
2024-11-19 06:59:47 -08:00
$fail(trans('validation.alpha', ['attribute' => $attributeName]));
}
} catch (\Exception $e) {
report($e);
2024-11-14 11:45:47 -08:00
$fail(trans('general.something_went_wrong'));
}
}
}