1 | <?php |
||
11 | class Port implements UriPartInterface |
||
12 | { |
||
13 | use SchemePortsTrait; |
||
14 | |||
15 | protected $port = null; |
||
16 | |||
17 | protected static $part_pattern = '[0-9]{0,4}'; |
||
18 | protected static $valid_pattern = '/^[0-9]{0,4}$/'; |
||
19 | |||
20 | /** |
||
21 | * Port constructor. Accepts an integer or null value representing a URI port component. Construction will throw an |
||
22 | * exception if port is not an integer or is not null or if the given integer is outside of the allowed TCP/UDP |
||
23 | * range of 1 to 65535. |
||
24 | * |
||
25 | * port = *DIGIT |
||
26 | * |
||
27 | * @see https://tools.ietf.org/html/rfc3986#appendix-A |
||
28 | * |
||
29 | * @throws \InvalidArgumentException if the given port is not an integer or null or if the given integer is out of |
||
30 | * range |
||
31 | * |
||
32 | * @param int|null $port A string representing a URI port |
||
33 | */ |
||
34 | 117 | public function __construct($port) |
|
44 | |||
45 | /** |
||
46 | * Returns a string representation of the port. |
||
47 | * |
||
48 | * @return string The string representation of the port |
||
49 | */ |
||
50 | 5 | public function __toString() |
|
54 | |||
55 | /** |
||
56 | * Returns the raw port data |
||
57 | * |
||
58 | * @return int|null |
||
59 | */ |
||
60 | 2 | public function getPort() |
|
64 | |||
65 | /** |
||
66 | * Returns a string representing a valid pattern for a URI port |
||
67 | * |
||
68 | * @return string |
||
69 | */ |
||
70 | 17 | public static function getValidPattern() |
|
74 | |||
75 | /** |
||
76 | * Validates a given port. A port is valid if it is either null or an integer between 1 and 65535 inclusive. |
||
77 | * |
||
78 | * @param mixed $port The port to be validated |
||
79 | * @return bool Returns true if the given port is valid |
||
80 | * Returns false otherwise |
||
81 | */ |
||
82 | 111 | public static function isValid($port) |
|
94 | |||
95 | /** |
||
96 | * Returns a string representation of the port formatted so that it can be compiled into a complete URI string |
||
97 | * per the Uri object's string specification. |
||
98 | * |
||
99 | * If the port is empty, toUriString returns an empty string; if the port is not empty, toUriString returns the |
||
100 | * port prefixed with a colon. |
||
101 | * |
||
102 | * toUriString will also check the port against an optionally-provided scheme; if the port is standard for the given |
||
103 | * scheme, toUriString returns an empty string. |
||
104 | * |
||
105 | * @see Uri::__toString |
||
106 | * |
||
107 | * @param Scheme|null $scheme [optional] A scheme against which to check if the port is the standard |
||
108 | * |
||
109 | * @return string A string representation of the port formatted for a complete URI string |
||
110 | */ |
||
111 | 87 | public function toUriString(Scheme $scheme = null) |
|
123 | |||
124 | /** |
||
125 | * Normalizes the port against a given scheme so that the scheme is reported as null if the port is standard for the |
||
126 | * scheme. |
||
127 | * |
||
128 | * For example, given a http scheme and a port of 80, the normalized port will be reported as null; given a https |
||
129 | * scheme and a port of 80, the normalized port will be reported as 80. |
||
130 | * |
||
131 | * @see Port::toUriString() |
||
132 | * @see SchemePortsTrait |
||
133 | * |
||
134 | * @param Scheme|null $scheme |
||
135 | * @return int|null |
||
136 | */ |
||
137 | 94 | public function normalizePortAgainstScheme(Scheme $scheme = null) |
|
152 | |||
153 | /** |
||
154 | * Validates whether a given port falls within the range of valid ports for TCP/UDP (1 to 65535) |
||
155 | * |
||
156 | * @param int $port The port to be validated |
||
157 | * |
||
158 | * @return bool Returns true if the port falls within the valid range |
||
159 | * Returns false otherwise |
||
160 | */ |
||
161 | 58 | private static function portIsInValidRange($port) |
|
169 | } |
||
170 |