Skip to content

Commit

Permalink
Feature: Input::getBodyJson() (#304)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
g105b authored Feb 13, 2025
1 parent dea8ab7 commit 4254bf3
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 4 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

"require": {
"php": ">=8.1",
"phpgt/http": "^v1.1"
"phpgt/http": "^1.1",
"phpgt/json": "^1.2"
},

"require-dev": {
"phpstan/phpstan": "^v1.10",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.5",
"phpmd/phpmd": "^2.13",
"squizlabs/php_codesniffer": "^3.7"
Expand Down
162 changes: 161 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion src/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

use ArrayAccess;
use Countable;
use Gt\Json\JsonDecodeException;
use Gt\Json\JsonObject;
use Gt\Json\JsonObjectBuilder;
use Iterator;
use Psr\Http\Message\StreamInterface;
use Gt\Input\Trigger\Trigger;
Expand All @@ -19,6 +22,7 @@
/**
* @implements ArrayAccess<string, ?string>
* @implements Iterator<string, ?string>
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Input implements ArrayAccess, Countable, Iterator {
use InputValueGetter;
Expand All @@ -45,7 +49,7 @@ public function __construct(
array $get = [],
array $post = [],
array $files = [],
string $bodyPath = "php://input"
string $bodyPath = "php://input",
) {
$this->bodyStream = new BodyStream($bodyPath);

Expand Down Expand Up @@ -193,6 +197,17 @@ public function getAll(?string $method = null):InputData {
}
}

public function getBodyJson():?JsonObject {
$jsonBuilder = new JsonObjectBuilder();

try {
return $jsonBuilder->fromJsonString($this->bodyStream->getContents());
}
catch(JsonDecodeException) {
return null;
}
}

/**
* Return a "do" Trigger, matching when a request variable is present with the
* provided $match value.
Expand Down
46 changes: 46 additions & 0 deletions test/phpunit/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Gt\Input\MissingInputParameterException;
use Gt\Input\Test\Helper\Helper;
use Gt\Input\Trigger\Trigger;
use Gt\Json\JsonObject;
use Gt\Json\JsonPrimitive\JsonArrayPrimitive;
use Gt\Json\JsonPrimitive\JsonStringPrimitive;
use PHPUnit\Framework\TestCase;

class InputTest extends TestCase {
Expand Down Expand Up @@ -664,6 +667,49 @@ public function testGetMultipleFile():void {
}
}

public function testGetBodyJson_notJson():void {
$get = [];
$post = ["this" => "is not JSON!"];
$sut = new Input($get, $post);
$json = $sut->getBodyJson();
self::assertNull($json);
}

public function testGetBodyJson_string():void {
$inputPath = tempnam(sys_get_temp_dir(), "phpgt-input-test-");
file_put_contents($inputPath, "\"Hello, PHP.Gt!\"");
$sut = new Input(bodyPath: $inputPath);
$json = $sut->getBodyJson();
self::assertInstanceOf(JsonStringPrimitive::class, $json);
self::assertSame("Hello, PHP.Gt!", $json->getPrimitiveValue());
}

public function testGetBodyJson_arrayWithObject():void {
$inputPath = tempnam(sys_get_temp_dir(), "phpgt-input-test-");
file_put_contents($inputPath, "[1, 2, 3, {\"name\": \"Cody\"}]");
$sut = new Input(bodyPath: $inputPath);
$json = $sut->getBodyJson();
self::assertInstanceOf(JsonArrayPrimitive::class, $json);
$array = $json->getPrimitiveValue();
self::assertSame(1, $array[0]);
self::assertSame(2, $array[1]);
self::assertSame(3, $array[2]);
/** @var JsonObject $thirdArrayElement */
$thirdArrayElement = $array[3];
self::assertInstanceOf(JsonObject::class, $thirdArrayElement);
self::assertSame("Cody", $thirdArrayElement->getString("name"));
}

public function testGetBodyJson_withQueryString():void {
$inputPath = tempnam(sys_get_temp_dir(), "phpgt-input-test-");
file_put_contents($inputPath, "\"Hello, PHP.Gt!\"");
$sut = new Input(["id" => 123], bodyPath: $inputPath);
$json = $sut->getBodyJson();
self::assertInstanceOf(JsonStringPrimitive::class, $json);
self::assertSame("Hello, PHP.Gt!", $json->getPrimitiveValue());
self::assertSame(123, $sut->getInt("id"));
}

public static function dataRandomGetPost():array {
$data = [];

Expand Down

0 comments on commit 4254bf3

Please sign in to comment.