Email   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateString() 0 4 2
A validateEmail() 0 8 2
A filter() 0 4 1
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