From c7809b24f398b675c5abd326d8c7425ee1dcf9f2 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Tue, 11 Sep 2018 22:08:59 -0300 Subject: [PATCH] Return empty array on JSON decoding failure, closes #106 --- src/Http/Request.php | 8 +++++++- tests/Unit/Http/RequestTest.php | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Http/Request.php b/src/Http/Request.php index 3f04b23e..c66a626e 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -45,7 +45,13 @@ function params(string $input = 'php://input') : array */ function json(string $input = 'php://input') : array { - return json_decode(raw($input), true); + $params = json_decode(raw($input), true); + + if (is_null($params)) { + return []; + } + + return $params; } /** diff --git a/tests/Unit/Http/RequestTest.php b/tests/Unit/Http/RequestTest.php index 53396e29..a3f06aeb 100644 --- a/tests/Unit/Http/RequestTest.php +++ b/tests/Unit/Http/RequestTest.php @@ -45,6 +45,9 @@ public function testJson() $this->assertContains('bar', $params); $this->assertCount(1, $params); $this->assertArraySubset(['foo' => 'bar'], $params); + + $params = Request\json(); + $this->assertEmpty($params); } public function testHeaders()