-
Notifications
You must be signed in to change notification settings - Fork 1
/
EmailConstraint.php
50 lines (41 loc) · 1.31 KB
/
EmailConstraint.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
declare(strict_types=1);
namespace Chubbyphp\Validation\Constraint;
use Chubbyphp\Validation\Error\Error;
use Chubbyphp\Validation\Error\ErrorInterface;
use Chubbyphp\Validation\ValidatorContextInterface;
use Chubbyphp\Validation\ValidatorInterface;
final class EmailConstraint implements ConstraintInterface
{
/**
* @deprecated this pattern is not in use anymore in favor of filter_var($value, FILTER_VALIDATE_EMAIL)
*/
public const PATTERN = '/^(.+)\@(\S+\.\S+)$/';
/**
* @param mixed $value
*
* @return array<ErrorInterface>
*/
public function validate(
string $path,
$value,
ValidatorContextInterface $context,
?ValidatorInterface $validator = null
) {
if (null === $value || '' === $value) {
return [];
}
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
return [new Error(
$path,
'constraint.email.invalidtype',
['type' => get_debug_type($value)]
)];
}
$value = (string) $value;
if (false === filter_var($value, FILTER_VALIDATE_EMAIL)) {
return [new Error($path, 'constraint.email.invalidformat', ['value' => $value])];
}
return [];
}
}