You are here

public function Uri::withPort in Zircon Profile 8

Same name in this branch
  1. 8 vendor/zendframework/zend-diactoros/src/Uri.php \Zend\Diactoros\Uri::withPort()
  2. 8 vendor/guzzlehttp/psr7/src/Uri.php \GuzzleHttp\Psr7\Uri::withPort()
Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-diactoros/src/Uri.php \Zend\Diactoros\Uri::withPort()

Return an instance with the specified port.

This method MUST retain the state of the current instance, and return an instance that contains the specified port.

Implementations MUST raise an exception for ports outside the established TCP and UDP port ranges.

A null value provided for the port is equivalent to removing the port information.

Parameters

null|int $port The port to use with the new instance; a null value: removes the port information.

Return value

self A new instance with the specified port.

Throws

\InvalidArgumentException for invalid ports.

Overrides UriInterface::withPort

File

vendor/zendframework/zend-diactoros/src/Uri.php, line 307

Class

Uri
Implementation of Psr\Http\UriInterface.

Namespace

Zend\Diactoros

Code

public function withPort($port) {
  if (!is_numeric($port)) {
    throw new InvalidArgumentException(sprintf('Invalid port "%s" specified; must be an integer or integer string', is_object($port) ? get_class($port) : gettype($port)));
  }
  $port = (int) $port;
  if ($port === $this->port) {

    // Do nothing if no change was made.
    return clone $this;
  }
  if ($port < 1 || $port > 65535) {
    throw new InvalidArgumentException(sprintf('Invalid port "%d" specified; must be a valid TCP/UDP port', $port));
  }
  $new = clone $this;
  $new->port = $port;
  return $new;
}