Skip to content

Commit a91e0ce

Browse files
committed
allow protocols to be passed as a string
1 parent 120b8cc commit a91e0ce

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/Symfony/Component/Validator/Constraints/Url.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ class Url extends Constraint
4040
public $normalizer;
4141

4242
/**
43-
* @param string[]|null $protocols The protocols considered to be valid for the URL (e.g. http, https, ftp, etc.) (defaults to ['http', 'https']; use ['*'] to allow any protocol or regex patterns like ['.*'] for custom matching)
44-
* @param bool|null $relativeProtocol Whether to accept URL without the protocol (i.e. //example.com) (defaults to false)
45-
* @param string[]|null $groups
46-
* @param bool|null $requireTld Whether to require the URL to include a top-level domain (defaults to false)
43+
* @param string[]|string|null $protocols The protocols considered to be valid for the URL (e.g. http, https, ftp, etc.) (defaults to ['http', 'https']; use '*' to allow any protocol)
44+
* @param bool|null $relativeProtocol Whether to accept URL without the protocol (i.e. //example.com) (defaults to false)
45+
* @param string[]|null $groups
46+
* @param bool|null $requireTld Whether to require the URL to include a top-level domain (defaults to false)
4747
*/
4848
#[HasNamedArguments]
4949
public function __construct(
5050
?array $options = null,
5151
?string $message = null,
52-
?array $protocols = null,
52+
array|string|null $protocols = null,
5353
?bool $relativeProtocol = null,
5454
?callable $normalizer = null,
5555
?array $groups = null,
@@ -67,6 +67,10 @@ public function __construct(
6767
trigger_deprecation('symfony/validator', '7.1', 'Not passing a value for the "requireTld" option to the Url constraint is deprecated. Its default value will change to "true".');
6868
}
6969

70+
if (\is_string($protocols)) {
71+
$protocols = (array) $protocols;
72+
}
73+
7074
$this->message = $message ?? $this->message;
7175
$this->protocols = $protocols ?? $this->protocols;
7276
$this->relativeProtocol = $relativeProtocol ?? $this->relativeProtocol;

src/Symfony/Component/Validator/Tests/Constraints/UrlTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\Attributes\Group;
1515
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
16+
use PHPUnit\Framework\Attributes\TestWith;
1617
use PHPUnit\Framework\TestCase;
1718
use Symfony\Component\Validator\Constraints\Url;
1819
use Symfony\Component\Validator\Exception\InvalidArgumentException;
@@ -88,6 +89,15 @@ public function testRequireTldDefaultsToFalse()
8889

8990
$this->assertFalse($constraint->requireTld);
9091
}
92+
93+
#[TestWith(['*'])]
94+
#[TestWith(['http'])]
95+
public function testProtocolsAsString(string $protocol)
96+
{
97+
$constraint = new Url(protocols: $protocol, requireTld: true);
98+
99+
$this->assertSame([$protocol], $constraint->protocols);
100+
}
91101
}
92102

93103
class UrlDummy

0 commit comments

Comments
 (0)