Skip to content

Commit fcf37d6

Browse files
authored
Merge pull request codeigniter4#9251 from neznaika0/fix-fetch-global-request
fix: `fetchGlobal()` with numeric key
2 parents 9b6de62 + 2642a9b commit fcf37d6

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-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+
0 => ['foo' => 0],
203+
1 => ['bar' => 1],
204+
2 => ['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
@@ -580,6 +580,26 @@ public function testCallWithJsonRequest(): void
580580
$response->assertJSONExact($data);
581581
}
582582

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

user_guide_src/source/changelogs/v4.5.6.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ Deprecations
2929
**********
3030
Bugs Fixed
3131
**********
32+
- **RequestTrait:** Fixed a bug where the ``fetchGlobal()`` method did not allow handling data by numeric key when stored as a list.
33+
3234
- **Session Library:** The session initialization debug message now uses the correct log type "debug" instead of "info".
3335

3436
- **Validation:** Fixed the `getValidated()` method that did not return valid data when validation rules used multiple asterisks.
37+
3538
- **Database:** Fixed the case insensitivity option in the ``like()`` method when dealing with accented characters.
3639

3740
- **Parser:** Fixed bug that caused equal key names to be replaced by the key name defined first.

0 commit comments

Comments
 (0)