Skip to content

Commit ae6f875

Browse files
committed
Add new "requiredRegion" to PhoneNumberValidator
This option allows to validate the region of the phone number
1 parent 0b36405 commit ae6f875

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/Validator/Constraints/PhoneNumber.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class PhoneNumber extends Constraint
5252
public string|array $type = self::ANY;
5353
public ?string $defaultRegion = null;
5454
public ?string $regionPath = null;
55+
public ?string $requiredRegion = null;
5556
public ?PhoneNumberFormat $format = null;
5657

5758
/**
@@ -69,6 +70,7 @@ public function __construct(
6970
?array $groups = null,
7071
$payload = null,
7172
?array $options = null,
73+
?string $requiredRegion = null,
7274
) {
7375
parent::__construct($options, $groups, $payload);
7476

@@ -82,6 +84,7 @@ public function __construct(
8284
$this->type = $type ?? $this->type;
8385
$this->defaultRegion = $defaultRegion ?? $this->defaultRegion;
8486
$this->regionPath = $regionPath ?? $this->regionPath;
87+
$this->requiredRegion = $requiredRegion ?? $this->requiredRegion;
8588
}
8689

8790
/**

src/Validator/Constraints/PhoneNumberValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ public function validate(mixed $value, Constraint $constraint): void
8888
return;
8989
}
9090

91+
if (null !== $constraint->requiredRegion && false === $this->phoneUtil->isValidNumberForRegion($phoneNumber, $constraint->requiredRegion)) {
92+
$this->addViolation($value, $constraint);
93+
}
94+
9195
$validTypes = [];
9296
foreach ($constraint->getTypes() as $type) {
9397
switch ($type) {

tests/Validator/Constraints/PhoneNumberValidatorTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ public function testValidate(
5757
?string $defaultRegion = null,
5858
?string $regionPath = null,
5959
PhoneNumberFormat|int|null $format = null,
60+
?string $requiredRegion = null,
6061
): void {
61-
$constraint = new PhoneNumber($format, $type, $defaultRegion, $regionPath);
62+
$constraint = new PhoneNumber($format, $type, $defaultRegion, $regionPath, requiredRegion: $requiredRegion);
6263

6364
if (true === $violates) {
6465
$constraintViolationBuilder = $this->createMock(ConstraintViolationBuilderInterface::class);
@@ -112,8 +113,18 @@ public function testValidateFromAttribute(): void
112113
* 2 => Type (optional)
113114
* 3 => Default region (optional).
114115
* 4 => Region Path (optional).
116+
* 5 => Format (optional)
117+
* 6 => Required region (optional).
115118
*
116-
* @return iterable<array{string|LibPhoneNumber|null, bool, 2?: string|string[]|null, 3?: ?string, 4?: ?string, 5?: PhoneNumberFormat|int|null}>
119+
* @return iterable<array{
120+
* string|LibPhoneNumber|null,
121+
* bool,
122+
* 2?: string|string[]|null,
123+
* 3?: ?string,
124+
* 4?: ?string,
125+
* 5?: PhoneNumberFormat|int|null,
126+
* 6?: string|null
127+
* }>
117128
*/
118129
public function validateProvider(): iterable
119130
{
@@ -160,6 +171,8 @@ public function validateProvider(): iterable
160171
yield ['+33606060606', false, 'mobile', null, 'regionPath'];
161172
yield ['+33606060606', false, 'mobile', null, null, PhoneNumberFormat::E164];
162173
yield ['2015555555', true, null, null, null, PhoneNumberFormat::E164];
174+
yield ['+33650505050', false, null, null, null, PhoneNumberFormat::E164, 'FR'];
175+
yield ['+33650505050', true, null, null, null, PhoneNumberFormat::E164, 'EN'];
163176

164177
// Ensure BC promise is respected
165178
yield ['+33606060606', false, 'mobile', null, null, 0];

0 commit comments

Comments
 (0)