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