Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
90.00% covered (success)
90.00%
9 / 10
44.44% covered (danger)
44.44%
4 / 9
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Email
100.00% covered (success)
100.00%
10 / 10
90.00% covered (success)
90.00%
9 / 10
44.44% covered (danger)
44.44%
4 / 9
100.00% covered (success)
100.00%
1 / 1
6.74
100.00% covered (success)
100.00%
1 / 1
 validate
100.00% covered (success)
100.00%
10 / 10
90.00% covered (success)
90.00%
9 / 10
44.44% covered (danger)
44.44%
4 / 9
100.00% covered (success)
100.00%
1 / 1
6.74
1<?php
2
3declare(strict_types=1);
4
5namespace Celema\Sire\Rule;
6
7use Celema\Sire\Contract;
8use Celema\Sire\Validation;
9use Override;
10
11/** @api */
12final class Email implements Contract\Rule
13{
14    public string $message {
15        get => '{label} must be a valid email address';
16    }
17
18    #[Override]
19    public function validate(Contract\Value $value, string ...$args): Contract\Validation
20    {
21        if (!is_string($value->value)) {
22            return Validation::invalid();
23        }
24
25        $email = filter_var(
26            trim($value->value),
27            \FILTER_VALIDATE_EMAIL,
28        );
29
30        if ($email !== false && ($args[0] ?? null) === 'checkdns') {
31            [, $mailDomain] = explode('@', $email);
32
33            return Validation::from(checkdnsrr($mailDomain, 'MX'));
34        }
35
36        return Validation::from($email !== false);
37    }
38}