The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Uri

Uri Schemes

Uri manipulation

Uri Parts API

Services

Upgrading Guide

The Port component

The library provides a Port class to ease port manipulation.

Instantiation

Using the default constructor.

<?php

public function __contruct(int $port = null)

The constructor accepts:

Examples

<?php

use League\Uri\Components\Port;

$port = new Port(443);
$port->getContent();           //return (int) 443
echo $port;                    //display '443'
echo $port->getUriComponent(); //display ':443'

$string_port = new Port('443');
$string_port->getContent();           //return (int) 443
echo $string_port;                    //display '443'
echo $string_port->getUriComponent(); //display ':443'

$empty_port = new Port();
$empty_port->getContent();           //return null
echo $empty_port;                    //display ''
echo $empty_port->getUriComponent(); //display ''

If the submitted value is not a valid port number an InvalidArgumentException exception is thrown.

On instantiation the submitted string is normalized using RFC3986 rules.

Using a League Uri object

You can acces a Port object with an already instantiated League Uri object.

<?php

use League\Uri\Schemes\Http as HttpUri;

$uri  = HttpUri::createFromString('http://uri.thephpleague.com:82');
$port = $uri->port; // $port is a League\Uri\Components\Port object;

Properties and Methods

The component representation, comparison and manipulation is done using the package UriPart and the Component interfaces methods.

Port::toInt

Since version 4.2 this method is deprecated please use Port::getContent instead.

Return an integer if the port was defined or null otherwise.

<?php

use League\Uri\Components\Port;

$port = new Port(81);
$port->toInt(); //return (int) 81

$empty_port = new Port();
$empty_port->toInt(); //return null