Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
EmailAddress
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
7 / 7
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 withEmail
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 withName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAddress
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toString
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3// ╔════════════════════════════════════════════════════════════╗
4// ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║
5// ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com>     ║
6// ╚════════════════════════════════════════════════════════════╝
7
8declare(strict_types=1);
9
10namespace Phexium\Plugin\Mailer;
11
12use Assert\Assert;
13use InvalidArgumentException;
14use Stringable;
15
16final readonly class EmailAddress implements Stringable
17{
18    private function __construct(
19        private string $address,
20        private ?string $name,
21    ) {
22        try {
23            Assert::that($address)
24                ->notEmpty('Email address cannot be empty')
25                ->email('Email address must be a valid email address')
26            ;
27        } catch (InvalidArgumentException $e) {
28            throw InvalidEmailAddressException::whenValidationFailed($e->getMessage());
29        }
30    }
31
32    public function __toString(): string
33    {
34        return $this->toString();
35    }
36
37    public static function withEmail(string $email): self
38    {
39        return new self($email, null);
40    }
41
42    public function withName(string $name): self
43    {
44        return new self($this->address, $name);
45    }
46
47    public function getAddress(): string
48    {
49        return $this->address;
50    }
51
52    public function getName(): ?string
53    {
54        return $this->name;
55    }
56
57    public function toString(): string
58    {
59        if ($this->name !== null) {
60            return sprintf('%s <%s>', $this->name, $this->address);
61        }
62
63        return $this->address;
64    }
65}