-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from chadicus/email
Add email filter
- Loading branch information
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
/** | ||
* Defines the DominionEnterprises\Filter\Email class. | ||
*/ | ||
|
||
namespace DominionEnterprises\Filter; | ||
|
||
/** | ||
* A collection of filters for emails. | ||
*/ | ||
final class Email | ||
{ | ||
/** | ||
* Filter an email | ||
* | ||
* The return value is the email, as expected by the \DominionEnterprises\Filterer class. | ||
* | ||
* @param mixed $value The value to filter. | ||
* | ||
* @return string The passed in $value. | ||
* | ||
* @throws \Exception if the value did not pass validation. | ||
*/ | ||
public static function filter($value) | ||
{ | ||
if (!is_string($value)) { | ||
throw new \Exception("Value '" . var_export($value, true) . "' is not a string"); | ||
} | ||
|
||
$filteredEmail = filter_var($value, FILTER_VALIDATE_EMAIL); | ||
if ($filteredEmail === false) { | ||
throw new \Exception("Value '{$value}' is not a valid email"); | ||
} | ||
|
||
return $filteredEmail; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
namespace DominionEnterprises\Filter; | ||
use DominionEnterprises\Filter\Email as E; | ||
|
||
/** | ||
* @coversDefaultClass \DominionEnterprises\Filter\Email | ||
*/ | ||
final class EmailTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @test | ||
* @covers ::filter | ||
*/ | ||
public function filter() | ||
{ | ||
$email = '[email protected]'; | ||
$this->assertSame($email, E::filter($email)); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException Exception | ||
* @expectedExceptionMessage Value '1' is not a string | ||
* @covers ::filter | ||
*/ | ||
public function filter_nonstring() | ||
{ | ||
E::filter(1); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException Exception | ||
* @expectedExceptionMessage Value '@email.com' is not a valid email | ||
* @covers ::filter | ||
*/ | ||
public function filter_notValid() | ||
{ | ||
E::filter('@email.com'); | ||
} | ||
} |