mirror of
https://gitlab.com/izyim/prototypes/ddd-example.git
synced 2026-08-20 21:14:21 +00:00
36 lines
785 B
PHP
36 lines
785 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Src\Zone\User\Domain\Model\ValueObjects;
|
|
|
|
use Src\Common\Domain\Exceptions\IncorrectEmailFormatException;
|
|
use Src\Common\Domain\Exceptions\RequiredException;
|
|
|
|
final class Email implements \JsonSerializable
|
|
{
|
|
private string $email;
|
|
|
|
public function __construct(?string $email, $isOptional = false)
|
|
{
|
|
if (!$email && !$isOptional) {
|
|
throw new RequiredException('email');
|
|
}
|
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
throw new IncorrectEmailFormatException();
|
|
}
|
|
|
|
$this->email = $email;
|
|
}
|
|
|
|
public function __toString(): string
|
|
{
|
|
return $this->email;
|
|
}
|
|
|
|
public function jsonSerialize(): string
|
|
{
|
|
return $this->email;
|
|
}
|
|
} |