2011-08-18 19:56:29 -05:00
|
|
|
<?php namespace Laravel;
|
2011-06-08 23:45:08 -05:00
|
|
|
|
2011-08-30 22:35:32 -05:00
|
|
|
class Input {
|
2011-06-08 23:45:08 -05:00
|
|
|
|
|
|
|
|
/**
|
2011-08-23 21:04:40 -05:00
|
|
|
* The applicable input for the request.
|
2011-06-08 23:45:08 -05:00
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2011-10-29 22:31:50 -05:00
|
|
|
public static $input;
|
2011-09-21 21:46:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The key used to store old input in the session.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
const old_input = 'laravel_old_input';
|
|
|
|
|
|
2011-07-12 20:04:14 -05:00
|
|
|
/**
|
|
|
|
|
* Get all of the input data for the request.
|
|
|
|
|
*
|
2011-09-21 23:32:12 -05:00
|
|
|
* This method returns a merged array containing Input::get() and Input::files().
|
2011-07-12 20:04:14 -05:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2011-10-29 22:31:50 -05:00
|
|
|
public static function all()
|
2011-07-12 20:04:14 -05:00
|
|
|
{
|
2011-10-29 22:31:50 -05:00
|
|
|
return array_merge(static::get(), static::file());
|
2011-07-12 20:04:14 -05:00
|
|
|
}
|
|
|
|
|
|
2011-06-15 12:41:35 -07:00
|
|
|
/**
|
2011-06-17 12:23:26 -05:00
|
|
|
* Determine if the input data contains an item.
|
2011-06-08 23:45:08 -05:00
|
|
|
*
|
2011-09-21 23:32:12 -05:00
|
|
|
* If the item is in the input array, but is an empty string, false will be returned.
|
|
|
|
|
*
|
2011-06-16 22:45:33 -05:00
|
|
|
* @param string $key
|
2011-06-08 23:45:08 -05:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-10-29 22:31:50 -05:00
|
|
|
public static function has($key)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-29 22:31:50 -05:00
|
|
|
return ( ! is_null(static::get($key)) and trim((string) static::get($key)) !== '');
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-06-14 17:27:11 -05:00
|
|
|
* Get an item from the input data.
|
2011-06-08 23:45:08 -05:00
|
|
|
*
|
2011-08-24 22:51:32 -05:00
|
|
|
* This method should be used for all request methods (GET, POST, PUT, and DELETE).
|
|
|
|
|
*
|
2011-09-21 23:32:12 -05:00
|
|
|
* <code>
|
|
|
|
|
* // Get the "email" item from the input array
|
|
|
|
|
* $email = Input::get('email');
|
|
|
|
|
*
|
|
|
|
|
* // Return a default value if the specified item doesn't exist
|
|
|
|
|
* $email = Input::get('name', 'Taylor');
|
|
|
|
|
* </code>
|
|
|
|
|
*
|
2011-06-08 23:45:08 -05:00
|
|
|
* @param string $key
|
2011-06-14 17:27:11 -05:00
|
|
|
* @param mixed $default
|
2011-08-30 22:09:47 -05:00
|
|
|
* @return mixed
|
2011-06-08 23:45:08 -05:00
|
|
|
*/
|
2011-10-29 22:31:50 -05:00
|
|
|
public static function get($key = null, $default = null)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-29 22:31:50 -05:00
|
|
|
return Arr::get(static::$input, $key, $default);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-11-01 22:55:43 -05:00
|
|
|
* Flash the input for the current request to the session.
|
2011-06-15 12:41:35 -07:00
|
|
|
*
|
2011-11-01 22:55:43 -05:00
|
|
|
* @return void
|
2011-06-15 12:41:35 -07:00
|
|
|
*/
|
2011-11-01 22:55:43 -05:00
|
|
|
public static function flash()
|
2011-06-15 12:41:35 -07:00
|
|
|
{
|
2011-11-02 21:27:43 -05:00
|
|
|
if (Config::$items['session']['driver'] !== '')
|
|
|
|
|
{
|
2011-11-11 09:52:30 +00:00
|
|
|
IoC::core('session')->flash(Input::old_input, static::get());
|
2011-11-02 21:27:43 -05:00
|
|
|
}
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
2011-11-11 21:27:30 -06:00
|
|
|
/**
|
|
|
|
|
* Flush the old input from the session.
|
|
|
|
|
*
|
2011-11-15 19:36:48 -06:00
|
|
|
* On a successful form submission, the application may redirect to another form.
|
|
|
|
|
* If this is the case, it may be necessary to flush the old input so that the new
|
|
|
|
|
* form does not have the previous form's data.
|
2011-11-11 21:27:30 -06:00
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function flush()
|
|
|
|
|
{
|
|
|
|
|
if (Config::$items['session']['driver'] !== '')
|
|
|
|
|
{
|
|
|
|
|
IoC::core('session')->flash(Input::old_input, array());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-01 19:41:35 -05:00
|
|
|
/**
|
2011-11-01 22:55:43 -05:00
|
|
|
* Determine if the old input data contains an item.
|
2011-11-01 19:41:35 -05:00
|
|
|
*
|
2011-11-01 22:55:43 -05:00
|
|
|
* @param string $key
|
|
|
|
|
* @return bool
|
2011-11-01 19:41:35 -05:00
|
|
|
*/
|
2011-11-01 22:55:43 -05:00
|
|
|
public static function had($key)
|
2011-11-01 19:41:35 -05:00
|
|
|
{
|
2011-11-01 22:55:43 -05:00
|
|
|
return ( ! is_null(static::old($key)) and trim((string) static::old($key)) !== '');
|
2011-11-01 19:41:35 -05:00
|
|
|
}
|
|
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
/**
|
|
|
|
|
* Get input data from the previous request.
|
|
|
|
|
*
|
2011-09-21 23:32:12 -05:00
|
|
|
* <code>
|
|
|
|
|
* // Get the "email" item from the old input
|
|
|
|
|
* $email = Input::old('email');
|
|
|
|
|
*
|
|
|
|
|
* // Return a default value if the specified item doesn't exist
|
|
|
|
|
* $email = Input::old('name', 'Taylor');
|
|
|
|
|
* </code>
|
|
|
|
|
*
|
2011-08-23 21:04:40 -05:00
|
|
|
* @param string $key
|
|
|
|
|
* @param mixed $default
|
2011-06-08 23:45:08 -05:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-10-29 22:31:50 -05:00
|
|
|
public static function old($key = null, $default = null)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-09-20 23:14:09 -05:00
|
|
|
if (Config::get('session.driver') == '')
|
2011-09-08 17:49:16 -05:00
|
|
|
{
|
2011-11-16 12:55:45 -06:00
|
|
|
throw new \UnexpectedValueException('A session driver must be specified to access old input.');
|
2011-09-08 17:49:16 -05:00
|
|
|
}
|
|
|
|
|
|
2011-11-11 09:52:30 +00:00
|
|
|
$old = IoC::core('session')->get(Input::old_input, array());
|
2011-11-08 20:08:20 -06:00
|
|
|
|
|
|
|
|
return Arr::get($old, $key, $default);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
2011-06-26 08:44:31 -05:00
|
|
|
/**
|
|
|
|
|
* Get an item from the uploaded file data.
|
|
|
|
|
*
|
2011-09-21 23:32:12 -05:00
|
|
|
* <code>
|
|
|
|
|
* // Get the array of information for the "picture" upload
|
|
|
|
|
* $picture = Input::file('picture');
|
|
|
|
|
*
|
|
|
|
|
* // Get a specific element from the file array
|
|
|
|
|
* $size = Input::file('picture.size');
|
|
|
|
|
* </code>
|
|
|
|
|
*
|
2011-06-26 08:44:31 -05:00
|
|
|
* @param string $key
|
|
|
|
|
* @param mixed $default
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2011-10-29 22:31:50 -05:00
|
|
|
public static function file($key = null, $default = null)
|
2011-06-26 08:44:31 -05:00
|
|
|
{
|
2011-10-29 22:31:50 -05:00
|
|
|
return Arr::get($_FILES, $key, $default);
|
2011-06-26 08:44:31 -05:00
|
|
|
}
|
|
|
|
|
|
2011-08-22 22:38:20 -05:00
|
|
|
/**
|
|
|
|
|
* Move an uploaded file to permanent storage.
|
|
|
|
|
*
|
2011-08-24 22:51:32 -05:00
|
|
|
* This method is simply a convenient wrapper around move_uploaded_file.
|
|
|
|
|
*
|
2011-09-21 23:32:12 -05:00
|
|
|
* <code>
|
|
|
|
|
* // Move the "picture" item from the $_FILES array to a permanent location
|
|
|
|
|
* Input::upload('picture', 'path/to/storage/picture.jpg');
|
|
|
|
|
* </code>
|
|
|
|
|
*
|
2011-08-22 22:38:20 -05:00
|
|
|
* @param string $key
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-10-29 22:31:50 -05:00
|
|
|
public static function upload($key, $path)
|
2011-08-22 22:38:20 -05:00
|
|
|
{
|
2011-10-29 22:31:50 -05:00
|
|
|
return array_key_exists($key, $_FILES) ? File::upload($key, $path, $_FILES) : false;
|
2011-08-22 22:38:20 -05:00
|
|
|
}
|
|
|
|
|
|
2011-11-11 09:52:30 +00:00
|
|
|
}
|