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-05 18:32:48 -05:00
|
|
|
protected 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-10-05 18:32:48 -05:00
|
|
|
* Set the input for the current request.
|
2011-09-21 21:46:16 -05:00
|
|
|
*
|
|
|
|
|
* @param array $input
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
public static function set($input)
|
2011-09-21 21:46:16 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
static::$input = $input;
|
2011-09-21 21:46:16 -05:00
|
|
|
}
|
2011-06-08 23:45:08 -05:00
|
|
|
|
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-05 18:32:48 -05:00
|
|
|
public static function all()
|
2011-07-12 20:04:14 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -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-05 18:32:48 -05:00
|
|
|
public static function has($key)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -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-05 18:32:48 -05:00
|
|
|
public static function get($key = null, $default = null)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
return Arr::get(static::$input, $key, $default);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-06-17 12:23:26 -05:00
|
|
|
* Determine if the old input data contains an item.
|
2011-06-15 12:41:35 -07:00
|
|
|
*
|
2011-06-16 22:45:33 -05:00
|
|
|
* @param string $key
|
2011-06-15 12:41:35 -07:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
public static function had($key)
|
2011-06-15 12:41:35 -07:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
return ( ! is_null(static::old($key)) and trim((string) static::old($key)) !== '');
|
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-05 18:32:48 -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
|
|
|
{
|
|
|
|
|
throw new \Exception('A session driver must be specified in order to access old input.');
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-10 21:34:15 -05:00
|
|
|
return Arr::get(Session\Manager::$payload->get(Input::old_input, array()), $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-05 18:32:48 -05:00
|
|
|
public static function file($key = null, $default = null)
|
2011-06-26 08:44:31 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -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-05 18:32:48 -05:00
|
|
|
public static function upload($key, $path)
|
2011-08-22 22:38:20 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
return array_key_exists($key, $_FILES) ? File::upload($key, $path, $_FILES) : false;
|
2011-08-22 22:38:20 -05:00
|
|
|
}
|
|
|
|
|
|
2011-09-21 21:46:16 -05:00
|
|
|
}
|