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 Phexium\Plugin\Mailer\Port\Exception\InvalidEmailAddressException;
15use Stringable;
16
17final readonly class EmailAddress implements Stringable
18{
19    private function __construct(
20        private string $address,
21        private ?string $name,
22    ) {
23        try {
24            Assert::that($address)
25                ->notEmpty('Email address cannot be empty')
26                ->email('Email address must be a valid email address')
27            ;
28        } catch (InvalidArgumentException $e) {
29            throw InvalidEmailAddressException::whenValidationFailed($e->getMessage());
30        }
31    }
32
33    public function __toString(): string
34    {
35        return $this->toString();
36    }
37
38    public static function withEmail(string $email): self
39    {
40        return new self($email, null);
41    }
42
43    public function withName(string $name): self
44    {
45        return new self($this->address, $name);
46    }
47
48    public function getAddress(): string
49    {
50        return $this->address;
51    }
52
53    public function getName(): ?string
54    {
55        return $this->name;
56    }
57
58    public function toString(): string
59    {
60        if ($this->name !== null) {
61            return sprintf('%s <%s>', $this->name, $this->address);
62        }
63
64        return $this->address;
65    }
66}