Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorder setParameters checks to fix mobile and fixed_line rules being interpreted as countries #242

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Rules/Phone.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ public function setParameters($parameters)
$this->lenient();
} elseif (strcasecmp('international', $parameter) === 0) {
$this->international();
} elseif ($this->isDataKey($parameter)) {
$this->countryField = $parameter;
} elseif (PhoneNumberCountry::isValid($parameter)) {
$this->country($parameter);
} elseif (ctype_digit($parameter) && PhoneNumberType::isValid((int) $parameter)) {
$this->type((int) $parameter);
} elseif (PhoneNumberType::isValidName($parameter)) {
$this->type($parameter);
} elseif ($this->isDataKey($parameter)) {
$this->countryField = $parameter;
} elseif (PhoneNumberCountry::isValid($parameter)) {
$this->country($parameter);
}
}

Expand Down
16 changes: 16 additions & 0 deletions tests/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,22 @@ public function it_converts_string_validation_parameters()
$this->assertEquals(['mobile', 'fixed_line'], $this->getProtectedProperty($rule, 'blockedTypes'));
}

/** @test */
public function it_allows_data_field_names_to_be_parameters()
{
$base = (new Phone)->setValidator(validator([
'mobile' => null,
'fixed_line' => null,
'lenient' => null,
'international' => null,
]));

$rule = $base->setParameters(['mobile', 'fixed_line', 'lenient', 'international']);
$this->assertEquals(['mobile', 'fixed_line'], $this->getProtectedProperty($rule, 'allowedTypes'));
$this->assertTrue($this->getProtectedProperty($rule, 'lenient'));
$this->assertTrue($this->getProtectedProperty($rule, 'international'));
}

/** @test */
public function it_treats_string_validation_parameters_case_insensitive()
{
Expand Down