Skip to content

Commit 130864d

Browse files
committed
Merge pull request #393 from philsturgeon/patch-1
Make Input::json() more logical
2 parents fbb9d8c + 3584cf2 commit 130864d

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/Illuminate/Http/Request.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,15 +384,22 @@ public function replace(array $input)
384384
/**
385385
* Get the JSON payload for the request.
386386
*
387-
* @return object
387+
* @param string $key
388+
* @param mixed $default
389+
* @return mixed
388390
*/
389-
public function json()
391+
public function json($key = null, $default = null)
390392
{
391-
$arguments = func_get_args();
393+
$mime = $this->retrieveItem('server', 'CONTENT_TYPE', null);
392394

393-
array_unshift($arguments, $this->getContent());
395+
if (strpos($mime, '/json') === false)
396+
{
397+
return $default;
398+
}
399+
400+
$json = json_decode($this->getContent(), true);
394401

395-
return call_user_func_array('json_decode', $arguments);
402+
return array_get($json, $key, $default);
396403
}
397404

398405
/**

tests/Http/HttpRequestTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,12 @@ public function testHeaderMethod()
202202

203203
public function testJSONMethod()
204204
{
205-
$request = Request::create('/', 'GET', array(), array(), array(), array(), json_encode(array('taylor' => 'name')));
206-
$json = $request->json();
207-
$this->assertEquals('name', $json->taylor);
205+
$payload = array('name' => 'taylor');
206+
$request = Request::create('/', 'GET', array(), array(), array(), array('CONTENT_TYPE' => 'application/json'), json_encode($payload));
207+
$name = $request->json('name');
208+
$this->assertEquals('taylor', $name);
209+
$data = $request->json();
210+
$this->assertEquals($payload, $data);
208211
}
209212

210213

0 commit comments

Comments
 (0)