Skip to content

Commit 2a26047

Browse files
committed
fix: support array indexes in getPostGet() and getGetPost()
1 parent 01da624 commit 2a26047

4 files changed

Lines changed: 59 additions & 5 deletions

File tree

system/HTTP/IncomingRequest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,16 @@ public function getPostGet($index = null, $filter = null, $flags = null)
602602
return array_merge($this->getGet($index, $filter, $flags), $this->getPost($index, $filter, $flags));
603603
}
604604

605+
if (is_array($index)) {
606+
$output = [];
607+
608+
foreach ($index as $key) {
609+
$output[$key] = $this->getPostGet($key, $filter, $flags);
610+
}
611+
612+
return $output;
613+
}
614+
605615
// Use $_POST directly here, since filter_has_var only
606616
// checks the initial POST data, not anything that might
607617
// have been added since.
@@ -625,6 +635,16 @@ public function getGetPost($index = null, $filter = null, $flags = null)
625635
return array_merge($this->getPost($index, $filter, $flags), $this->getGet($index, $filter, $flags));
626636
}
627637

638+
if (is_array($index)) {
639+
$output = [];
640+
641+
foreach ($index as $key) {
642+
$output[$key] = $this->getGetPost($key, $filter, $flags);
643+
}
644+
645+
return $output;
646+
}
647+
628648
// Use $_GET directly here, since filter_has_var only
629649
// checks the initial GET data, not anything that might
630650
// have been added since.

tests/system/HTTP/IncomingRequestTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,34 @@ public function testCanGrabPostBeforeGet(): void
101101
$this->assertSame('3', $this->request->getGetPost('TEST'));
102102
}
103103

104+
public function testCanGrabMultiplePostAndGetVars(): void
105+
{
106+
service('superglobals')
107+
->setPostArray([
108+
'post' => 'post value',
109+
'shared' => 'post shared value',
110+
])
111+
->setGetArray([
112+
'get' => 'get value',
113+
'shared' => 'get shared value',
114+
]);
115+
116+
$index = ['post', 'get', 'shared', 'missing'];
117+
118+
$this->assertSame([
119+
'post' => 'post value',
120+
'get' => 'get value',
121+
'shared' => 'post shared value',
122+
'missing' => null,
123+
], $this->request->getPostGet($index));
124+
$this->assertSame([
125+
'post' => 'post value',
126+
'get' => 'get value',
127+
'shared' => 'get shared value',
128+
'missing' => null,
129+
], $this->request->getGetPost($index));
130+
}
131+
104132
public function testNoOldInput(): void
105133
{
106134
$this->assertNull($this->request->getOldInput('name'));

user_guide_src/source/changelogs/v4.7.4.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Bugs Fixed
8484
- **Database:** Fixed a bug where ``updateBatch()`` could be called after Query Builder ``where()`` conditions, even though it's not supported. In this situation, now the ``DatabaseException`` is thrown.
8585
- **Encryption:** Fixed bugs in ``SodiumHandler`` where runtime ``blockSize`` overrides without ``key`` were handled incorrectly, invalid key lengths could leak native Sodium errors, and mismatched decrypt ``blockSize`` values could throw ``SodiumException`` instead of ``EncryptionException``.
8686
- **Filters:** Fixed a bug in ``InvalidChars`` filter where invalid UTF-8 or control characters in array keys were not checked.
87+
- **HTTP:** Fixed a bug where ``IncomingRequest::getPostGet()`` and ``IncomingRequest::getGetPost()`` threw a ``TypeError`` when passed an array of indexes.
8788
- **HTTP:** Fixed a bug where the User Agent library reported Safari's WebKit version instead of the browser version from the ``Version`` token.
8889
- **Model:** Fixed a bug in ``Model::objectToRawArray()`` where the ``$recursive`` parameter was ignored.
8990
- **Security:** Fixed a bug where ``CheckPhpIni`` could raise a type error when ``ini_get_all()`` returned ``null`` for a configured directive value.

user_guide_src/source/incoming/incomingrequest.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ The methods provided by the parent classes that are available are:
384384

385385
.. php:method:: getGet([$index = null[, $filter = null[, $flags = null]]])
386386
387-
:param string $index: The name of the variable/key to look for.
387+
:param array|string $index: The name of the variable/key to look for, or an array of names.
388388
:param int $filter: The type of filter to apply. A list of filters can be found in
389389
`Types of filters <https://www.php.net/manual/en/filters.php>`__.
390390
:param int $flags: Flags to apply. A list of flags can be found in
@@ -423,7 +423,7 @@ The methods provided by the parent classes that are available are:
423423

424424
.. php:method:: getPost([$index = null[, $filter = null[, $flags = null]]])
425425
426-
:param string $index: The name of the variable/key to look for.
426+
:param array|string $index: The name of the variable/key to look for, or an array of names.
427427
:param int $filter: The type of filter to apply. A list of filters can be
428428
found `here <https://www.php.net/manual/en/filters.php>`__.
429429
:param int $flags: Flags to apply. A list of flags can be found
@@ -435,7 +435,7 @@ The methods provided by the parent classes that are available are:
435435

436436
.. php:method:: getPostGet([$index = null[, $filter = null[, $flags = null]]])
437437
438-
:param string $index: The name of the variable/key to look for.
438+
:param array|string $index: The name of the variable/key to look for, or an array of names.
439439
:param int $filter: The type of filter to apply. A list of filters can be found in
440440
`Types of filters <https://www.php.net/manual/en/filters.php>`__.
441441
:param int $flags: Flags to apply. A list of flags can be found in
@@ -450,12 +450,15 @@ The methods provided by the parent classes that are available are:
450450

451451
.. literalinclude:: incomingrequest/032.php
452452

453+
If an array of indexes is specified, the method returns an associative array and applies
454+
the POST-then-GET lookup order to each index.
455+
453456
If no index is specified, it will return both POST and GET streams combined.
454457
Although POST data will be preferred in case of name conflict.
455458

456459
.. php:method:: getGetPost([$index = null[, $filter = null[, $flags = null]]])
457460
458-
:param string $index: The name of the variable/key to look for.
461+
:param array|string $index: The name of the variable/key to look for, or an array of names.
459462
:param int $filter: The type of filter to apply. A list of filters can be found in
460463
`Types of filters <https://www.php.net/manual/en/filters.php>`__.
461464
:param int $flags: Flags to apply. A list of flags can be found in
@@ -470,6 +473,9 @@ The methods provided by the parent classes that are available are:
470473

471474
.. literalinclude:: incomingrequest/033.php
472475

476+
If an array of indexes is specified, the method returns an associative array and applies
477+
the GET-then-POST lookup order to each index.
478+
473479
If no index is specified, it will return both GET and POST streams combined.
474480
Although GET data will be preferred in case of name conflict.
475481

@@ -534,4 +540,3 @@ The methods provided by the parent classes that are available are:
534540
.. note:: Prior to v4.4.0, this was the safest method to determine the
535541
"current URI", since ``IncomingRequest::$uri`` might not be aware of
536542
the complete App configuration for base URLs.
537-

0 commit comments

Comments
 (0)