Do not allow lowercase country codes

Country codes in ISO 3166-1 should be in uppercase, and the
`AbstractSearcher` should not change the input to search for a value.

While working on this fix, I also discovered that the
"PublicDomainSuffix" rule would throw an exception if it got a
non-scalar value as an input.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2024-02-02 16:48:42 +01:00
parent debf6c556e
commit 5b7ea866e4
No known key found for this signature in database
GPG key ID: 221E9281655813A6
4 changed files with 23 additions and 14 deletions

View file

@ -13,7 +13,6 @@ use Respect\Validation\Helpers\CanValidateUndefined;
use function in_array;
use function is_scalar;
use function mb_strtoupper;
/**
* Abstract class for searches into arrays.
@ -45,6 +44,6 @@ abstract class AbstractSearcher extends AbstractRule
return false;
}
return in_array(mb_strtoupper((string) $input), $dataSource, true);
return in_array((string) $input, $dataSource, true);
}
}

View file

@ -9,29 +9,34 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Helpers\CanValidateUndefined;
use Respect\Validation\Helpers\DomainInfo;
use function array_pop;
use function explode;
use function in_array;
use function is_scalar;
use function strtoupper;
final class PublicDomainSuffix extends AbstractSearcher
final class PublicDomainSuffix extends AbstractRule
{
/**
* @var string[]
*/
private $domainInfo;
use CanValidateUndefined;
/**
* {@inheritDoc}
*/
protected function getDataSource($input = null): array
public function validate($input): bool
{
$parts = explode('.', $input);
if (!is_scalar($input)) {
return false;
}
$parts = explode('.', (string) $input);
$tld = array_pop($parts);
$domainInfo = new DomainInfo($tld);
$this->domainInfo = $domainInfo->getPublicSuffixes();
$dataSource = $domainInfo->getPublicSuffixes();
if ($this->isUndefined($input) && empty($dataSource)) {
return true;
}
return $this->domainInfo;
return in_array(strtoupper((string) $input), $dataSource, true);
}
}

View file

@ -61,6 +61,7 @@ final class CountryCodeTest extends RuleTestCase
public static function providerForInvalidInput(): array
{
return [
[new CountryCode(), 'ca'],
[new CountryCode(CountryCode::ALPHA2), 'USA'],
[new CountryCode(CountryCode::ALPHA3), 'US'],
[new CountryCode(CountryCode::NUMERIC), '000'],

View file

@ -10,6 +10,7 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Test\RuleTestCase;
use stdClass;
/**
* @covers \Respect\Validation\Exceptions\PublicDomainSuffixException
@ -41,6 +42,9 @@ final class PublicDomainSuffixTest extends RuleTestCase
$rule = new PublicDomainSuffix();
return [
[$rule, []],
[$rule, null],
[$rule, new stdClass()],
[$rule, 'NONONONONONONONONON'],
[$rule, 'NONONONONONONONONON.uk'],
[$rule, 'invalid.com'],