Created
December 2, 2022 11:41
-
-
Save BackEndTea/2af5e23917f951fc6a31cd81156264a4 to your computer and use it in GitHub Desktop.
VO with possible invalid state
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Email; | |
interface EmailAddress { | |
public function isValid(): bool; | |
public function asString(): string; | |
} | |
class ValidEmailAddress implements EmailAddress { | |
private function __construct( | |
private string $email | |
) {} | |
public static function create(string $input): EmailAddress { | |
if(filter_var($input, FILTER_VALIDATE_EMAIL)) { | |
return new self($input); | |
} | |
return new InValidEmailAddress($input); | |
} | |
public function isValid(): bool { | |
return true; | |
} | |
public function asString(): string { | |
return $this->email; | |
} | |
} | |
class InValidEmailAddress implements EmailAddress { | |
public function __construct( | |
private string $email | |
) {} | |
public function isValid(): bool { | |
return false; | |
} | |
public function asString(): string { | |
return $this->email; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment