input.php 2.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php namespace System;

class Input {

	/**
	 * The input data for the request.
	 *
	 * @var array
	 */
	public static $input;

	/**
13
	 * Determine if the input data contains an item.
14
	 *
Taylor Otwell committed
15
	 * @param  string  $key
16 17
	 * @return bool
	 */
Taylor Otwell committed
18
	public static function has($key)
19
	{
20
		return ( ! is_null(static::get($key)) and trim((string) static::get($key)) !== '');
21 22 23
	}

	/**
24
	 * Get an item from the input data.
25 26
	 *
	 * @param  string  $key
27 28
	 * @param  mixed   $default
	 * @return string
29
	 */
30
	public static function get($key = null, $default = null)
31
	{
32 33 34 35 36
		if (is_null(static::$input))
		{
			static::hydrate();
		}

37
		return (array_key_exists($key, static::$input)) ? static::$input[$key] : $default;
38 39 40
	}

	/**
41
	 * Determine if the old input data contains an item.
42
	 *
Taylor Otwell committed
43
	 * @param  string  $key
44 45
	 * @return bool
	 */
Taylor Otwell committed
46
	public static function had($key)
47
	{
48
		return ( ! is_null(static::old($key)) and trim((string) static::old($key)) !== '');
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	}

	/**
	 * Get input data from the previous request.
	 *
	 * @param  string  $key
	 * @param  mixed   $default
	 * @return string
	 */
	public static function old($key = null, $default = null)
	{
		if (Config::get('session.driver') == '')
		{
			throw new \Exception("Sessions must be enabled to retrieve old input data.");
		}

65
		return (array_key_exists($key, $old = Session::get('laravel_old_input', array()))) ? $old[$key] : $default;
66 67 68
	}

	/**
69 70 71 72 73 74
	 * Get an item from the uploaded file data.
	 *
	 * @param  string  $key
	 * @param  mixed   $default
	 * @return array
	 */
75
	public static function file($key = null, $default = null)
76
	{
77 78 79 80 81 82 83 84
		if (strpos($key, '.') !== false)
		{
			list($file, $key) = explode('.', $key);

			return (isset($_FILES[$file][$key])) ? $_FILES[$file][$key] : $default;
		}

		return (array_key_exists($key, $_FILES)) ? $_FILES[$key] : $default;
85 86 87
	}

	/**
88 89 90 91 92 93
	 * Hydrate the input data for the request.
	 *
	 * @return void
	 */
	public static function hydrate()
	{
94
		switch (Request::method())
95
		{
96 97 98
			case 'GET':
				static::$input =& $_GET;
				break;
99

100 101 102
			case 'POST':
				static::$input =& $_POST;
				break;
103

104 105
			case 'PUT':
			case 'DELETE':
106

107 108
				// The request method can be spoofed by specifying a "REQUEST_METHOD" in the $_POST array.
				// If the method is being spoofed, the $_POST array will be considered the input.
109

110
				if (isset($_POST['REQUEST_METHOD']) and in_array($_POST['REQUEST_METHOD'], array('PUT', 'DELETE')))
111 112 113 114 115 116 117
				{
					static::$input =& $_POST;
				}
				else
				{
					parse_str(file_get_contents('php://input'), static::$input);
				}
118 119 120 121
		}
	}

}