input.php 2.35 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 14 15 16 17 18 19 20 21 22 23 24
	 * Get all of the input data for the request.
	 *
	 * This method returns a merged array containing Input::get and Input::file.
	 *
	 * @return array
	 */
	public static function all()
	{
		return array_merge(static::get(), static::file());
	}

	/**
25
	 * Determine if the input data contains an item.
26
	 *
Taylor Otwell committed
27
	 * @param  string  $key
28 29
	 * @return bool
	 */
Taylor Otwell committed
30
	public static function has($key)
31
	{
32
		return ( ! is_null(static::get($key)) and trim((string) static::get($key)) !== '');
33 34 35
	}

	/**
36
	 * Get an item from the input data.
37 38
	 *
	 * @param  string  $key
39 40
	 * @param  mixed   $default
	 * @return string
41
	 */
42
	public static function get($key = null, $default = null)
43
	{
44 45 46 47 48
		if (is_null(static::$input))
		{
			static::hydrate();
		}

49
		return Arr::get(static::$input, $key, $default);
50 51 52
	}

	/**
53
	 * Determine if the old input data contains an item.
54
	 *
Taylor Otwell committed
55
	 * @param  string  $key
56 57
	 * @return bool
	 */
Taylor Otwell committed
58
	public static function had($key)
59
	{
60
		return ( ! is_null(static::old($key)) and trim((string) static::old($key)) !== '');
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	}

	/**
	 * 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.");
		}

77
		return Arr::get(Session::get('laravel_old_input', array()), $key, $default);
78 79 80
	}

	/**
81 82 83 84 85 86
	 * Get an item from the uploaded file data.
	 *
	 * @param  string  $key
	 * @param  mixed   $default
	 * @return array
	 */
87
	public static function file($key = null, $default = null)
88
	{
89 90 91 92
		if (strpos($key, '.') !== false)
		{
			list($file, $key) = explode('.', $key);

Taylor Otwell committed
93
			return Arr::get($_FILES[$file], $key, $default);
94 95
		}

96
		return Arr::get($_FILES, $key, $default);
97 98 99
	}

	/**
100 101 102 103 104 105
	 * Hydrate the input data for the request.
	 *
	 * @return void
	 */
	public static function hydrate()
	{
106
		switch (Request::method())
107
		{
108 109 110
			case 'GET':
				static::$input =& $_GET;
				break;
111

112 113 114
			case 'POST':
				static::$input =& $_POST;
				break;
115

116 117
			case 'PUT':
			case 'DELETE':
118
				if (Request::spoofed())
119 120 121 122 123 124 125
				{
					static::$input =& $_POST;
				}
				else
				{
					parse_str(file_get_contents('php://input'), static::$input);
				}
126 127 128 129
		}
	}

}