Skip to content

Commit 39d8072

Browse files
committed
Added isValidQuery
Fixed uri/iri validations
1 parent d1adee5 commit 39d8072

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

src/Formats/Iri.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function validate($data): bool
4242
if (!$data) {
4343
return false;
4444
}
45-
foreach (['host', 'path', 'fragment'] as $component) {
45+
foreach (['host', 'path', 'query', 'fragment'] as $component) {
4646
if (isset($data[$component])) {
4747
$data[$component] = idn_to_ascii($data[$component], 0, INTL_IDNA_VARIANT_UTS46);
4848
}

src/Formats/IriReference.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function validate($data): bool
4242
if (!$data) {
4343
return false;
4444
}
45-
foreach (['host', 'path', 'fragment'] as $component) {
45+
foreach (['host', 'path', 'query', 'fragment'] as $component) {
4646
if (isset($data[$component])) {
4747
$data[$component] = idn_to_ascii($data[$component], 0, INTL_IDNA_VARIANT_UTS46);
4848
}

src/URI.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class URI
3131
'fragment' => null
3232
];
3333

34-
const FRAGMENT_REGEX = '/^(?:(%[0-9a-f]{2})|[a-z0-9\-\/?:@._~!\$&\'\(\)*+,;=])*$/i';
34+
const FRAGMENT_REGEX = '/^(?:(?:%[0-9a-f]{2})+|[a-z0-9\-\/?:@._~!\$&\'\(\)*+,;=])*$/i';
35+
36+
const QUERY_REGEX = self::FRAGMENT_REGEX;
3537

3638
const PATH_REGEX = '/^(?:(%[0-9a-f]{2})|[a-z0-9\/:@\-._~\!\$&\'\(\)*+,;=])*$/i';
3739

@@ -134,6 +136,9 @@ public static function isValid(string $uri, bool $require_scheme = true): bool
134136
if (isset($uri['path']) && !static::isValidPath($uri['path'])) {
135137
return false;
136138
}
139+
if (isset($uri['query']) && !static::isValidQuery($uri['query'])) {
140+
return false;
141+
}
137142
if (isset($uri['fragment']) && !static::isValidFragment($uri['fragment'])) {
138143
return false;
139144
}
@@ -173,6 +178,15 @@ public static function isValidFragment(string $fragment): bool
173178
return (bool) preg_match(static::FRAGMENT_REGEX, $fragment);
174179
}
175180

181+
/**
182+
* @param string $query
183+
* @return bool
184+
*/
185+
public static function isValidQuery(string $query): bool
186+
{
187+
return $query === '' || preg_match(static::QUERY_REGEX, $query);
188+
}
189+
176190
/**
177191
* @param string $uri
178192
* @return array

0 commit comments

Comments
 (0)