input.php 4.63 KB
Newer Older
Taylor Otwell committed
1
<?php namespace Laravel;
2

Taylor Otwell committed
3
class Input {
4 5

	/**
Taylor Otwell committed
6
	 * The applicable input for the request.
7 8 9
	 *
	 * @var array
	 */
10
	public static $input;
Taylor Otwell committed
11 12 13 14 15 16 17 18 19

	/**
	 * The key used to store old input in the session.
	 *
	 * @var string
	 */
	const old_input = 'laravel_old_input';

	/**
20
	 * Get all of the input data for the request, including files.
21 22 23
	 *
	 * @return array
	 */
24
	public static function all()
25
	{
26
		return array_merge(static::get(), static::file());
27 28 29
	}

	/**
30
	 * Determine if the input data contains an item.
31
	 *
32
	 * If the input item is an empty string, false will be returned.
33
	 *
Taylor Otwell committed
34
	 * @param  string  $key
35 36
	 * @return bool
	 */
37
	public static function has($key)
38
	{
39
		return trim((string) static::get($key)) !== '';
40 41 42
	}

	/**
43
	 * Get an item from the input data.
44
	 *
45
	 * This method is used for all request verbs (GET, POST, PUT, and DELETE).
46
	 *
47 48 49 50 51 52 53 54
	 * <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>
	 *
55
	 * @param  string  $key
56
	 * @param  mixed   $default
57
	 * @return mixed
58
	 */
59
	public static function get($key = null, $default = null)
60
	{
61
		return array_get(static::$input, $key, $default);
62 63 64
	}

	/**
65
	 * Get a subset of the items from the input data.
66 67
	 *
	 * <code>
68 69
	 *		// Get only the email from the input data
	 *		$value = Input::only('email');
70
	 *
71 72
	 *		// Get only the username and email from the input data
	 *		$input = Input::only(array('username', 'email'));
73 74
	 * </code>
	 *
75 76
	 * @param  array  $keys
	 * @return array
77
	 */
78
	public static function only($keys)
79
	{
80
 		return array_intersect_key(static::get(), array_flip((array) $keys));
81 82 83
	}

	/**
84
	 * Get all of the input data except for a specified array of items.
85
	 *
86 87 88 89 90 91 92 93 94 95
	 * <code>
	 *		// Get all of the input data except for username
	 *		$input = Input::except('username');
	 *
	 *		// Get all of the input data except for username and email
	 *		$input = Input::except(array('username', 'email'));
	 * </code>
	 *
	 * @param  array  $keys
	 * @return array
96
	 */
97
	public static function except($keys)
98
	{
99
		return array_diff_key(static::get(), array_flip($keys));
100 101 102
	}

	/**
103
	 * Determine if the old input data contains an item.
Taylor Otwell committed
104
	 *
105 106
	 * @param  string  $key
	 * @return bool
Taylor Otwell committed
107
	 */
108
	public static function had($key)
Taylor Otwell committed
109
	{
110
		return trim((string) static::old($key)) !== '';
Taylor Otwell committed
111 112 113
	}

	/**
114 115
	 * Get input data from the previous request.
	 *
116 117 118 119 120 121 122 123
	 * <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>
	 *
Taylor Otwell committed
124 125
	 * @param  string          $key
	 * @param  mixed           $default
126 127
	 * @return string
	 */
128
	public static function old($key = null, $default = null)
129
	{
130
		return array_get(Session::get(Input::old_input, array()), $key, $default);
131 132 133
	}

	/**
134 135
	 * Get an item from the uploaded file data.
	 *
136 137 138 139
	 * <code>
	 *		// Get the array of information for the "picture" upload
	 *		$picture = Input::file('picture');
	 *
140
	 *		// Get a specific element from within the file's data array
141 142 143
	 *		$size = Input::file('picture.size');
	 * </code>
	 *
144 145 146 147
	 * @param  string  $key
	 * @param  mixed   $default
	 * @return array
	 */
148
	public static function file($key = null, $default = null)
149
	{
150
		return array_get($_FILES, $key, $default);
151 152 153
	}

	/**
154 155
	 * Move an uploaded file to permanent storage.
	 *
156 157
	 * This method is simply a convenient wrapper around move_uploaded_file.
	 *
158
	 * <code>
159 160
	 *		// Move the "picture" file to a permanent location on disk
	 *		Input::upload('picture', 'path/to/photos/picture.jpg');
161 162
	 * </code>
	 *
163 164 165 166
	 * @param  string  $key
	 * @param  string  $path
	 * @return bool
	 */
167
	public static function upload($key, $path)
168
	{
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
		if (is_null(static::file($key))) return false;

		return move_uploaded_file(static::file("{$key}.tmp_name"), $path);
	}

	/**
	 * Flash the input for the current request to the session.
	 *
	 * <code>
	 *		// Flash all of the input to the session
	 *		Input::flash();
	 *
	 *		// Flash only a few input items to the session
	 *		Input::flash('only', array('name', 'email'));
	 *
	 *		// Flash all but a few input items to the session
	 *		Input::flash('except', array('password', 'social_number'));
	 * </code>
	 *
	 * @param  string  $filter
	 * @param  array   $keys
	 * @return void
	 */
	public static function flash($filter = null, $keys = array())
	{
		$flash = ( ! is_null($filter)) ? static::$filter($keys) : static::get();

		Session::flash(Input::old_input, $flash);
	}

	/**
	 * Flush all of the old input from the session.
	 *
	 * @return void
	 */
	public static function flush()
	{
		Session::flash(Input::old_input, array());
207 208
	}

209
}