-
-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathContainsString.php
More file actions
24 lines (19 loc) · 650 Bytes
/
ContainsString.php
File metadata and controls
24 lines (19 loc) · 650 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class ContainsString implements ValidationRule
{
public function __construct(
protected string $needle,
protected bool $caseSensitive = false
) {}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$haystack = $this->caseSensitive ? $value : strtolower($value);
$needle = $this->caseSensitive ? $this->needle : strtolower($this->needle);
if (! str_contains($haystack, $needle)) {
$fail("The :attribute must contain '{$this->needle}'.");
}
}
}