|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\ValidationRules\Rules; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Validation\Rule; |
|
6
|
|
|
use Illuminate\Support\Facades\Validator; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
class CommaSeparatedEmails implements Rule |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var int|null */ |
|
12
|
|
|
protected $minimum; |
|
13
|
|
|
|
|
14
|
|
|
/** @var int|null */ |
|
15
|
|
|
protected $maximum; |
|
16
|
|
|
|
|
17
|
|
|
protected $message = ''; |
|
18
|
|
|
|
|
19
|
|
|
public function passes($attribute, $value) |
|
20
|
|
|
{ |
|
21
|
|
|
[$validEmails, $invalidEmails] = collect(explode(',', $value)) |
|
|
|
|
|
|
22
|
|
|
->map(function (string $rawEmail) { |
|
23
|
|
|
return trim($rawEmail); |
|
24
|
|
|
}) |
|
25
|
|
|
->partition(function (string $email) { |
|
26
|
|
|
return $this->isValidEmail($email); |
|
27
|
|
|
}); |
|
28
|
|
|
|
|
29
|
|
|
if ($invalidEmails->count() === 1) { |
|
30
|
|
|
$this->message = __('validation.email', ['attribute' => $invalidEmails->first()]); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
return false; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if ($invalidEmails->count() > 1) { |
|
36
|
|
|
$this->message = __('validation.emails', ['attribute' => $invalidEmails->implode(',')]); |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
return false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if ($validEmails->unique()->count() !== $validEmails->count()) { |
|
42
|
|
|
$this->message = __('validation.unique_emails'); |
|
|
|
|
|
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
if (! is_null($this->minimum)) { |
|
49
|
|
|
if ($validEmails->count() < $this->minimum) { |
|
50
|
|
|
$this->message = __('validation.minimum_emails', [ |
|
|
|
|
|
|
51
|
|
|
'actualCount' => $invalidEmails->implode(','), |
|
52
|
|
|
'expectedMinimum' => $this->minimum, |
|
53
|
|
|
'emailword' => Str::plural('e-mail address', $this->minimum) |
|
54
|
|
|
]); |
|
55
|
|
|
|
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (! is_null($this->maximum)) { |
|
61
|
|
|
if ($validEmails->count() > $this->maximum) { |
|
62
|
|
|
$this->message = __('validation.maximum_emails', [ |
|
|
|
|
|
|
63
|
|
|
'actualCount' => $invalidEmails->implode(','), |
|
64
|
|
|
'expectedMaximum' => $this->maximum, |
|
65
|
|
|
'emailword' => Str::plural('e-mail address', $this->maximum) |
|
66
|
|
|
]); |
|
67
|
|
|
|
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function message() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->message; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function min(int $minimum) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->minimum = $minimum; |
|
83
|
|
|
|
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function max(int $maximum) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->maximum = $maximum; |
|
90
|
|
|
|
|
91
|
|
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
protected function isValidEmail(string $email): bool |
|
95
|
|
|
{ |
|
96
|
|
|
return Validator::make(['email' => $email], ['email' => 'email'])->passes(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.