Skip to content

Commit

Permalink
Merge pull request #38 from chadicus/email
Browse files Browse the repository at this point in the history
Add email filter
  • Loading branch information
Jonathan Gaillard committed Jun 5, 2014
2 parents e115ed6 + c071f22 commit f10cc0f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ The following checks that `$value` is a URL.
\DominionEnterprises\Filter\Url::filter($value);
```

#### Email::filter
Aliased in the filterer as `email`, this filter verifies that the argument is an email.

The following checks that `$value` is an email.
```php
\DominionEnterprises\Filter\Email::filter($value);
```

##Contact
Developers may be contacted at:

Expand Down
37 changes: 37 additions & 0 deletions src/Filter/Email.php
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;
}
}
1 change: 1 addition & 0 deletions src/Filterer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ final class Filterer
'ofArrays' => '\DominionEnterprises\Filter\Arrays::ofArrays',
'ofArray' => '\DominionEnterprises\Filter\Arrays::ofArray',
'url' => '\DominionEnterprises\Filter\Url::filter',
'email' => '\DominionEnterprises\Filter\Email::filter',
);

/**
Expand Down
41 changes: 41 additions & 0 deletions tests/Filter/EmailTest.php
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');
}
}

0 comments on commit f10cc0f

Please sign in to comment.