1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* League.Uri (http://uri.thephpleague.com) |
4
|
|
|
* |
5
|
|
|
* @package League.uri |
6
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
7
|
|
|
* @copyright 2013-2015 Ignace Nyamagana Butera |
8
|
|
|
* @license https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License) |
9
|
|
|
* @version 4.1.1 |
10
|
|
|
* @link https://github.com/thephpleague/uri/ |
11
|
|
|
*/ |
12
|
|
|
namespace League\Uri\Components; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use League\Uri\Interfaces\Port as PortInterface; |
16
|
|
|
use League\Uri\Types\ValidatorTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Value object representing a URI port component. |
20
|
|
|
* |
21
|
|
|
* @package League.uri |
22
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
23
|
|
|
* @since 1.0.0 |
24
|
|
|
*/ |
25
|
|
|
class Port extends AbstractComponent implements PortInterface |
26
|
|
|
{ |
27
|
|
|
use ValidatorTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Validate Port data |
31
|
|
|
* |
32
|
|
|
* @param null|int $port |
33
|
|
|
* |
34
|
|
|
* @throws InvalidArgumentException if the submitted port is invalid |
35
|
|
|
* |
36
|
|
|
* @return null|int |
37
|
|
|
*/ |
38
|
205 |
|
protected function validate($port) |
39
|
|
|
{ |
40
|
205 |
|
if ('' === $port) { |
41
|
3 |
|
throw new InvalidArgumentException( |
42
|
1 |
|
'Expected port to be a int or null; received an empty string' |
43
|
2 |
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
202 |
|
return $this->validatePort($port); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns the instance string representation |
51
|
|
|
* with its optional URI delimiters |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
158 |
|
public function getUriComponent() |
56
|
|
|
{ |
57
|
158 |
|
return null === $this->data ? '' : PortInterface::DELIMITER.$this->data; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Return an integer representation of the Port component |
62
|
|
|
* |
63
|
|
|
* @return null|int |
64
|
|
|
*/ |
65
|
583 |
|
public function toInt() |
66
|
|
|
{ |
67
|
583 |
|
return $this->data; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Initialize the Port data |
72
|
|
|
* |
73
|
|
|
* @param null|int $data |
74
|
|
|
*/ |
75
|
205 |
|
protected function init($data) |
76
|
|
|
{ |
77
|
205 |
|
$this->data = $this->validate($data); |
78
|
178 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritdoc |
82
|
|
|
*/ |
83
|
2 |
|
public function __debugInfo() |
84
|
|
|
{ |
85
|
2 |
|
return ['port' => $this->toInt()]; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|