Created
June 21, 2015 18:30
-
-
Save p810/f14715551a9e0495e911 to your computer and use it in GitHub Desktop.
A simple script to test my understanding of value objects.
This file contains 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 | |
abstract class EmailValidator | |
{ | |
function __construct($value) | |
{ | |
if(!$this->validate($value)) { | |
throw new InvalidArgumentException; | |
} | |
$this->email = $value; | |
} | |
private function validate($value) | |
{ | |
if(!filter_var($value, FILTER_VALIDATE_EMAIL)) { | |
return false; | |
} | |
return true; | |
} | |
} | |
class Email | |
extends EmailValidator | |
{ | |
public function get() | |
{ | |
return $this->email; | |
} | |
} | |
// Tests | |
var_dump( | |
new Email('hellopayton@gmail.com'), // object | |
new Email('test'), // throws InvalidArgumentException | |
new Email(null) // throws InvalidArgumentException | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment