<?php $email = Email::create('TEST@fluentbe.com'); var_dump($email); readonly class Email { final protected function __construct( private string $value, ) { } public static function create(string $value): static { $data = static::recreate($value); $data->guard(); return $data; } public static function recreate(string $value): static { return new static($value); } public function getValue(): string { return $this->value; } public function equals(self $other): bool { return $this->getValue() === $other->getValue(); } protected function guard(): void { if (preg_match("/^(?!\.)(?!.*\.\.)([a-zA-Z0-9_'+\-.]*)[a-zA-Z0-9_'+\-]@([a-zA-Z0-9][a-zA-Z0-9\-]*\.)+[a-zA-Z]{2,}$/", $this->getValue()) !== 1) { throw new \InvalidArgumentException($this->getValue()); } } }
You have javascript disabled. You will not be able to edit any code.