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.2.0 |
10
|
|
|
* @link https://github.com/thephpleague/uri/ |
11
|
|
|
*/ |
12
|
|
|
namespace League\Uri\Components; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use League\Uri\Interfaces\Pass as PassInterface; |
16
|
|
|
use League\Uri\Interfaces\UriPart; |
17
|
|
|
use League\Uri\Interfaces\User as UserInterface; |
18
|
|
|
use League\Uri\Interfaces\UserInfo as UserInfoInterface; |
19
|
|
|
use League\Uri\Types\ImmutablePropertyTrait; |
20
|
|
|
use League\Uri\Types\ValidatorTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Value object representing the UserInfo part of an URI. |
24
|
|
|
* |
25
|
|
|
* @package League.uri |
26
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
27
|
|
|
* @since 4.0.0 |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
class UserInfo implements UserInfoInterface |
31
|
|
|
{ |
32
|
|
|
use ImmutablePropertyTrait; |
33
|
|
|
use ValidatorTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* User Component |
37
|
|
|
* |
38
|
|
|
* @var User |
39
|
|
|
*/ |
40
|
|
|
protected $user; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Pass Component |
44
|
|
|
* |
45
|
|
|
* @var Pass |
46
|
|
|
*/ |
47
|
|
|
protected $pass; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Create a new instance of UserInfo |
51
|
|
|
* |
52
|
|
|
* @param UserInterface|string $user |
53
|
|
|
* @param PassInterface|string $pass |
54
|
|
|
*/ |
55
|
817 |
|
public function __construct($user = null, $pass = null) |
56
|
|
|
{ |
57
|
817 |
|
$this->user = !$user instanceof UserInterface ? new User($user) : $user; |
|
|
|
|
58
|
817 |
|
$this->pass = !$pass instanceof PassInterface ? new Pass($pass) : $pass; |
|
|
|
|
59
|
817 |
|
$this->assertValidObject(); |
60
|
817 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Retrieve the user component of the URI User Info part |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
276 |
|
public function getUser() |
68
|
|
|
{ |
69
|
276 |
|
return $this->user->__toString(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Retrieve the pass component of the URI User Info part |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
270 |
|
public function getPass() |
78
|
|
|
{ |
79
|
270 |
|
return $this->pass->__toString(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritdoc |
84
|
|
|
*/ |
85
|
2 |
|
public function __debugInfo() |
86
|
|
|
{ |
87
|
2 |
|
return ['userInfo' => $this->getContent()]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritdoc |
92
|
|
|
*/ |
93
|
12 |
|
public static function __set_state(array $properties) |
94
|
|
|
{ |
95
|
12 |
|
return new static($properties['user'], $properties['pass']); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the component literal value. |
100
|
|
|
* |
101
|
|
|
* @return null|string |
102
|
|
|
*/ |
103
|
829 |
|
public function getContent() |
104
|
|
|
{ |
105
|
829 |
|
$userInfo = $this->user->__toString(); |
106
|
829 |
|
if ('' === $userInfo) { |
107
|
735 |
|
return $this->user->getContent(); |
108
|
|
|
} |
109
|
|
|
|
110
|
154 |
|
$pass = $this->pass->__toString(); |
111
|
154 |
|
if ('' !== $pass) { |
112
|
145 |
|
$userInfo .= UserInfoInterface::SEPARATOR.$pass; |
113
|
96 |
|
} |
114
|
|
|
|
115
|
154 |
|
return $userInfo; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns the instance string representation; If the |
120
|
|
|
* instance is not defined an empty string is returned |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
823 |
|
public function __toString() |
125
|
|
|
{ |
126
|
823 |
|
return (string) $this->getContent(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns the instance string representation |
131
|
|
|
* with its optional URI delimiters |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
788 |
|
public function getUriComponent() |
136
|
|
|
{ |
137
|
788 |
|
$component = $this->__toString(); |
138
|
788 |
|
if ('' !== $component) { |
139
|
134 |
|
$component .= UserInfoInterface::DELIMITER; |
140
|
89 |
|
} |
141
|
|
|
|
142
|
788 |
|
return $component; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* DEPRECATION WARNING! This method will be removed in the next major point release |
147
|
|
|
* |
148
|
|
|
* @deprecated deprecated since version 4.2 |
149
|
|
|
* |
150
|
|
|
* Returns whether two UriPart objects represent the same value |
151
|
|
|
* The comparison is based on the getUriComponent method |
152
|
|
|
* |
153
|
|
|
* @param UriPart $component |
154
|
|
|
* |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
public function sameValueAs(UriPart $component) |
158
|
|
|
{ |
159
|
|
|
return $this->getUriComponent() === $component->getUriComponent(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Return an instance with the specified user. |
164
|
|
|
* |
165
|
|
|
* This method MUST retain the state of the current instance, and return |
166
|
|
|
* an instance that contains the specified user. |
167
|
|
|
* |
168
|
|
|
* An empty user is equivalent to removing the user information. |
169
|
|
|
* |
170
|
|
|
* @param string $user The user to use with the new instance. |
171
|
|
|
* |
172
|
|
|
* @throws \InvalidArgumentException for invalid user. |
173
|
|
|
* |
174
|
|
|
* @return static |
175
|
|
|
*/ |
176
|
243 |
|
public function withUser($user) |
177
|
|
|
{ |
178
|
243 |
|
return $this->withProperty('user', $user); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Return an instance with the specified password. |
183
|
|
|
* |
184
|
|
|
* This method MUST retain the state of the current instance, and return |
185
|
|
|
* an instance that contains the specified password. |
186
|
|
|
* |
187
|
|
|
* An empty password is equivalent to removing the password. |
188
|
|
|
* |
189
|
|
|
* @param string $pass The password to use with the new instance. |
190
|
|
|
* |
191
|
|
|
* @throws \InvalidArgumentException for invalid password. |
192
|
|
|
* |
193
|
|
|
* @return static |
194
|
|
|
*/ |
195
|
243 |
|
public function withPass($pass) |
196
|
|
|
{ |
197
|
243 |
|
return $this->withProperty('pass', $pass); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Create a new instance from a string |
202
|
|
|
* |
203
|
|
|
* @param string $str |
|
|
|
|
204
|
|
|
* |
205
|
|
|
* @return static |
206
|
|
|
*/ |
207
|
21 |
|
public function withContent($content = null) |
208
|
|
|
{ |
209
|
21 |
|
if ($content === $this->getContent()) { |
210
|
3 |
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
21 |
|
if (null !== $content && !is_string($content)) { |
214
|
3 |
|
throw new InvalidArgumentException(sprintf( |
215
|
3 |
|
'Expected data to be a string or NULL; received "%s"', |
216
|
2 |
|
gettype($content) |
217
|
2 |
|
)); |
218
|
|
|
} |
219
|
|
|
|
220
|
18 |
|
$res = explode(UserInfoInterface::SEPARATOR, $content, 2); |
221
|
18 |
|
$new = new static(array_shift($res), array_shift($res)); |
222
|
18 |
|
if ($new->getContent() === $this->getContent()) { |
223
|
3 |
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
15 |
|
return $new; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @inheritdoc |
231
|
|
|
*/ |
232
|
817 |
|
protected function assertValidObject() |
233
|
|
|
{ |
234
|
817 |
|
} |
235
|
|
|
} |
236
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.