Skip to content

Commit 5dfc57b

Browse files
committed
[Notifier] Remove Assert class and introduce validation traits
1 parent 655d358 commit 5dfc57b

File tree

6 files changed

+75
-51
lines changed

6 files changed

+75
-51
lines changed

src/Symfony/Component/Notifier/Recipient/Assert.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/Symfony/Component/Notifier/Recipient/EmailRecipient.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
*/
1919
class EmailRecipient implements EmailRecipientInterface
2020
{
21+
use EmailValidationTrait;
22+
2123
private $email;
2224

2325
public function __construct(string $email)
2426
{
25-
Assert::email($email);
27+
self::ensureEmailIsValid($email);
2628

2729
$this->email = $email;
2830
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Recipient;
13+
14+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
15+
16+
/**
17+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
18+
*/
19+
trait EmailValidationTrait
20+
{
21+
private static function ensureEmailIsValid(string $email): void
22+
{
23+
if ('' === $email) {
24+
throw new InvalidArgumentException('The email must be set.');
25+
}
26+
27+
if (!preg_match('/^[a-zA-Z0-9.!#$%&\'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/', $email)) {
28+
throw new InvalidArgumentException('The email is not valid.');
29+
}
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Recipient;
13+
14+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
15+
16+
/**
17+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
18+
*/
19+
trait PhoneValidationTrait
20+
{
21+
private static function ensurePhoneIsValid(string $phone): void
22+
{
23+
if ('' === $phone) {
24+
throw new InvalidArgumentException('The phone number must be set (no spaces, international code like in +3312345678).');
25+
}
26+
27+
if (!preg_match('/^\+[0-9]*/', $phone)) {
28+
throw new InvalidArgumentException('The phone number is not valid (no spaces, international code like in +3312345678).');
29+
}
30+
}
31+
}

src/Symfony/Component/Notifier/Recipient/Recipient.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
*/
2020
class Recipient implements EmailRecipientInterface, SmsRecipientInterface
2121
{
22+
use EmailValidationTrait;
23+
use PhoneValidationTrait;
24+
2225
private $email;
2326
private $phone;
2427

2528
public function __construct(string $email, string $phone)
2629
{
27-
Assert::email($email);
28-
Assert::phone($phone);
30+
self::ensureEmailIsValid($email);
31+
self::ensurePhoneIsValid($phone);
2932

3033
$this->email = $email;
3134
$this->phone = $phone;

src/Symfony/Component/Notifier/Recipient/SmsRecipient.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,22 @@
1111

1212
namespace Symfony\Component\Notifier\Recipient;
1313

14+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
15+
1416
/**
1517
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
1618
*
1719
* @experimental in 5.1
1820
*/
1921
class SmsRecipient implements SmsRecipientInterface
2022
{
23+
use PhoneValidationTrait;
24+
2125
private $phone;
2226

2327
public function __construct(string $phone)
2428
{
25-
Assert::phone($phone);
29+
self::ensurePhoneIsValid($phone);
2630

2731
$this->phone = $phone;
2832
}

0 commit comments

Comments
 (0)