Skip to content

Commit

Permalink
make request helper and __get consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed May 9, 2017
1 parent 757be73 commit a6ff272
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,9 @@ function request($key = null, $default = null)
return app('request')->only($key);
}

return data_get(app('request')->all(), $key, $default);
$value = app('request')->__get($key);

return is_null($value) ? value($default) : $value;
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/Illuminate/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,9 @@ public function toArray()
*/
public function offsetExists($offset)
{
return array_key_exists($offset, $this->all());
return array_key_exists(
$offset, $this->all() + $this->route()->parameters()
);
}

/**
Expand All @@ -541,7 +543,7 @@ public function offsetExists($offset)
*/
public function offsetGet($offset)
{
return data_get($this->all(), $offset);
return $this->__get($offset);
}

/**
Expand Down Expand Up @@ -586,8 +588,8 @@ public function __isset($key)
*/
public function __get($key)
{
if ($this->offsetExists($key)) {
return $this->offsetGet($key);
if (array_key_exists($key, $this->all())) {
return data_get($this->all(), $key);
}

return $this->route($key);
Expand Down
7 changes: 4 additions & 3 deletions tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,8 +693,8 @@ public function testMagicMethods()

// Parameter 'empty' is '', then it ISSET and is EMPTY.
$this->assertEquals($request->empty, '');
$this->assertEquals(isset($request->empty), true);
$this->assertEquals(empty($request->empty), true);
$this->assertTrue(isset($request->empty));
$this->assertTrue(empty($request->empty));

// Parameter 'undefined' is undefined/null, then it NOT ISSET and is EMPTY.
$this->assertEquals($request->undefined, null);
Expand All @@ -711,7 +711,8 @@ public function testMagicMethods()
});

// Router parameter 'foo' is 'bar', then it ISSET and is NOT EMPTY.
$this->assertEquals($request->foo, 'bar');
$this->assertEquals('bar', $request->foo);
$this->assertEquals('bar', $request['foo']);
$this->assertEquals(isset($request->foo), true);
$this->assertEquals(empty($request->foo), false);

Expand Down

0 comments on commit a6ff272

Please sign in to comment.