Skip to content

Commit ccc3ad8

Browse files
committed
created email validator
1 parent f404885 commit ccc3ad8

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Validator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ public static function validateDoctrineFieldName(string $name, ManagerRegistry $
167167
return $name;
168168
}
169169

170+
public static function validateEmailAddress(string $email): string
171+
{
172+
if (!\filter_var($email, FILTER_VALIDATE_EMAIL)) {
173+
throw new RuntimeCommandException(\sprintf('"%s" is not a valid email address.', $email));
174+
}
175+
176+
return $email;
177+
}
178+
170179
public static function existsOrNull(string $className = null, array $entities = [])
171180
{
172181
if (null !== $className) {

tests/ValidatorTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ public function testValidateClassName()
6565
$this->assertSame('Foo', Validator::validateClassName('Foo'));
6666
}
6767

68+
public function testValidateEmailAddress()
69+
{
70+
$this->assertSame('[email protected]', Validator::validateEmailAddress('[email protected]'));
71+
72+
$this->expectException(RuntimeCommandException::class);
73+
$this->expectExceptionMessage('"badEmail" is not a valid email address.');
74+
Validator::validateEmailAddress('badEmail');
75+
}
76+
6877
public function testInvalidClassName()
6978
{
7079
$this->expectException(RuntimeCommandException::class);

0 commit comments

Comments
 (0)