summaryrefslogtreecommitdiff
path: root/src/Validators/EmailValidator.php
blob: bbb237fbd7fcaf7070e112d65e17002563caa4e1 (plain)
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
<?php

declare(strict_types=1);

namespace Asko\Hird\Validators;

/**
 * Implements an e-mail validator that has a job 
 * of validating e-mail addresses.
 * 
 * @author Asko Nomm <asko@asko.dev>
 */
class EmailValidator implements Validator
{
    /**
     * Returns a boolean `true` when given `$value` is a valid e-mail
     * address. Returns `false` otherwise.
     *
     * @param mixed $value
     * @param mixed $modifier
     * @return boolean
     */
    public function validate(string $field, mixed $value, mixed $modifier = null): bool
    {
        return !!filter_var($value, FILTER_VALIDATE_EMAIL);
    }

    /**
     * Composes the error message in case the validation fails.
     *
     * @param string $field
     * @param mixed $modifier
     * @return string
     */
    public function composeError(string $field, mixed $modifier = null): string
    {
        return "{$field} is not a valid e-mail address.";
    }
}