1
|
|
|
<?php |
2
|
|
|
namespace Subreality\Dilmun\Anshar\Http\UriParts; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class Port |
6
|
|
|
* @package Subreality\Dilmun\Anshar\Http\UriParts |
7
|
|
|
*/ |
8
|
|
|
class Port implements UriPartInterface |
9
|
|
|
{ |
10
|
|
|
protected $port = null; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Port constructor. Accepts an integer or null value representing a URI port component. Construction will throw an |
14
|
|
|
* exception if port is not an integer or is not null or if the given integer is outside of the allowed TCP/UDP |
15
|
|
|
* range of 1 to 65535. |
16
|
|
|
* |
17
|
|
|
* port = *DIGIT |
18
|
|
|
* |
19
|
|
|
* @see https://tools.ietf.org/html/rfc3986#appendix-A |
20
|
|
|
* |
21
|
|
|
* @throws \InvalidArgumentException if the given port is not an integer or null or if the given integer is out of |
22
|
|
|
* range |
23
|
|
|
* |
24
|
|
|
* @param int|null $port A string representing a URI port |
25
|
|
|
*/ |
26
|
11 |
|
public function __construct($port) |
27
|
|
|
{ |
28
|
11 |
|
if (!is_int($port) && !is_null($port)) { |
29
|
6 |
|
throw new \InvalidArgumentException("Port must be an integer or null"); |
30
|
5 |
|
} elseif (!self::isValid($port)) { |
31
|
3 |
|
throw new \InvalidArgumentException("Port must be between 1 and 65535 inclusive"); |
32
|
|
|
} |
33
|
|
|
|
34
|
2 |
|
$this->port = $port; |
35
|
2 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns a string representation of the port. |
39
|
|
|
* |
40
|
|
|
* @return string The string representation of the port |
41
|
|
|
*/ |
42
|
3 |
|
public function __toString() |
43
|
|
|
{ |
44
|
3 |
|
return (string) $this->port; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Validates a given port. A port is valid if it is either null or an integer between 1 and 65535 inclusive. |
49
|
|
|
* |
50
|
|
|
* @param mixed $port The port to be validated |
51
|
|
|
* @return bool Returns true if the given port is valid |
52
|
|
|
* Returns false otherwise |
53
|
|
|
*/ |
54
|
10 |
|
public static function isValid($port) |
55
|
|
|
{ |
56
|
10 |
|
$port_valid = false; |
57
|
|
|
|
58
|
10 |
|
if (is_int($port)) { |
59
|
7 |
|
$port_valid = self::portIsInValidRange($port); |
60
|
10 |
|
} elseif (is_null($port)) { |
61
|
2 |
|
$port_valid = true; |
62
|
2 |
|
} |
63
|
|
|
|
64
|
10 |
|
return $port_valid; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns a string representation of the port formatted so that it can be compiled into a complete URI string |
69
|
|
|
* per the Uri object's string specification. |
70
|
|
|
* |
71
|
|
|
* If the port is empty, toUriString returns an empty string; if the port is not empty, toUriString returns the |
72
|
|
|
* port prefixed with a colon. |
73
|
|
|
* |
74
|
|
|
* @see Uri::__toString |
75
|
|
|
* |
76
|
|
|
* @return string A string representation of the port formatted for a complete URI string |
77
|
|
|
*/ |
78
|
2 |
|
public function toUriString() |
79
|
|
|
{ |
80
|
2 |
|
$uri_string = (string) $this->port; |
81
|
|
|
|
82
|
2 |
|
if (!is_null($this->port)) { |
83
|
1 |
|
$uri_string = ":" . $uri_string; |
84
|
1 |
|
} |
85
|
|
|
|
86
|
2 |
|
return $uri_string; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Validates whether a given port falls within the range of valid ports for TCP/UDP (1 to 65535) |
91
|
|
|
* |
92
|
|
|
* @param int $port The port to be validated |
93
|
|
|
* |
94
|
|
|
* @return bool Returns true if the port falls within the valid range |
95
|
|
|
* Returns false otherwise |
96
|
|
|
*/ |
97
|
7 |
|
private static function portIsInValidRange($port) |
98
|
|
|
{ |
99
|
7 |
|
if ($port > 0 && $port < 65536) { |
|
|
|
|
100
|
2 |
|
return true; |
101
|
|
|
} else { |
102
|
5 |
|
return false; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|