Completed
Push — master ( bb98fa...b1dd66 )
by Beñat
08:36
created

Email::email()   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 Shared Kernel library.
5
 *
6
 * Copyright (c) 2016-2017 LIN3S <[email protected]>
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 LIN3S\SharedKernel\Domain\Model\Email;
13
14
/**
15
 * @author Beñat Espiña <[email protected]>
16
 */
17
class Email
18
{
19
    private $email;
20
    private $domain;
21
    private $localPart;
22
23
    public function __construct($anEmail)
24
    {
25
        if (!filter_var($anEmail, FILTER_VALIDATE_EMAIL)) {
26
            throw new EmailInvalidException();
27
        }
28
        $this->email = $anEmail;
29
        $this->localPart = implode(explode('@', $this->email, -1), '@');
30
        $this->domain = str_replace($this->localPart . '@', '', $this->email);
31
    }
32
33
    public function email()
34
    {
35
        return $this->email;
36
    }
37
38
    public function localPart()
39
    {
40
        return $this->localPart;
41
    }
42
43
    public function domain()
44
    {
45
        return $this->domain;
46
    }
47
48
    public function equals(Email $anEmail)
49
    {
50
        return strtolower((string) $this) === strtolower((string) $anEmail);
51
    }
52
53
    public function __toString()
54
    {
55
        return (string) $this->email;
56
    }
57
}
58