Skip to content

Commit 4173a67

Browse files
committed
Improve Encoder features
1 parent 9a50e91 commit 4173a67

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

interfaces/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ All Notable changes to `League\Uri\Interfaces` will be documented in this file
3737
- `Encoder::isFragmentEncoded`
3838
- `Encoder::normalizeUser`
3939
- `Encoder::normalizePassword`
40+
- `Encoder::normalizeUserInfo`
4041
- `Encoder::normalizePath`
4142
- `Encoder::normalizeQuery`
4243
- `Encoder::normalizeFragment`

interfaces/Encoder.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use SensitiveParameter;
2222
use Stringable;
2323

24+
use function explode;
2425
use function filter_var;
2526
use function gettype;
2627
use function in_array;
@@ -143,6 +144,36 @@ public static function isUserInfoEncoded(#[SensitiveParameter] Stringable|string
143144
&& self::isPasswordEncoded($password);
144145
}
145146

147+
public static function encodeUserInfo(#[SensitiveParameter] Stringable|string|null $userInfo): ?string
148+
{
149+
if (null === $userInfo) {
150+
return null;
151+
}
152+
153+
[$user, $password] = explode(':', (string) $userInfo, 2) + [1 => null];
154+
$userInfo = self::encodeUser($user);
155+
if (null === $password) {
156+
return $userInfo;
157+
}
158+
159+
return $userInfo.':'.self::encodePassword($password);
160+
}
161+
162+
public static function normalizeUserInfo(#[SensitiveParameter] Stringable|string|null $userInfo): ?string
163+
{
164+
if (null === $userInfo) {
165+
return null;
166+
}
167+
168+
[$user, $password] = explode(':', (string) $userInfo, 2) + [1 => null];
169+
$userInfo = self::normalizeUser($user);
170+
if (null === $password) {
171+
return $userInfo;
172+
}
173+
174+
return $userInfo.':'.self::normalizePassword($password);
175+
}
176+
146177
/**
147178
* Decodes all the URI component characters.
148179
*/

0 commit comments

Comments
 (0)