Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 84ecde8

Browse files
committed
FIX uri support encoding from array.
1 parent 277b3ca commit 84ecde8

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

‎src/utils/UriHelper.php‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@
99
class UriHelper
1010
{
1111
# region Private methods
12-
private static function mixUrlEncodedParams(string $encoded_string, array $map, $replace = true, $toString = true): string
13-
{
12+
private static function mixUrlEncodedParams(
13+
string $encoded_string,
14+
array $map,
15+
bool $replace = true,
16+
bool $toString = true
17+
): string {
1418
parse_str($encoded_string, $params);
1519
if ($toString) {
1620
$map = array_map(function ($v) {
17-
return (string)$v;
21+
if (is_object($v)) {
22+
return (string)$v;
23+
}
24+
return $v;
1825
}, $map);
1926
}
2027
$params = $replace ? array_merge($params, $map) : array_merge($map, $params);

‎test/UriHelperTest.php‎

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,32 @@ private static function callPrivateStatic($obj, string $method, ...$args)
1414
}
1515
public function testUriMapping()
1616
{
17-
$uri = static::callPrivateStatic(UriHelper::class, 'mapReplaceString',
17+
$uri = static::callPrivateStatic(
18+
UriHelper::class,
19+
'mapReplaceString',
1820
'https://example.com/collection/{id}/{id2}',
19-
['id'=>20, 'id2'=>30]
21+
['id' => 20, 'id2' => 30]
2022
);
2123

2224
$this->assertEquals('https://example.com/collection/20/30', $uri);
2325
}
2426

27+
public function testEncodeParams()
28+
{
29+
$ref = new ReflectionClass(UriHelper::class);
30+
$method = $ref->getMethod('mixUrlEncodedParams');
31+
$method->setAccessible(true);
32+
33+
$this->assertEquals('a=1&b=2', $method->invokeArgs(null, [
34+
'', // encoded_string
35+
['a' => 1, 'b' => 2] // map
36+
]));
37+
$this->assertEquals('a=1&b%5B0%5D=2&b%5B1%5D=3', $method->invokeArgs(null, [
38+
'',
39+
['a' => 1, 'b' => [2, 3]]
40+
]));
41+
}
42+
2543
public function testGetPathInfo()
2644
{
2745
$this->assertEquals('/some/path', UriHelper::getPathInfo('/some/path', '/'));
@@ -37,42 +55,42 @@ public function testGetBaseUrl()
3755
{
3856
$this->assertEquals(
3957
'http://localhost/assets/css/style.css',
40-
UriHelper::getBaseUrl('/assets/css/style.css',[
41-
'HTTP_HOST'=>'localhost',
58+
UriHelper::getBaseUrl('/assets/css/style.css',[
59+
'HTTP_HOST' => 'localhost',
4260
'SCRIPT_NAME' => '/index.php'
4361
])
4462
);
4563
$this->assertEquals(
4664
'https://localhost/assets/css/style.css',
47-
UriHelper::getBaseUrl('/assets/css/style.css',[
48-
'HTTP_HOST'=>'localhost',
49-
'HTTPS'=>'on',
65+
UriHelper::getBaseUrl('/assets/css/style.css',[
66+
'HTTP_HOST' => 'localhost',
67+
'HTTPS' => 'on',
5068
'SCRIPT_NAME' => '/index.php'
5169
])
5270
);
5371
$this->assertEquals(
5472
'https://localhost/public/assets/css/style.css',
55-
UriHelper::getBaseUrl('/assets/css/style.css',[
56-
'HTTP_HOST'=>'localhost',
57-
'HTTPS'=>'on',
73+
UriHelper::getBaseUrl('/assets/css/style.css',[
74+
'HTTP_HOST' => 'localhost',
75+
'HTTPS' => 'on',
5876
'SCRIPT_NAME' => '/public/index.php'
5977
])
6078
);
6179
$this->assertEquals(
6280
'https://localhost/public/assets/css/style.css',
63-
UriHelper::getBaseUrl('/assets/css/style.css',[
64-
'HTTPS'=>'on',
65-
'SERVER_NAME'=>'localhost',
66-
'SERVER_PORT'=>443,
81+
UriHelper::getBaseUrl('/assets/css/style.css',[
82+
'HTTPS' => 'on',
83+
'SERVER_NAME' => 'localhost',
84+
'SERVER_PORT' => 443,
6785
'SCRIPT_NAME' => '/public/index.php'
6886
])
6987
);
7088
$this->assertEquals(
7189
'https://localhost:3000/public/assets/css/style.css',
72-
UriHelper::getBaseUrl('/assets/css/style.css',[
73-
'HTTPS'=>'on',
74-
'SERVER_NAME'=>'localhost',
75-
'SERVER_PORT'=>3000,
90+
UriHelper::getBaseUrl('/assets/css/style.css',[
91+
'HTTPS' => 'on',
92+
'SERVER_NAME' => 'localhost',
93+
'SERVER_PORT' => 3000,
7694
'SCRIPT_NAME' => '/public/index.php'
7795
])
7896
);
@@ -90,4 +108,4 @@ public function testGetSiteUrl()
90108
])
91109
);
92110
}
93-
}
111+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /