Skip to content

Commit

Permalink
Merge pull request #118 from tomschlick/master
Browse files Browse the repository at this point in the history
removed strict hostname validator for Question::setName
  • Loading branch information
samuelwilliams authored Aug 6, 2024
2 parents d829350 + 3678b11 commit 75a504e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getName(): string
*/
public function setName($name): void
{
if (!Validator::fullyQualifiedDomainName($name)) {
if (!Validator::fullyQualifiedDomainName($name, false)) {
throw new InvalidArgumentException(sprintf('"%s" is not a fully qualified domain name.', $name));
}

Expand Down
17 changes: 15 additions & 2 deletions lib/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,32 @@ public static function hostName(string $name): bool
]);
}

/**
* Validate the string as a valid domain name, without the strict character checking of hostName().
*/
public static function domainName(string $name): bool
{
return (bool) filter_var($name, FILTER_VALIDATE_DOMAIN);
}

/**
* Validate the string is a Fully Qualified Domain Name.
*/
public static function fullyQualifiedDomainName(string $name): bool
public static function fullyQualifiedDomainName(string $name, bool $strictHostValidation = true): bool
{
if ('.' === $name) {
return true;
}

if ('.' !== substr($name, -1, 1)) {
return false;
}

return self::hostName($name);
if ($strictHostValidation) {
return self::hostName($name);
}

return self::domainName($name);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ public function getTestFqdnDataProvider(): array
['alt2.aspmx.l.google.com.', true],
['www.eXAMple.cOm.', true],
['3xample.com.', true],
['_sip._tcp.example.com.', true, false],
['_sip._tcp.example.com.', false, true],
['_example.com.', false],
['-example.com.', false],
['example.com', false],
Expand All @@ -310,9 +312,9 @@ public function getTestFqdnDataProvider(): array
/**
* @dataProvider getTestFqdnDataProvider
*/
public function testFqdn(string $domain, bool $isValid): void
public function testFqdn(string $domain, bool $isValid, bool $strictHostValidation = true): void
{
$this->assertEquals($isValid, Validator::fullyQualifiedDomainName($domain));
$this->assertEquals($isValid, Validator::fullyQualifiedDomainName($domain, $strictHostValidation));
}

public function testHostName(): void
Expand Down

0 comments on commit 75a504e

Please sign in to comment.