Skip to content
This repository has been archived by the owner on Feb 13, 2022. It is now read-only.

Commit

Permalink
Return empty array on JSON decoding failure, closes #106
Browse files Browse the repository at this point in the history
  • Loading branch information
leocavalcante committed Sep 12, 2018
1 parent 6947722 commit c7809b2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/Unit/Http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c7809b2

Please sign in to comment.