Skip to content

Commit cfbd28b

Browse files
author
Greg Bowler
committed
feature: getBodyJson
1 parent ad1c9fe commit cfbd28b

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

src/Input.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
use ArrayAccess;
55
use Countable;
6+
use Gt\Json\JsonDecodeException;
67
use Gt\Json\JsonObject;
8+
use Gt\Json\JsonObjectBuilder;
79
use Iterator;
810
use Psr\Http\Message\StreamInterface;
911
use Gt\Input\Trigger\Trigger;
@@ -46,7 +48,7 @@ public function __construct(
4648
array $get = [],
4749
array $post = [],
4850
array $files = [],
49-
string $bodyPath = "php://input"
51+
string $bodyPath = "php://input",
5052
) {
5153
$this->bodyStream = new BodyStream($bodyPath);
5254

@@ -194,6 +196,17 @@ public function getAll(?string $method = null):InputData {
194196
}
195197
}
196198

199+
public function getBodyJson():?JsonObject {
200+
$jsonBuilder = new JsonObjectBuilder();
201+
202+
try {
203+
return $jsonBuilder->fromJsonString($this->bodyStream->getContents());
204+
}
205+
catch(JsonDecodeException) {
206+
return null;
207+
}
208+
}
209+
197210
/**
198211
* Return a "do" Trigger, matching when a request variable is present with the
199212
* provided $match value.
@@ -250,9 +263,4 @@ protected function newTrigger(string $functionName, string...$args):Trigger {
250263
$trigger = new Trigger($this);
251264
return $trigger->$functionName(...$args);
252265
}
253-
254-
public function getBodyJson():?JsonObject {
255-
return null;
256-
}
257-
258266
}

test/phpunit/InputTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Gt\Input\MissingInputParameterException;
1212
use Gt\Input\Test\Helper\Helper;
1313
use Gt\Input\Trigger\Trigger;
14+
use Gt\Json\JsonObject;
15+
use Gt\Json\JsonPrimitive\JsonArrayPrimitive;
16+
use Gt\Json\JsonPrimitive\JsonStringPrimitive;
1417
use PHPUnit\Framework\TestCase;
1518

1619
class InputTest extends TestCase {
@@ -672,6 +675,41 @@ public function testGetBodyJson_notJson():void {
672675
self::assertNull($json);
673676
}
674677

678+
public function testGetBodyJson_string():void {
679+
$inputPath = tempnam(sys_get_temp_dir(), "phpgt-input-test-");
680+
file_put_contents($inputPath, "\"Hello, PHP.Gt!\"");
681+
$sut = new Input(bodyPath: $inputPath);
682+
$json = $sut->getBodyJson();
683+
self::assertInstanceOf(JsonStringPrimitive::class, $json);
684+
self::assertSame("Hello, PHP.Gt!", $json->getPrimitiveValue());
685+
}
686+
687+
public function testGetBodyJson_arrayWithObject():void {
688+
$inputPath = tempnam(sys_get_temp_dir(), "phpgt-input-test-");
689+
file_put_contents($inputPath, "[1, 2, 3, {\"name\": \"Cody\"}]");
690+
$sut = new Input(bodyPath: $inputPath);
691+
$json = $sut->getBodyJson();
692+
self::assertInstanceOf(JsonArrayPrimitive::class, $json);
693+
$array = $json->getPrimitiveValue();
694+
self::assertSame(1, $array[0]);
695+
self::assertSame(2, $array[1]);
696+
self::assertSame(3, $array[2]);
697+
/** @var JsonObject $thirdArrayElement */
698+
$thirdArrayElement = $array[3];
699+
self::assertInstanceOf(JsonObject::class, $thirdArrayElement);
700+
self::assertSame("Cody", $thirdArrayElement->getString("name"));
701+
}
702+
703+
public function testGetBodyJson_withQueryString():void {
704+
$inputPath = tempnam(sys_get_temp_dir(), "phpgt-input-test-");
705+
file_put_contents($inputPath, "\"Hello, PHP.Gt!\"");
706+
$sut = new Input(["id" => 123], bodyPath: $inputPath);
707+
$json = $sut->getBodyJson();
708+
self::assertInstanceOf(JsonStringPrimitive::class, $json);
709+
self::assertSame("Hello, PHP.Gt!", $json->getPrimitiveValue());
710+
self::assertSame(123, $sut->getInt("id"));
711+
}
712+
675713
public static function dataRandomGetPost():array {
676714
$data = [];
677715

0 commit comments

Comments
 (0)