Skip to content

Commit

Permalink
Working on cleaning HTTP code.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 19, 2016
1 parent 3385fdc commit e689b2a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 62 deletions.
25 changes: 2 additions & 23 deletions src/Illuminate/Http/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function withCallback($callback = null)
* Get the json_decoded data from the response.
*
* @param bool $assoc
* @param int $depth
* @param int $depth
* @return mixed
*/
public function getData($assoc = false, $depth = 512)
Expand Down Expand Up @@ -72,31 +72,10 @@ public function setData($data = [])
return $this->update();
}

/**
* Get the JSON encoding options.
*
* @return int
*/
public function getJsonOptions()
{
return $this->getEncodingOptions();
}

/**
* {@inheritdoc}
*/
public function setEncodingOptions($encodingOptions)
{
return $this->setJsonOptions($encodingOptions);
}

/**
* Set the JSON encoding options.
*
* @param int $options
* @return mixed
*/
public function setJsonOptions($options)
public function setEncodingOptions($options)
{
$this->encodingOptions = (int) $options;

Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Http/RedirectResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public function withCookies(array $cookies)
*/
public function withInput(array $input = null)
{
$input = $input ?: $this->request->input();

$this->session->flashInput($this->removeFilesFromInput($input));
$this->session->flashInput($this->removeFilesFromInput(
$input ?: $this->request->input()
));

return $this;
}
Expand Down
48 changes: 21 additions & 27 deletions src/Illuminate/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,7 @@ protected function isEmptyString($key)
{
$value = $this->input($key);

$boolOrArray = is_bool($value) || is_array($value);

return ! $boolOrArray && trim((string) $value) === '';
return ! is_bool($value) && ! is_array($value) && trim((string) $value) === '';
}

/**
Expand Down Expand Up @@ -454,18 +452,6 @@ protected function convertUploadedFiles(array $files)
}, $files);
}

/**
* Retrieve a file from the request.
*
* @param string $key
* @param mixed $default
* @return \Illuminate\Http\UploadedFile|array|null
*/
public function file($key = null, $default = null)
{
return data_get($this->allFiles(), $key, $default);
}

/**
* Determine if the uploaded data contains a file.
*
Expand All @@ -487,6 +473,18 @@ public function hasFile($key)
return false;
}

/**
* Retrieve a file from the request.
*
* @param string $key
* @param mixed $default
* @return \Illuminate\Http\UploadedFile|array|null
*/
public function file($key = null, $default = null)
{
return data_get($this->allFiles(), $key, $default);
}

/**
* Check that the given file is a valid file instance.
*
Expand Down Expand Up @@ -548,15 +546,11 @@ public function old($key = null, $default = null)
/**
* Flash the input for the current request to the session.
*
* @param string $filter
* @param array $keys
* @return void
*/
public function flash($filter = null, $keys = [])
public function flash()
{
$flash = (! is_null($filter)) ? $this->$filter($keys) : $this->input();

$this->session()->flashInput($flash);
$this->session()->flashInput($this->input());
}

/**
Expand All @@ -567,9 +561,9 @@ public function flash($filter = null, $keys = [])
*/
public function flashOnly($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();

return $this->flash('only', $keys);
$this->session()->flashInput(
$this->only(is_array($keys) ? $keys : func_get_args())
);
}

/**
Expand All @@ -580,9 +574,9 @@ public function flashOnly($keys)
*/
public function flashExcept($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();

return $this->flash('except', $keys);
$this->session()->flashInput(
$this->except(is_array($keys) ? $keys : func_get_args())
);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/Http/HttpJsonResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public function testSetAndRetrieveData()
public function testSetAndRetrieveOptions()
{
$response = new Illuminate\Http\JsonResponse(['foo' => 'bar']);
$response->setJsonOptions(JSON_PRETTY_PRINT);
$this->assertSame(JSON_PRETTY_PRINT, $response->getJsonOptions());
$response->setEncodingOptions(JSON_PRETTY_PRINT);
$this->assertSame(JSON_PRETTY_PRINT, $response->getEncodingOptions());
}

public function testSetAndRetrieveDefaultOptions()
{
$response = new Illuminate\Http\JsonResponse(['foo' => 'bar']);
$this->assertSame(0, $response->getJsonOptions());
$this->assertSame(0, $response->getEncodingOptions());
}

public function testSetAndRetrieveStatusCode()
Expand Down
16 changes: 10 additions & 6 deletions tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -745,15 +745,19 @@ public function testHttpRequestFlashCallsSessionFlashInputWithInputData()

public function testHttpRequestFlashOnlyCallsFlashWithProperParameters()
{
$request = m::mock('Illuminate\Http\Request[flash]');
$request->shouldReceive('flash')->once()->with('only', ['key1', 'key2']);
$request->flashOnly(['key1', 'key2']);
$session = m::mock('Illuminate\Session\Store');
$session->shouldReceive('flashInput')->once()->with(['name' => 'Taylor']);
$request = Request::create('/', 'GET', ['name' => 'Taylor', 'email' => 'foo']);
$request->setSession($session);
$request->flashOnly(['name']);
}

public function testHttpRequestFlashExceptCallsFlashWithProperParameters()
{
$request = m::mock('Illuminate\Http\Request[flash]');
$request->shouldReceive('flash')->once()->with('except', ['key1', 'key2']);
$request->flashExcept(['key1', 'key2']);
$session = m::mock('Illuminate\Session\Store');
$session->shouldReceive('flashInput')->once()->with(['name' => 'Taylor']);
$request = Request::create('/', 'GET', ['name' => 'Taylor', 'email' => 'foo']);
$request->setSession($session);
$request->flashExcept(['email']);
}
}

0 comments on commit e689b2a

Please sign in to comment.