This commit is contained in:
60
api/app/Rules/CnsTermoValido.php
Normal file
60
api/app/Rules/CnsTermoValido.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Illuminate\Support\Str;
|
||||
use App\Models\Cns\TermoInvalido;
|
||||
|
||||
class CnsTermoValido implements Rule
|
||||
{
|
||||
private $options;
|
||||
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the validation rule passes.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value)
|
||||
{
|
||||
$value = ' ' . trim($value) . ' ';
|
||||
$valid = true;
|
||||
|
||||
foreach (TermoInvalido::pluck('descricao') as $termo) {
|
||||
|
||||
if (str_contains($value, " {$termo} ")) {
|
||||
|
||||
$valid = false;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $valid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
if (array_key_exists('message', $this->options)) {
|
||||
|
||||
return $this->options['message'];
|
||||
}
|
||||
|
||||
$attribute = array_key_exists('attribute', $this->options)
|
||||
? $this->options['attribute'] : ':attribute';
|
||||
|
||||
return "{$attribute} possui um termo inválido.";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user