mirror of
https://github.com/Respect/Validation.git
synced 2024-06-01 13:22:24 +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 in_array;
|
||||||
use function is_scalar;
|
use function is_scalar;
|
||||||
use function mb_strtoupper;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract class for searches into arrays.
|
* Abstract class for searches into arrays.
|
||||||
|
@ -45,6 +44,6 @@ abstract class AbstractSearcher extends AbstractRule
|
||||||
return false;
|
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;
|
namespace Respect\Validation\Rules;
|
||||||
|
|
||||||
|
use Respect\Validation\Helpers\CanValidateUndefined;
|
||||||
use Respect\Validation\Helpers\DomainInfo;
|
use Respect\Validation\Helpers\DomainInfo;
|
||||||
|
|
||||||
use function array_pop;
|
use function array_pop;
|
||||||
use function explode;
|
use function explode;
|
||||||
|
use function in_array;
|
||||||
|
use function is_scalar;
|
||||||
|
use function strtoupper;
|
||||||
|
|
||||||
final class PublicDomainSuffix extends AbstractSearcher
|
final class PublicDomainSuffix extends AbstractRule
|
||||||
{
|
{
|
||||||
/**
|
use CanValidateUndefined;
|
||||||
* @var string[]
|
|
||||||
*/
|
|
||||||
private $domainInfo;
|
|
||||||
|
|
||||||
/**
|
public function validate($input): bool
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
protected function getDataSource($input = null): array
|
|
||||||
{
|
{
|
||||||
$parts = explode('.', $input);
|
if (!is_scalar($input)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$parts = explode('.', (string) $input);
|
||||||
$tld = array_pop($parts);
|
$tld = array_pop($parts);
|
||||||
|
|
||||||
$domainInfo = new DomainInfo($tld);
|
$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
|
public static function providerForInvalidInput(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
[new CountryCode(), 'ca'],
|
||||||
[new CountryCode(CountryCode::ALPHA2), 'USA'],
|
[new CountryCode(CountryCode::ALPHA2), 'USA'],
|
||||||
[new CountryCode(CountryCode::ALPHA3), 'US'],
|
[new CountryCode(CountryCode::ALPHA3), 'US'],
|
||||||
[new CountryCode(CountryCode::NUMERIC), '000'],
|
[new CountryCode(CountryCode::NUMERIC), '000'],
|
||||||
|
|
|
@ -10,6 +10,7 @@ declare(strict_types=1);
|
||||||
namespace Respect\Validation\Rules;
|
namespace Respect\Validation\Rules;
|
||||||
|
|
||||||
use Respect\Validation\Test\RuleTestCase;
|
use Respect\Validation\Test\RuleTestCase;
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \Respect\Validation\Exceptions\PublicDomainSuffixException
|
* @covers \Respect\Validation\Exceptions\PublicDomainSuffixException
|
||||||
|
@ -41,6 +42,9 @@ final class PublicDomainSuffixTest extends RuleTestCase
|
||||||
$rule = new PublicDomainSuffix();
|
$rule = new PublicDomainSuffix();
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
[$rule, []],
|
||||||
|
[$rule, null],
|
||||||
|
[$rule, new stdClass()],
|
||||||
[$rule, 'NONONONONONONONONON'],
|
[$rule, 'NONONONONONONONONON'],
|
||||||
[$rule, 'NONONONONONONONONON.uk'],
|
[$rule, 'NONONONONONONONONON.uk'],
|
||||||
[$rule, 'invalid.com'],
|
[$rule, 'invalid.com'],
|
||||||
|
|
Loading…
Reference in a new issue