• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nepada / email-address / 6338291046

28 Sep 2023 11:30AM UTC coverage: 97.222%. Remained the same
6338291046

push

github

xificurk
nette/tester version bump

35 of 36 relevant lines covered (97.22%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
/src/EmailAddress/AbstractEmailAddress.php
1
<?php
2
declare(strict_types = 1);
3

4
namespace Nepada\EmailAddress;
5

6
use Nette\SmartObject;
7
use Nette\Utils\Validators;
8

9
abstract class AbstractEmailAddress implements EmailAddress
10
{
11

12
    use SmartObject;
13

14
    private readonly string $rawValue;
15

16
    private readonly string $localPart;
17

18
    private readonly string $domain;
19

20
    final private function __construct(string $rawValue, string $domain, string $localPart)
1✔
21
    {
22
        $this->rawValue = $rawValue;
1✔
23
        $this->domain = $domain;
1✔
24
        $this->localPart = $localPart;
1✔
25
    }
1✔
26

27
    /**
28
     * @throws InvalidEmailAddressException
29
     */
30
    final public static function fromString(string $emailAddress): static
1✔
31
    {
32
        if (! Validators::isEmail($emailAddress)) {
1✔
33
            throw new InvalidEmailAddressException($emailAddress);
1✔
34
        }
35

36
        $parts = explode('@', $emailAddress);
1✔
37
        $domain = array_pop($parts);
1✔
38
        $localPart = implode('@', $parts);
1✔
39
        [$normalizedDomain, $normalizedLocalPart] = static::normalizeDomainAndLocalPart($domain, $localPart);
1✔
40

41
        return new static($emailAddress, $normalizedDomain, $normalizedLocalPart);
1✔
42
    }
43

44
    /**
45
     * @throws InvalidEmailAddressException
46
     */
47
    final public static function fromDomainAndLocalPart(string $domain, string $localPart): static
1✔
48
    {
49
        return static::fromString($localPart . '@' . $domain);
1✔
50
    }
51

52
    /**
53
     * @return string[]
54
     */
55
    protected static function normalizeDomainAndLocalPart(string $domain, string $localPart): array
1✔
56
    {
57
        $normalizedDomain = idn_to_ascii($domain, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
1✔
58
        if ($normalizedDomain === false) {
1✔
59
            throw new FailedToNormalizeDomainException($domain);
×
60
        }
61

62
        return [$normalizedDomain, $localPart];
1✔
63
    }
64

65
    final public function getLocalPart(): string
66
    {
67
        return $this->localPart;
1✔
68
    }
69

70
    final public function getDomain(): string
71
    {
72
        return $this->domain;
1✔
73
    }
74

75
    final public function getValue(): string
76
    {
77
        return $this->localPart . '@' . $this->domain;
1✔
78
    }
79

80
    final public function toString(): string
81
    {
82
        return $this->rawValue;
1✔
83
    }
84

85
    final public function __toString(): string
86
    {
87
        return $this->toString();
1✔
88
    }
89

90
    final public function equals(EmailAddress $other): bool
1✔
91
    {
92
        return $this->getValue() === $other->getValue();
1✔
93
    }
94

95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc