diff --git a/library/Rules/AbstractSearcher.php b/library/Rules/AbstractSearcher.php index f73d9630..b749bb38 100644 --- a/library/Rules/AbstractSearcher.php +++ b/library/Rules/AbstractSearcher.php @@ -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); } } diff --git a/library/Rules/PublicDomainSuffix.php b/library/Rules/PublicDomainSuffix.php index 67f07ac6..d25980b4 100644 --- a/library/Rules/PublicDomainSuffix.php +++ b/library/Rules/PublicDomainSuffix.php @@ -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); } } diff --git a/tests/unit/Rules/CountryCodeTest.php b/tests/unit/Rules/CountryCodeTest.php index 408ef324..17c7a497 100644 --- a/tests/unit/Rules/CountryCodeTest.php +++ b/tests/unit/Rules/CountryCodeTest.php @@ -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'], diff --git a/tests/unit/Rules/PublicDomainSuffixTest.php b/tests/unit/Rules/PublicDomainSuffixTest.php index 487e5967..53faa53b 100644 --- a/tests/unit/Rules/PublicDomainSuffixTest.php +++ b/tests/unit/Rules/PublicDomainSuffixTest.php @@ -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'],