EmailAddress::user()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Domain\Web;
13
14
use Cubiche\Domain\System\StringLiteral;
15
16
/**
17
 * EmailAddress class.
18
 *
19
 * @author Ivannis Suárez Jerez <[email protected]>
20
 */
21
class EmailAddress extends StringLiteral
22
{
23
    /**
24
     * @param string $value
25
     *
26
     * @return bool
27
     */
28
    public static function isValid($value)
29
    {
30
        return \filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
31
    }
32
33
    /**
34
     * @param string $value
35
     *
36
     * @throws \InvalidArgumentException
37
     */
38
    public function __construct($value)
39
    {
40
        $filteredValue = \filter_var($value, FILTER_VALIDATE_EMAIL);
41
        if ($filteredValue === false) {
42
            throw new \InvalidArgumentException(sprintf(
43
                'Argument "%s" is invalid. Allowed types for argument are "email".',
44
                $value
45
            ));
46
        }
47
48
        parent::__construct($filteredValue);
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function user()
55
    {
56
        $parts = explode('@', $this->toNative());
57
58
        return new StringLiteral($parts[0]);
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    public function domain()
65
    {
66
        $parts = \explode('@', $this->toNative());
67
        $domain = \trim($parts[1], '[]');
68
69
        return new StringLiteral($domain);
70
    }
71
}
72