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 League\Uri\Interfaces\Pass as PassInterface; |
15
|
|
|
use League\Uri\Interfaces\UriPart; |
16
|
|
|
use League\Uri\Interfaces\User as UserInterface; |
17
|
|
|
use League\Uri\Interfaces\UserInfo as UserInfoInterface; |
18
|
|
|
use League\Uri\Types\ImmutablePropertyTrait; |
19
|
|
|
use League\Uri\Types\ValidatorTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Value object representing the UserInfo part of an URI. |
23
|
|
|
* |
24
|
|
|
* @package League.uri |
25
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
26
|
|
|
* @since 4.0.0 |
27
|
|
|
* |
28
|
|
|
*/ |
29
|
|
|
class UserInfo implements UserInfoInterface |
30
|
|
|
{ |
31
|
|
|
use ImmutablePropertyTrait; |
32
|
|
|
use ValidatorTrait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* User Component |
36
|
|
|
* |
37
|
|
|
* @var User |
38
|
|
|
*/ |
39
|
|
|
protected $user; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Pass Component |
43
|
|
|
* |
44
|
|
|
* @var Pass |
45
|
|
|
*/ |
46
|
|
|
protected $pass; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create a new instance from a string |
50
|
|
|
* |
51
|
|
|
* @param string $str |
52
|
|
|
* |
53
|
|
|
* @return static |
54
|
|
|
*/ |
55
|
15 |
|
public static function createFromString($str) |
56
|
|
|
{ |
57
|
15 |
|
$res = explode(UserInfoInterface::SEPARATOR, self::validateString($str), 2); |
58
|
|
|
|
59
|
15 |
|
return new static( |
60
|
15 |
|
new User(array_shift($res)), |
61
|
15 |
|
new Pass(array_shift($res)) |
62
|
10 |
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Create a new instance of UserInfo |
67
|
|
|
* |
68
|
|
|
* @param UserInterface|string $user |
69
|
|
|
* @param PassInterface|string $pass |
70
|
|
|
*/ |
71
|
766 |
|
public function __construct($user = null, $pass = null) |
72
|
|
|
{ |
73
|
766 |
|
$this->user = !$user instanceof UserInterface ? new User($user) : $user; |
|
|
|
|
74
|
766 |
|
$this->pass = !$pass instanceof PassInterface ? new Pass($pass) : $pass; |
|
|
|
|
75
|
766 |
|
$this->assertValidObject(); |
76
|
766 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Retrieve the user component of the URI User Info part |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
246 |
|
public function getUser() |
84
|
|
|
{ |
85
|
246 |
|
return $this->user->__toString(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Retrieve the pass component of the URI User Info part |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
240 |
|
public function getPass() |
94
|
|
|
{ |
95
|
240 |
|
return $this->pass->__toString(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritdoc |
100
|
|
|
*/ |
101
|
2 |
|
public function __debugInfo() |
102
|
|
|
{ |
103
|
2 |
|
return ['userInfo' => $this->getContent()]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritdoc |
108
|
|
|
*/ |
109
|
12 |
|
public static function __set_state(array $properties) |
110
|
|
|
{ |
111
|
12 |
|
return new static($properties['user'], $properties['pass']); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns the component literal value. |
116
|
|
|
* |
117
|
|
|
* @return null|string |
118
|
|
|
*/ |
119
|
760 |
|
public function getContent() |
120
|
|
|
{ |
121
|
760 |
|
$userInfo = $this->user->__toString(); |
122
|
760 |
|
if ('' === $userInfo) { |
123
|
657 |
|
return $this->user->getContent(); |
124
|
|
|
} |
125
|
|
|
|
126
|
151 |
|
$pass = $this->pass->__toString(); |
127
|
151 |
|
if ('' !== $pass) { |
128
|
142 |
|
$userInfo .= UserInfoInterface::SEPARATOR.$pass; |
129
|
94 |
|
} |
130
|
|
|
|
131
|
151 |
|
return $userInfo; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns the instance string representation; If the |
136
|
|
|
* instance is not defined an empty string is returned |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
760 |
|
public function __toString() |
141
|
|
|
{ |
142
|
760 |
|
return (string) $this->getContent(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns the instance string representation |
147
|
|
|
* with its optional URI delimiters |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
725 |
|
public function getUriComponent() |
152
|
|
|
{ |
153
|
725 |
|
$component = $this->__toString(); |
154
|
725 |
|
if ('' !== $component) { |
155
|
134 |
|
$component .= UserInfoInterface::DELIMITER; |
156
|
89 |
|
} |
157
|
|
|
|
158
|
725 |
|
return $component; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns whether two UriPart objects represent the same value |
163
|
|
|
* The comparison is based on the getUriComponent method |
164
|
|
|
* |
165
|
|
|
* @param UriPart $component |
166
|
|
|
* |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
12 |
|
public function sameValueAs(UriPart $component) |
170
|
|
|
{ |
171
|
12 |
|
return $this->getUriComponent() === $component->getUriComponent(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Return an instance with the specified user. |
176
|
|
|
* |
177
|
|
|
* This method MUST retain the state of the current instance, and return |
178
|
|
|
* an instance that contains the specified user. |
179
|
|
|
* |
180
|
|
|
* An empty user is equivalent to removing the user information. |
181
|
|
|
* |
182
|
|
|
* @param string $user The user to use with the new instance. |
183
|
|
|
* |
184
|
|
|
* @throws \InvalidArgumentException for invalid user. |
185
|
|
|
* |
186
|
|
|
* @return static |
187
|
|
|
*/ |
188
|
213 |
|
public function withUser($user) |
189
|
|
|
{ |
190
|
213 |
|
return $this->withProperty('user', $user); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Return an instance with the specified password. |
195
|
|
|
* |
196
|
|
|
* This method MUST retain the state of the current instance, and return |
197
|
|
|
* an instance that contains the specified password. |
198
|
|
|
* |
199
|
|
|
* An empty password is equivalent to removing the password. |
200
|
|
|
* |
201
|
|
|
* @param string $pass The password to use with the new instance. |
202
|
|
|
* |
203
|
|
|
* @throws \InvalidArgumentException for invalid password. |
204
|
|
|
* |
205
|
|
|
* @return static |
206
|
|
|
*/ |
207
|
213 |
|
public function withPass($pass) |
208
|
|
|
{ |
209
|
213 |
|
return $this->withProperty('pass', $pass); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @inheritdoc |
214
|
|
|
*/ |
215
|
766 |
|
protected function assertValidObject() |
216
|
|
|
{ |
217
|
766 |
|
} |
218
|
|
|
} |
219
|
|
|
|
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.