Completed
Push — master ( f92151...0a9cfa )
by Freek
10:51
created

src/Rules/CommaSeparatedEmails.php (2 issues)

Labels

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Spatie\ValidationRules\Rules;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Contracts\Validation\Rule;
7
use Illuminate\Support\Facades\Validator;
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 24
    public function passes($attribute, $value)
20
    {
21 24
        [$validEmails, $invalidEmails] = collect(explode(',', $value))
0 ignored issues
show
The variable $validEmails does not exist. Did you forget to declare it?

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.

Loading history...
The variable $invalidEmails does not exist. Did you forget to declare it?

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.

Loading history...
22
            ->map(function (string $rawEmail) {
23 24
                return trim($rawEmail);
24 24
            })
25
            ->partition(function (string $email) {
26 24
                return $this->isValidEmail($email);
27 24
            });
28
29 24
        if ($invalidEmails->count() === 1) {
30 6
            $this->message = __('validation.email', ['attribute' => $invalidEmails->first()]);
31
32 6
            return false;
33
        }
34
35 24
        if ($invalidEmails->count() > 1) {
36
            $this->message = __('validation.emails', ['attribute' => $invalidEmails->implode(',')]);
37
38
            return false;
39
        }
40
41 24
        if ($validEmails->unique()->count() !== $validEmails->count()) {
42 6
            $this->message = __('validation.unique_emails');
43 6
44
            return false;
45
        }
46
47
        if (! is_null($this->minimum)) {
48 18
            if ($validEmails->count() < $this->minimum) {
49 6
                $this->message = __('validation.minimum_emails', [
50 6
                    'actualCount' => $invalidEmails->implode(','),
51 6
                    'expectedMinimum' => $this->minimum,
52 6
                    'emailword' => Str::plural('e-mail address', $this->minimum),
53 6
                ]);
54
55
                return false;
56 6
            }
57
        }
58
59
        if (! is_null($this->maximum)) {
60 18
            if ($validEmails->count() > $this->maximum) {
61 6
                $this->message = __('validation.maximum_emails', [
62 6
                    'actualCount' => $invalidEmails->implode(','),
63 6
                    'expectedMaximum' => $this->maximum,
64 6
                    'emailword' => Str::plural('e-mail address', $this->maximum),
65 6
                ]);
66
67
                return false;
68 6
            }
69
        }
70
71
        return true;
72 18
    }
73
74
    public function message()
75
    {
76
        return $this->message;
77
    }
78
79
    public function min(int $minimum)
80 6
    {
81
        $this->minimum = $minimum;
82 6
83
        return $this;
84 6
    }
85
86
    public function max(int $maximum)
87 6
    {
88
        $this->maximum = $maximum;
89 6
90
        return $this;
91 6
    }
92
93
    protected function isValidEmail(string $email): bool
94 24
    {
95
        return Validator::make(['email' => $email], ['email' => 'email'])->passes();
96 24
    }
97
}
98