Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Validator/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class Domain extends Validator
*/
public function __construct(
protected array $restrictions = [],
protected bool $hostnames = true
protected bool $hostnames = true,
protected bool $allowEmpty = false
) {
}

Expand Down Expand Up @@ -64,6 +65,10 @@ public function getDescription(): string
*/
public function isValid($value): bool
{
if ($this->allowEmpty && $value === '') {
return true;
}

if (empty($value)) {
return false;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/Validator/DomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,30 @@ public function testInvalidDomainsWithHostnamesFalse()
$this->assertSame(true, $permissiveValidator->isValid('example.com/path')); // Path
}

/**
* Test allowEmpty parameter
*/
public function testAllowEmpty()
{
// By default, empty string is invalid
$this->assertSame(false, $this->domain->isValid(''));

// With allowEmpty=true, empty string is valid
$domainAllowEmpty = new Domain([], true, true);
$this->assertSame(true, $domainAllowEmpty->isValid(''));

// null is still invalid even with allowEmpty=true
$this->assertSame(false, $domainAllowEmpty->isValid(null));

// Valid domains still pass with allowEmpty=true
$this->assertSame(true, $domainAllowEmpty->isValid('example.com'));
$this->assertSame(true, $domainAllowEmpty->isValid('subdomain.example.com'));

// Invalid domains still fail with allowEmpty=true
$this->assertSame(false, $domainAllowEmpty->isValid('invalid..domain'));
$this->assertSame(false, $domainAllowEmpty->isValid(1));
}

public function testRestrictions()
{
Comment thread
Meldiron marked this conversation as resolved.
Comment thread
Meldiron marked this conversation as resolved.
$validator = new Domain([
Expand Down
Loading