Skip to content

Instantly share code, notes, and snippets.

@p810
Created June 21, 2015 18:30
Show Gist options
  • Save p810/f14715551a9e0495e911 to your computer and use it in GitHub Desktop.
Save p810/f14715551a9e0495e911 to your computer and use it in GitHub Desktop.
A simple script to test my understanding of value objects.
<?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