forked from eatvio/eatvio-infra
29 lines
637 B
PHP
29 lines
637 B
PHP
<?php
|
|
|
|
namespace App\Rules;
|
|
|
|
use Closure;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
|
|
class MinWords implements ValidationRule
|
|
{
|
|
protected int $minWords;
|
|
|
|
public function __construct(int $minWords)
|
|
{
|
|
$this->minWords = $minWords;
|
|
}
|
|
|
|
/**
|
|
* Run the validation rule.
|
|
*/
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
{
|
|
$wordCount = count(preg_split('/\W+/u', $value, -1, PREG_SPLIT_NO_EMPTY));
|
|
|
|
if ($wordCount <= $this->minWords) {
|
|
$fail('validation.min_words_count')->translate(['min' => $this->minWords]);
|
|
}
|
|
}
|
|
}
|