Skip to content

Commit 5b90042

Browse files
committed
Add Request::tryUpgradeFromGetToPost method
1 parent d585f63 commit 5b90042

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/Request.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,17 @@ public function setJson(mixed $data, ?int $flags = null, int $depth = 512) : sta
314314
$data = \json_encode($data, $flags | \JSON_THROW_ON_ERROR, $depth);
315315
$this->setContentType('application/json');
316316
$this->setBody($data);
317+
$this->tryUpgradeFromGetToPost();
318+
return $this;
319+
}
320+
321+
/**
322+
* If the HTTP method is GET, it will be upgraded to POST.
323+
*
324+
* @return static
325+
*/
326+
protected function tryUpgradeFromGetToPost() : static
327+
{
317328
if ($this->isMethod(Method::GET)) {
318329
$this->setMethod(Method::POST);
319330
}
@@ -366,9 +377,7 @@ public function getFiles() : array
366377
*/
367378
public function setFiles(array $files) : static
368379
{
369-
if ($this->isMethod(Method::GET)) {
370-
$this->setMethod(Method::POST);
371-
}
380+
$this->tryUpgradeFromGetToPost();
372381
if ($files) {
373382
$this->setContentType('multipart/form-data');
374383
}

tests/RequestTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public function testFiles() : void
123123
self::assertSame([], $this->request->getFiles());
124124
self::assertNull($this->request->getHeader('content-type'));
125125
$this->request->setFiles([]);
126+
self::assertSame('GET', $this->request->getMethod());
126127
self::assertNull($this->request->getHeader('content-type'));
127128
$this->request->setFiles(['upload' => __FILE__]);
128129
self::assertSame(

0 commit comments

Comments
 (0)