mirror of
https://github.com/Respect/Validation.git
synced 2024-05-17 21:56:40 +02:00
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:
parent
debf6c556e
commit
5b7ea866e4
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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'],
|
||||
|
|
|
@ -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'],
|
||||
|
|
Loading…
Reference in a new issue