src/Valkyrja/HttpMessage/RequestTrait.php
public function withUri(Uri $uri, bool $preserveHost = false)
    {
        $new = clone $this;

        $new->uri = $uri;

        if ($preserveHost && $this->hasHeader(static::$HOST_NAME)) {
            return $new;
        }

        if (! $uri->getHost()) {
            return $new;
        }

        $host = $uri->getHost();

        $new->headerNames[static::$HOST_NAME_NORM] = static::$HOST_NAME;

        $new->headers =
            $this->injectHeader(
                static::$HOST_NAME,
                $host,
                $new->headerNames,
                true
            );

        return $new;
    }
public function withUri(Uri $uri, bool $preserveHost = false)
    {
        $new = clone $this;

        $new->uri = $uri;

        if ($preserveHost && $this->hasHeader(static::$HOST_NAME)) {
            return $new;
        }

        if (! $uri->getHost()) {
            return $new;
        }

        $host = $uri->getHost();

        $new->headerNames[static::$HOST_NAME_NORM] = static::$HOST_NAME;

        $new->headers =
            $this->injectHeader(
                static::$HOST_NAME,
                $host,
                $new->headerNames,
                true
            );

        return $new;
    }
Returns an instance with the provided URI.

This method MUST update the Host header of the returned request by
default if the URI contains a host component. If the URI does not
contain a host component, any pre-existing Host header MUST be carried
over to the returned request.

You can opt-in to preserving the original state of the Host header by
setting `$preserveHost` to `true`. When `$preserveHost` is set to
`true`, this method interacts with the Host header in the following
ways:

- If the Host header is missing or empty, and the new URI contains
a host component, this method MUST update the Host header in the
returned request.
- If the Host header is missing or empty, and the new URI does not
contain a host component, this method MUST NOT update the Host header in
the returned request.
- If a Host header is present and non-empty, this method MUST NOT update
the Host header in the returned request.

This method MUST be implemented in such a way as to retain the
immutability of the message, and MUST return an instance that has the
new Uri instance.

@link http://tools.ietf.org/html/rfc3986#section-4.3

@param Uri  $uri          New request URI to use.
@param bool $preserveHost Preserve the original state of the Host
header.

@return static