Passed
Push — master ( 819f30...8ceff2 )
by Thorsten
02:22
created

Email::makeEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Daikon\Entity\ValueObject;
4
5
use Daikon\Entity\Assert\Assertion;
6
7
final class Email implements ValueObjectInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    private const NIL = "";
13
14
    /**
15
     * @var Text
16
     */
17
    private $localPart;
18
19
    /**
20
     * @var Text
21
     */
22
    private $domain;
23
24
    /**
25
     * @param string|null $nativeValue
26
     * @return self
27
     */
28 18
    public static function fromNative($nativeValue): self
29
    {
30 18
        Assertion::nullOrString($nativeValue);
31 18
        if (empty($nativeValue)) {
32 1
            return new self(Text::fromNative(self::NIL), Text::fromNative(self::NIL));
33
        }
34 18
        Assertion::email($nativeValue, "Trying to create email from invalid string.");
35 18
        $parts = explode("@", $nativeValue);
36 18
        return new self(Text::fromNative($parts[0]), Text::fromNative(trim($parts[1], "[]")));
37
    }
38
39 4
    public function toNative(): string
40
    {
41 4
        if ($this->localPart->isEmpty() && $this->domain->isEmpty()) {
42 1
            return self::NIL;
43
        }
44 4
        return $this->localPart->toNative()."@".$this->domain->toNative();
45
    }
46
47 1
    public function equals(ValueObjectInterface $otherValue): bool
48
    {
49 1
        return $otherValue instanceof self && $this->toNative() === $otherValue->toNative();
50
    }
51
52 1
    public function __toString(): string
53
    {
54 1
        return $this->toNative();
55
    }
56
57 1
    public function getLocalPart(): Text
58
    {
59 1
        return $this->localPart;
60
    }
61
62 1
    public function getDomain(): Text
63
    {
64 1
        return $this->domain;
65
    }
66
67 18
    private function __construct(Text $localPart, Text $domain)
68
    {
69 18
        $this->localPart = $localPart;
70 18
        $this->domain = $domain;
71 18
    }
72
}
73