= 0 && $port <= 65535))) { return $port; } throw new \InvalidArgumentException('Uri port must be null or an integer between 0 and 65535'); } /** * Filter Uri path. * * This method percent-encodes all reserved characters in the provided path string. This method * will NOT double-encode characters that are already percent-encoded. * * @param string $path The raw uri path. * @return string The RFC 3986 percent-encoded uri path. * @throws \InvalidArgumentException If the path is invalid. * @link http://www.faqs.org/rfcs/rfc3986.html */ public static function filterPath($path) { if (!\is_string($path)) { throw new \InvalidArgumentException('Uri path must be a string'); } return preg_replace_callback( '/(?:[^a-zA-Z0-9_\-\.~:@&=\+\$,\/;%]+|%(?![A-Fa-f0-9]{2}))/u', function ($match) { return rawurlencode($match[0]); }, $path ) ?? ''; } /** * Filters the query string or fragment of a URI. * * @param string $query The raw uri query string. * @return string The percent-encoded query string. * @throws \InvalidArgumentException If the query is invalid. */ public static function filterQueryOrFragment($query) { if (!\is_string($query)) { throw new \InvalidArgumentException('Uri query string and fragment must be a string'); } return preg_replace_callback( '/(?:[^a-zA-Z0-9_\-\.~!\$&\'\(\)\*\+,;=%:@\/\?]+|%(?![A-Fa-f0-9]{2}))/u', function ($match) { return rawurlencode($match[0]); }, $query ) ?? ''; } }