Skip to content

Instantly share code, notes, and snippets.

@pelshoff
Created October 16, 2018 09:12
Show Gist options
  • Save pelshoff/b677bfcce7c2b9bb22feed83a677c11e to your computer and use it in GitHub Desktop.
Save pelshoff/b677bfcce7c2b9bb22feed83a677c11e to your computer and use it in GitHub Desktop.
<?php
class InvalidEmail extends DomainException
{
public static function becauseItDoesNotMatchEmailSpec(string $emailAddress): self
{
throw new self(sprintf('%s does not conform to a recognized e-mail address format'), $emailAddress);
}
public static function becauseTldIsNotAllowed(string $emailAddress): self
{
throw new self(sprintf('The tld of %s is not allowed'), $emailAddress);
}
public static function becauseItWasBanned(string $emailAddress, Date $onDate): self
{
throw new self(sprintf('The e-mail address %s was banned on %s and is not acceptable for the purposes of this amazing system we have here'), $emailAddress, $onDate->asString());
}
}
@carnage
Copy link

carnage commented Oct 16, 2018

Minor suggestion: return new self(...); and throw it at the site of the error, keeps stack traces a little shorter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment