Skip to content

Commit 4254bf3

Browse files
author
Greg Bowler
authored
Feature: Input::getBodyJson() (#304)
* ci: remove old artifacts * ci: run all php versions * build: require phpgt/json for #302 * build: initial non-json test for #302 * feature: getBodyJson * tweak: suppress coupling between objects warning
1 parent dea8ab7 commit 4254bf3

File tree

4 files changed

+226
-4
lines changed

4 files changed

+226
-4
lines changed

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66
"require": {
77
"php": ">=8.1",
8-
"phpgt/http": "^v1.1"
8+
"phpgt/http": "^1.1",
9+
"phpgt/json": "^1.2"
910
},
1011

1112
"require-dev": {
12-
"phpstan/phpstan": "^v1.10",
13+
"phpstan/phpstan": "^1.10",
1314
"phpunit/phpunit": "^10.5",
1415
"phpmd/phpmd": "^2.13",
1516
"squizlabs/php_codesniffer": "^3.7"

composer.lock

+161-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Input.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
use ArrayAccess;
55
use Countable;
6+
use Gt\Json\JsonDecodeException;
7+
use Gt\Json\JsonObject;
8+
use Gt\Json\JsonObjectBuilder;
69
use Iterator;
710
use Psr\Http\Message\StreamInterface;
811
use Gt\Input\Trigger\Trigger;
@@ -19,6 +22,7 @@
1922
/**
2023
* @implements ArrayAccess<string, ?string>
2124
* @implements Iterator<string, ?string>
25+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2226
*/
2327
class Input implements ArrayAccess, Countable, Iterator {
2428
use InputValueGetter;
@@ -45,7 +49,7 @@ public function __construct(
4549
array $get = [],
4650
array $post = [],
4751
array $files = [],
48-
string $bodyPath = "php://input"
52+
string $bodyPath = "php://input",
4953
) {
5054
$this->bodyStream = new BodyStream($bodyPath);
5155

@@ -193,6 +197,17 @@ public function getAll(?string $method = null):InputData {
193197
}
194198
}
195199

200+
public function getBodyJson():?JsonObject {
201+
$jsonBuilder = new JsonObjectBuilder();
202+
203+
try {
204+
return $jsonBuilder->fromJsonString($this->bodyStream->getContents());
205+
}
206+
catch(JsonDecodeException) {
207+
return null;
208+
}
209+
}
210+
196211
/**
197212
* Return a "do" Trigger, matching when a request variable is present with the
198213
* provided $match value.

test/phpunit/InputTest.php

+46
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 {
@@ -664,6 +667,49 @@ public function testGetMultipleFile():void {
664667
}
665668
}
666669

670+
public function testGetBodyJson_notJson():void {
671+
$get = [];
672+
$post = ["this" => "is not JSON!"];
673+
$sut = new Input($get, $post);
674+
$json = $sut->getBodyJson();
675+
self::assertNull($json);
676+
}
677+
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+
667713
public static function dataRandomGetPost():array {
668714
$data = [];
669715

0 commit comments

Comments
 (0)