Skip to content

Commit ca6a7d9

Browse files
author
Carlos Garcia
committed
Add getContent method to Request class; refactor json method to use getContent
1 parent bc59624 commit ca6a7d9

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

Core/Request.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ public function getBool(string $key, ?bool $default = null): ?bool
179179
return $this->request->getBool($key, $default);
180180
}
181181

182+
public function getContent(): string
183+
{
184+
return $this->rawInput ?? file_get_contents('php://input');
185+
}
186+
182187
/**
183188
* @deprecated use request->getDate() or query->getDate() instead
184189
*/
@@ -349,6 +354,18 @@ public function isMethod(string $method): bool
349354
return $this->method() === $method;
350355
}
351356

357+
public function json(?string $key = null, $default = null)
358+
{
359+
$input = $this->getContent();
360+
$data = json_decode($input, true);
361+
362+
if ($key === null) {
363+
return $data;
364+
}
365+
366+
return $data[$key] ?? $default;
367+
}
368+
352369
public function method(): string
353370
{
354371
return $_SERVER['REQUEST_METHOD'];
@@ -419,23 +436,6 @@ public function isSecure(): bool
419436
return $this->protocol() === 'https';
420437
}
421438

422-
public function json(?string $key = null, $default = null)
423-
{
424-
$input = $this->rawInput();
425-
$data = json_decode($input, true);
426-
427-
if ($key === null) {
428-
return $data;
429-
}
430-
431-
return $data[$key] ?? $default;
432-
}
433-
434-
public function rawInput(): string
435-
{
436-
return $this->rawInput ?? file_get_contents('php://input');
437-
}
438-
439439
public function url(?int $position = null): string
440440
{
441441
// si contiene '?', lo quitamos y lo que venga después

Test/Core/RequestTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -786,20 +786,20 @@ public function testJsonInvalid(): void
786786
$this->assertNull($request->json('any'));
787787
}
788788

789-
public function testRawInput(): void
789+
public function testGetContent(): void
790790
{
791791
// Test con contenido inyectado
792792
$rawData = '{"test":"value","xml":"<tag>content</tag>"}';
793793
$request = $this->createRequest(['input' => $rawData]);
794-
$this->assertEquals($rawData, $request->rawInput());
794+
$this->assertEquals($rawData, $request->getContent());
795795

796796
// Test con input vacío
797797
$request = $this->createRequest(['input' => '']);
798-
$this->assertEquals('', $request->rawInput());
798+
$this->assertEquals('', $request->getContent());
799799

800800
// Test sin input inyectado (usaría php://input)
801801
$request = $this->createRequest();
802-
$this->assertIsString($request->rawInput());
802+
$this->assertIsString($request->getContent());
803803
}
804804

805805
public function testConstants(): void

0 commit comments

Comments
 (0)