Skip to content

Commit 30a0a87

Browse files
committed
fix: Allows get by numeric key
1 parent 65872e5 commit 30a0a87

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

system/HTTP/RequestTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public function setGlobal(string $name, $value)
249249
*
250250
* @param string $name Supergrlobal name (lowercase)
251251
* @phpstan-param 'get'|'post'|'request'|'cookie'|'server' $name
252-
* @param array|string|null $index
252+
* @param array|int|string|null $index
253253
* @param int|null $filter Filter constant
254254
* @param array|int|null $flags Options
255255
*
@@ -290,7 +290,7 @@ public function fetchGlobal(string $name, $index = null, ?int $filter = null, $f
290290
}
291291

292292
// Does the index contain array notation?
293-
if (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) {
293+
if (is_string($index) && ($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) {
294294
$value = $this->globals[$name];
295295

296296
for ($i = 0; $i < $count; $i++) {

tests/system/HTTP/RequestTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,21 @@ public function testFetchGlobalReturnsArrayValues(): void
196196
$this->assertCount(2, $result['ANNOUNCEMENTS']);
197197
}
198198

199+
public function testFetchGlobalReturnsWithListValues(): void
200+
{
201+
$post = [
202+
['foo' => 0],
203+
['bar' => 1],
204+
['baz' => 2],
205+
];
206+
207+
$this->request->setGlobal('post', $post);
208+
$result = $this->request->fetchGlobal('post');
209+
210+
$this->assertIsList($result);
211+
$this->assertSame($post, $result);
212+
}
213+
199214
public function testFetchGlobalWithArrayTop(): void
200215
{
201216
$post = [

tests/system/Test/FeatureTestTraitTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,26 @@ public function testCallWithJsonRequest(): void
581581
$response->assertJSONExact($data);
582582
}
583583

584+
public function testCallWithListJsonRequest(): void
585+
{
586+
$this->withRoutes([
587+
[
588+
'POST',
589+
'home',
590+
'\Tests\Support\Controllers\Popcorn::echoJson',
591+
],
592+
]);
593+
$data = [
594+
['one' => 1, 'two' => 2],
595+
['one' => 3, 'two' => 4],
596+
];
597+
$response = $this->withBodyFormat('json')
598+
->call(Method::POST, 'home', $data);
599+
600+
$response->assertOK();
601+
$response->assertJSONExact($data);
602+
}
603+
584604
public function testSetupRequestBodyWithParams(): void
585605
{
586606
$request = $this->setupRequest('post', 'home');

0 commit comments

Comments
 (0)