Completed
Pull Request — master (#578)
by Richard
07:01
created

EmailAddress::withDisplayName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits of supporting
4
 developers from this source code or any supporting source code which is considered
5
 copyrighted (c) material of the original  comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xoops\Core\Service\Data;
13
14
use Xmf\Assert;
15
16
/**
17
 * The EmailAddress data object is a email address with optional display name
18
 *
19
 * This is an Immutable data object. That means any changes to the data (state)
20
 * return a new object, while the internal state of the original object is preserved.
21
 *
22
 * All data is validated for type and value, and an exception is generated when
23
 * data on any operation for a property when it is not valid.
24
 *
25
 * The EmailAddress data object is used for message and mailer services
26
 *
27
 * @category  Xoops\Core\Service\Data
28
 * @package   Xoops\Core
29
 * @author    Richard Griffith <[email protected]>
30
 * @copyright 2018 XOOPS Project (https://xoops.org)
31
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
32
 * @link      https://xoops.org
33
 */
34
class EmailAddress
35
{
36
    /** @var string $email an email address, i.e. [email protected] */
37
    protected $email;
38
39
    /** @var string $displayName a display name associated with an email address, i.e. "John Doe" */
40
    protected $displayName;
41
42
    /* assert messages */
43
    protected const MESSAGE_ADDRESS = 'Email address is invalid';
44
    protected const MESSAGE_NAME    = 'Display name is invalid';
45
46
    /**
47
     * EmailAddress constructor.
48
     *
49
     * If an argument is null, the corresponding value will not be set. Values can be set
50
     * later with the with*() methods, but each will result in a new object.
51
     *
52
     * @param null|string $email       an email address, i.e. [email protected]
53
     * @param null|string $displayName an optional display name associated with the email
54
     *                                 address, i.e. "John Doe" or other non-empty string.
55
     *                                 Empty or all whitespace strings will be ignored.
56
     *
57
     * @throws \InvalidArgumentException
58
     */
59 23
    public function __construct(?string $email = null, ?string $displayName = null)
60
    {
61 23
        if (null!==$email) {
62 13
            $email = trim($email);
63 13
            Assert::true(
64 13
                false!==filter_var($email, FILTER_VALIDATE_EMAIL, FILTER_FLAG_EMAIL_UNICODE),
65 13
                static::MESSAGE_ADDRESS
66
            );
67 12
            $this->email = $email;
68
        }
69 23
        if (null!==$displayName) {
70 6
            $displayName = trim($displayName);
71 6
            $displayName = empty($displayName) ? null : $displayName;
72 6
            Assert::nullOrStringNotEmpty($displayName, static::MESSAGE_NAME);
73 6
            $this->displayName = $displayName;
74
        }
75 23
    }
76
77
    /**
78
     * getEmail
79
     *
80
     * @return string an email address
81
     *
82
     * @throws \LogicException (property was not properly set before used)
83
     */
84 19
    public function getEmail() : string
85
    {
86
        try {
87 19
            Assert::true(
88 19
                false!==filter_var($this->email, FILTER_VALIDATE_EMAIL, FILTER_FLAG_EMAIL_UNICODE),
89 19
                static::MESSAGE_ADDRESS
90
            );
91 10
        } catch (\InvalidArgumentException $e) {
92 10
            throw new \LogicException($e->getMessage(), $e->getCode(), $e);
93
        }
94 12
        return $this->email;
95
    }
96
97
    /**
98
     * getDisplayName
99
     *
100
     * @return string|null the displayName or null if not set
101
     *
102
     * @throws \LogicException (property was not properly set before used)
103
     */
104 5
    public function getDisplayName() : ?string
105
    {
106
        try {
107 5
            Assert::nullOrStringNotEmpty($this->displayName, static::MESSAGE_NAME);
108 1
        } catch (\InvalidArgumentException $e) {
109 1
            throw new \LogicException($e->getMessage(), $e->getCode(), $e);
110
        }
111 4
        return $this->displayName;
112
    }
113
114
    /**
115
     * Return a new object with a the specified email
116
     *
117
     * @param string $email an email address, i.e. [email protected]
118
     *
119
     * @return EmailAddress
120
     *
121
     * @throws \InvalidArgumentException
122
     */
123 1
    public function withEmail(string $email) : EmailAddress
124
    {
125 1
        return new static($email, $this->displayName);
126
    }
127
128
    /**
129
     * Return a new object with a the specified email
130
     *
131
     * @param string $displayName a display name associated with the email
132
     *                            address, i.e. "John Doe" or other non-empty string.
133
     *                            Empty or all whitespace strings will be ignored.
134
     *
135
     * @return EmailAddress
136
     *
137
     * @throws \InvalidArgumentException
138
     */
139 1
    public function withDisplayName(string $displayName) : EmailAddress
140
    {
141 1
        return new static($this->email, $displayName);
142
    }
143
}
144