add user entity
This commit is contained in:
parent
b5697e1e1f
commit
0777f0da2f
10 changed files with 172 additions and 158 deletions
40
backend/app/Shared/ValueObject/EmailAddress.php
Normal file
40
backend/app/Shared/ValueObject/EmailAddress.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace App\Shared\ValueObject;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
final readonly class EmailAddress
|
||||
{
|
||||
private const string ERROR_MESSAGE = 'Invalid email address:';
|
||||
|
||||
private string $normalized;
|
||||
|
||||
public function __construct(string $email)
|
||||
{
|
||||
$trimmedEmail = trim($email);
|
||||
if (
|
||||
$trimmedEmail === ''
|
||||
|| ! str_contains($trimmedEmail, '@')
|
||||
) {
|
||||
throw new InvalidArgumentException(
|
||||
self::ERROR_MESSAGE." $email",
|
||||
);
|
||||
}
|
||||
|
||||
[$localPart, $domain] = explode('@', $trimmedEmail, 2);
|
||||
$normalizedEmail = $localPart.'@'.mb_strtolower($domain);
|
||||
if (filter_var($normalizedEmail, FILTER_VALIDATE_EMAIL) === false) {
|
||||
throw new InvalidArgumentException(
|
||||
self::ERROR_MESSAGE." $email",
|
||||
);
|
||||
}
|
||||
|
||||
$this->normalized = $normalizedEmail;
|
||||
}
|
||||
|
||||
public function value(): string
|
||||
{
|
||||
return $this->normalized;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue