Completed
Push — master ( 3b3980...4ce207 )
by Gorka
9s
created

EmailAddress::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\SharedKernel\Domain\Model\Identity;
16
17
class EmailAddress
18
{
19
    protected $email;
20
    protected $domain;
21
    protected $localPart;
22
23
    public function __construct($email)
24
    {
25
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
26
            throw new EmailAddressInvalidException();
27
        }
28
        $this->email = $email;
29
        $this->localPart = implode(explode('@', $this->email, -1), '@');
30
        $this->domain = str_replace($this->localPart . '@', '', $this->email);
31
    }
32
33
    public function email() : string
34
    {
35
        return $this->email;
36
    }
37
38
    public function localPart() : string
39
    {
40
        return $this->localPart;
41
    }
42
43
    public function domain() : string
44
    {
45
        return $this->domain;
46
    }
47
48
    public function equals(EmailAddress $email) : bool
49
    {
50
        return strtolower((string) $this) === strtolower((string) $email);
51
    }
52
53
    public function __toString() : string
54
    {
55
        return (string) $this->email;
56
    }
57
}
58