Email::validateEmail()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TraderInteractive\Filter;
4
5
use TraderInteractive\Exceptions\FilterException;
6
7
/**
8
 * A collection of filters for emails.
9
 */
10
final class Email
11
{
12
    /**
13
     * Filter an email
14
     *
15
     * The return value is the email, as expected by the \TraderInteractive\Filterer class.
16
     *
17
     * @param mixed $value The value to filter.
18
     *
19
     * @return string The passed in $value.
20
     *
21
     * @throws FilterException if the value did not pass validation.
22
     */
23
    public static function filter($value) : string
24
    {
25
        self::validateString($value);
26
        return self::validateEmail($value);
27
    }
28
29
    private static function validateEmail(string $value) : string
30
    {
31
        $filteredEmail = filter_var($value, FILTER_VALIDATE_EMAIL);
32
        if ($filteredEmail === false) {
33
            throw new FilterException("Value '{$value}' is not a valid email");
34
        }
35
        
36
        return $filteredEmail;
37
    }
38
39
    private static function validateString($value)
40
    {
41
        if (!is_string($value)) {
42
            throw new FilterException("Value '" . var_export($value, true) . "' is not a string");
43
        }
44
    }
45
}
46