/** * Return an instance with the specified query string. * * This method MUST retain the state of the current instance, and return * an instance that contains the specified query string. * * Users can provide both encoded and decoded query characters. * Implementations ensure the correct encoding as outlined in getQuery(). * * An empty query string value is equivalent to removing the query string. * * @param string|array $query The query string to use with the new instance. * * @return static A new instance with the specified query string. * @throws \InvalidArgumentException for invalid query strings. */ public function withQuery($query) { if (!is_string($query)) { throw new \InvalidArgumentException('URI Query should be a string.'); } $query = UriHelper::filterQuery($query); $new = clone $this; $new->vars = UriHelper::parseQuery($query); $new->query = $query; return $new; }