Skip to content

Commit f10cc0f

Browse files
author
Jonathan Gaillard
committed
Merge pull request #38 from chadicus/email
Add email filter
2 parents e115ed6 + c071f22 commit f10cc0f

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,14 @@ The following checks that `$value` is a URL.
198198
\DominionEnterprises\Filter\Url::filter($value);
199199
```
200200

201+
#### Email::filter
202+
Aliased in the filterer as `email`, this filter verifies that the argument is an email.
203+
204+
The following checks that `$value` is an email.
205+
```php
206+
\DominionEnterprises\Filter\Email::filter($value);
207+
```
208+
201209
##Contact
202210
Developers may be contacted at:
203211

src/Filter/Email.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Defines the DominionEnterprises\Filter\Email class.
4+
*/
5+
6+
namespace DominionEnterprises\Filter;
7+
8+
/**
9+
* A collection of filters for emails.
10+
*/
11+
final class Email
12+
{
13+
/**
14+
* Filter an email
15+
*
16+
* The return value is the email, as expected by the \DominionEnterprises\Filterer class.
17+
*
18+
* @param mixed $value The value to filter.
19+
*
20+
* @return string The passed in $value.
21+
*
22+
* @throws \Exception if the value did not pass validation.
23+
*/
24+
public static function filter($value)
25+
{
26+
if (!is_string($value)) {
27+
throw new \Exception("Value '" . var_export($value, true) . "' is not a string");
28+
}
29+
30+
$filteredEmail = filter_var($value, FILTER_VALIDATE_EMAIL);
31+
if ($filteredEmail === false) {
32+
throw new \Exception("Value '{$value}' is not a valid email");
33+
}
34+
35+
return $filteredEmail;
36+
}
37+
}

src/Filterer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ final class Filterer
2222
'ofArrays' => '\DominionEnterprises\Filter\Arrays::ofArrays',
2323
'ofArray' => '\DominionEnterprises\Filter\Arrays::ofArray',
2424
'url' => '\DominionEnterprises\Filter\Url::filter',
25+
'email' => '\DominionEnterprises\Filter\Email::filter',
2526
);
2627

2728
/**

tests/Filter/EmailTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
namespace DominionEnterprises\Filter;
3+
use DominionEnterprises\Filter\Email as E;
4+
5+
/**
6+
* @coversDefaultClass \DominionEnterprises\Filter\Email
7+
*/
8+
final class EmailTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @test
12+
* @covers ::filter
13+
*/
14+
public function filter()
15+
{
16+
$email = 'first.last@email.com';
17+
$this->assertSame($email, E::filter($email));
18+
}
19+
20+
/**
21+
* @test
22+
* @expectedException Exception
23+
* @expectedExceptionMessage Value '1' is not a string
24+
* @covers ::filter
25+
*/
26+
public function filter_nonstring()
27+
{
28+
E::filter(1);
29+
}
30+
31+
/**
32+
* @test
33+
* @expectedException Exception
34+
* @expectedExceptionMessage Value '@email.com' is not a valid email
35+
* @covers ::filter
36+
*/
37+
public function filter_notValid()
38+
{
39+
E::filter('@email.com');
40+
}
41+
}

0 commit comments

Comments
 (0)