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:
- a valid string according to their component validation rules as explain in RFC3986;
- a integer between
1
and65535
; - the
null
value;
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 ''
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
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