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

Taylor Otwell committed
3
class Input {
4 5

	/**
6 7 8 9 10 11 12
	 * The JSON payload for applications using Backbone.js or similar.
	 *
	 * @var object
	 */
	public static $json;

	/**
Taylor Otwell committed
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
		$input = array_merge(static::get(), static::query(), static::file());
27 28 29 30

		unset($input[Request::spoofer]);

		return $input;
31 32 33
	}

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

	/**
47
	 * Get an item from the input data.
48
	 *
49
	 * This method is used for all request verbs (GET, POST, PUT, and DELETE).
50
	 *
51 52 53 54 55 56 57 58
	 * <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>
	 *
59
	 * @param  string  $key
60
	 * @param  mixed   $default
61
	 * @return mixed
62
	 */
63
	public static function get($key = null, $default = null)
64
	{
Taylor Otwell committed
65 66 67 68 69 70 71 72
		$input = Request::foundation()->request->all();

		if (is_null($key))
		{
			return array_merge($input, static::query());
		}

		$value = array_get($input, $key);
73 74 75 76 77 78 79

		if (is_null($value))
		{
			return array_get(static::query(), $key, $default);
		}

		return $value;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	}

	/**
	 * Get an item from the query string.
	 *
	 * <code>
	 *		// Get the "email" item from the query string
	 *		$email = Input::query('email');
	 *
	 *		// Return a default value if the specified item doesn't exist
	 *		$email = Input::query('name', 'Taylor');
	 * </code>
	 *
	 * @param  string  $key
	 * @param  mixed   $default
	 * @return mixed
	 */
	public static function query($key = null, $default = null)
	{
		return array_get(Request::foundation()->query->all(), $key, $default);
100 101 102
	}

	/**
103 104
	 * Get the JSON payload for the request.
	 *
105
	 * @param  bool    $as_array
106 107
	 * @return object
	 */
108
	public static function json($as_array = false)
109 110 111
	{
		if ( ! is_null(static::$json)) return static::$json;

112
		return static::$json = json_decode(Request::foundation()->getContent(), $as_array);
113 114 115
	}

	/**
116
	 * Get a subset of the items from the input data.
117 118
	 *
	 * <code>
119 120
	 *		// Get only the email from the input data
	 *		$value = Input::only('email');
121
	 *
122 123
	 *		// Get only the username and email from the input data
	 *		$input = Input::only(array('username', 'email'));
124 125
	 * </code>
	 *
126 127
	 * @param  array  $keys
	 * @return array
128
	 */
129
	public static function only($keys)
130
	{
131
 		return array_only(static::get(), $keys);
132 133 134
	}

	/**
135
	 * Get all of the input data except for a specified array of items.
136
	 *
137 138 139 140 141 142 143 144 145 146
	 * <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
147
	 */
148
	public static function except($keys)
149
	{
150
		return array_except(static::get(), $keys);
151 152 153
	}

	/**
154
	 * Determine if the old input data contains an item.
Taylor Otwell committed
155
	 *
156 157
	 * @param  string  $key
	 * @return bool
Taylor Otwell committed
158
	 */
159
	public static function had($key)
Taylor Otwell committed
160
	{
161
		return trim((string) static::old($key)) !== '';
Taylor Otwell committed
162 163 164
	}

	/**
165 166
	 * Get input data from the previous request.
	 *
167 168 169 170 171 172 173 174
	 * <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
175 176
	 * @param  string          $key
	 * @param  mixed           $default
177 178
	 * @return string
	 */
179
	public static function old($key = null, $default = null)
180
	{
181
		return array_get(Session::get(Input::old_input, array()), $key, $default);
182 183 184
	}

	/**
185 186
	 * Get an item from the uploaded file data.
	 *
187 188 189 190 191
	 * <code>
	 *		// Get the array of information for the "picture" upload
	 *		$picture = Input::file('picture');
	 * </code>
	 *
192 193 194
	 * @param  string        $key
	 * @param  mixed         $default
	 * @return UploadedFile
195
	 */
196
	public static function file($key = null, $default = null)
197
	{
198 199 200 201
		return array_get($_FILES, $key, $default);
	}

	/**
Koen Schmeets committed
202 203 204 205 206 207 208
	 * Determine if the uploaded data contains a file.
	 *
	 * @param  string  $key
	 * @return bool
	 */
	public static function has_file($key)
	{
209
		return strlen(static::file("{$key}.tmp_name", "")) > 0;
Koen Schmeets committed
210
	}
211

Koen Schmeets committed
212
	/**
213 214 215 216 217 218 219 220 221 222 223 224
	 * Move an uploaded file to permanent storage.
	 *
	 * This method is simply a convenient wrapper around move_uploaded_file.
	 *
	 * <code>
	 *		// Move the "picture" file to a new permanent location on disk
	 *		Input::upload('picture', 'path/to/photos', 'picture.jpg');
	 * </code>
	 *
	 * @param  string  $key
	 * @param  string  $directory
	 * @param  string  $name
Taylor Otwell committed
225
	 * @return Symfony\Component\HttpFoundation\File\File
226 227 228 229 230 231
	 */
	public static function upload($key, $directory, $name = null)
	{
		if (is_null(static::file($key))) return false;

		return Request::foundation()->files->get($key)->move($directory, $name);
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
	}

	/**
	 * 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());
267 268
	}

269 270 271 272 273 274 275 276
	/**
	 * Merge new input into the current request's input array.
	 *
	 * @param  array  $input
	 * @return void
	 */
	public static function merge(array $input)
	{
Taylor Otwell committed
277 278 279 280 281 282 283 284 285 286 287
		Request::foundation()->request->add($input);
	}

	/**
	 * Replace the input for the current request.
	 *
	 * @param  array  $input
	 * @return void
	 */
	public static function replace(array $input)
	{
288 289 290
		Request::foundation()->request->replace($input);
	}

291 292 293 294 295 296 297 298 299
	/**
	 * Clear the input for the current request.
	 * @return void
	 */
	public static function clear()
	{
		Request::foundation()->request->replace(array());
	}

300
}