|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is a part of Woketo package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Nekland <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full license, take a look to the LICENSE file |
|
9
|
|
|
* on the root directory of this project |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Nekland\Woketo\Http; |
|
13
|
|
|
|
|
14
|
|
|
use Nekland\Tools\StringTools; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Url |
|
18
|
|
|
* |
|
19
|
|
|
* Represent a WebSocket URL |
|
20
|
|
|
* |
|
21
|
|
|
* @internal |
|
22
|
|
|
*/ |
|
23
|
|
|
class Url |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var bool |
|
27
|
|
|
*/ |
|
28
|
|
|
private $secured; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* example: 127.0.0.1 |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $host; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* example: 8080 |
|
39
|
|
|
* |
|
40
|
|
|
* @var int |
|
41
|
|
|
*/ |
|
42
|
|
|
private $port; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* example: /chat |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
private $uri; |
|
50
|
|
|
|
|
51
|
|
|
public function __construct(string $url = null) |
|
52
|
|
|
{ |
|
53
|
|
|
if (null !== $url) { |
|
54
|
|
|
$this->initialize($url); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
|
|
public function isSecured(): bool |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->secured; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param bool $secured |
|
68
|
|
|
* @return Url |
|
69
|
|
|
*/ |
|
70
|
|
|
public function setSecured(bool $secured = true) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->secured = $secured; |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getHost(): string |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->host; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $host |
|
87
|
|
|
* @return Url |
|
88
|
|
|
*/ |
|
89
|
|
|
public function setHost(string $host) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->host = $host; |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return int |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getPort(): int |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->port; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param int $port |
|
106
|
|
|
* @return Url |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setPort(int $port) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->port = $port; |
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getUri(): string |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->uri; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param string $uri |
|
125
|
|
|
* @return Url |
|
126
|
|
|
*/ |
|
127
|
|
|
public function setUri($uri) |
|
128
|
|
|
{ |
|
129
|
|
|
$this->uri = $uri; |
|
130
|
|
|
|
|
131
|
|
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Fill the object with given URL as string. |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $url |
|
138
|
|
|
*/ |
|
139
|
|
|
private function initialize(string $url) |
|
140
|
|
|
{ |
|
141
|
|
|
$match = preg_match('/^wss?:\/\/(.+):([\d]{2,5})(\/.+)?/', $url, $matches); |
|
142
|
|
|
|
|
143
|
|
|
if ($match !== 1 || !in_array(count($matches), [3, 4])) { |
|
144
|
|
|
throw new \InvalidArgumentException(sprintf('The URL %s is invalid.', $url)); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$this->secured = StringTools::startsWith($url, 'wss'); |
|
148
|
|
|
$this->host = $matches[1]; |
|
149
|
|
|
$this->port = $matches[2]; |
|
|
|
|
|
|
150
|
|
|
$this->uri = isset($matches[3]) ? $matches[3] : '/'; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public function __toString() |
|
157
|
|
|
{ |
|
158
|
|
|
$str = $this->secured ? 'wss://' : 'ws://'; |
|
159
|
|
|
$str .= $this->host . ':' . $this->port . $this->uri; |
|
160
|
|
|
|
|
161
|
|
|
return $str; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.