1: <?php namespace Laravel;
2:
3: class Input {
4:
5: /**
6: * The JSON payload for applications using Backbone.js or similar.
7: *
8: * @var object
9: */
10: public static $json;
11:
12: /**
13: * The key used to store old input in the session.
14: *
15: * @var string
16: */
17: const old_input = 'laravel_old_input';
18:
19: /**
20: * Get all of the input data for the request, including files.
21: *
22: * @return array
23: */
24: public static function all()
25: {
26: $input = array_merge(static::get(), static::query(), static::file());
27:
28: unset($input[Request::spoofer]);
29:
30: 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: *
38: * @param string $key
39: * @return bool
40: */
41: public static function has($key)
42: {
43: if (is_array(static::get($key))) return true;
44:
45: return trim((string) static::get($key)) !== '';
46: }
47:
48: /**
49: * Get an item from the input data.
50: *
51: * This method is used for all request verbs (GET, POST, PUT, and DELETE).
52: *
53: * <code>
54: * // Get the "email" item from the input array
55: * $email = Input::get('email');
56: *
57: * // Return a default value if the specified item doesn't exist
58: * $email = Input::get('name', 'Taylor');
59: * </code>
60: *
61: * @param string $key
62: * @param mixed $default
63: * @return mixed
64: */
65: public static function get($key = null, $default = null)
66: {
67: $input = Request::foundation()->request->all();
68:
69: if (is_null($key))
70: {
71: return array_merge($input, static::query());
72: }
73:
74: $value = array_get($input, $key);
75:
76: if (is_null($value))
77: {
78: return array_get(static::query(), $key, $default);
79: }
80:
81: return $value;
82: }
83:
84: /**
85: * Get an item from the query string.
86: *
87: * <code>
88: * // Get the "email" item from the query string
89: * $email = Input::query('email');
90: *
91: * // Return a default value if the specified item doesn't exist
92: * $email = Input::query('name', 'Taylor');
93: * </code>
94: *
95: * @param string $key
96: * @param mixed $default
97: * @return mixed
98: */
99: public static function query($key = null, $default = null)
100: {
101: return array_get(Request::foundation()->query->all(), $key, $default);
102: }
103:
104: /**
105: * Get the JSON payload for the request.
106: *
107: * @param bool $as_array
108: * @return object
109: */
110: public static function json($as_array = false)
111: {
112: if ( ! is_null(static::$json)) return static::$json;
113:
114: return static::$json = json_decode(Request::foundation()->getContent(), $as_array);
115: }
116:
117: /**
118: * Get a subset of the items from the input data.
119: *
120: * <code>
121: * // Get only the email from the input data
122: * $value = Input::only('email');
123: *
124: * // Get only the username and email from the input data
125: * $input = Input::only(array('username', 'email'));
126: * </code>
127: *
128: * @param array $keys
129: * @return array
130: */
131: public static function only($keys)
132: {
133: return array_only(static::get(), $keys);
134: }
135:
136: /**
137: * Get all of the input data except for a specified array of items.
138: *
139: * <code>
140: * // Get all of the input data except for username
141: * $input = Input::except('username');
142: *
143: * // Get all of the input data except for username and email
144: * $input = Input::except(array('username', 'email'));
145: * </code>
146: *
147: * @param array $keys
148: * @return array
149: */
150: public static function except($keys)
151: {
152: return array_except(static::get(), $keys);
153: }
154:
155: /**
156: * Determine if the old input data contains an item.
157: *
158: * @param string $key
159: * @return bool
160: */
161: public static function had($key)
162: {
163: if (is_array(static::old($key))) return true;
164:
165: return trim((string) static::old($key)) !== '';
166: }
167:
168: /**
169: * Get input data from the previous request.
170: *
171: * <code>
172: * // Get the "email" item from the old input
173: * $email = Input::old('email');
174: *
175: * // Return a default value if the specified item doesn't exist
176: * $email = Input::old('name', 'Taylor');
177: * </code>
178: *
179: * @param string $key
180: * @param mixed $default
181: * @return string
182: */
183: public static function old($key = null, $default = null)
184: {
185: return array_get(Session::get(Input::old_input, array()), $key, $default);
186: }
187:
188: /**
189: * Get an item from the uploaded file data.
190: *
191: * <code>
192: * // Get the array of information for the "picture" upload
193: * $picture = Input::file('picture');
194: * </code>
195: *
196: * @param string $key
197: * @param mixed $default
198: * @return UploadedFile
199: */
200: public static function file($key = null, $default = null)
201: {
202: return array_get($_FILES, $key, $default);
203: }
204:
205: /**
206: * Determine if the uploaded data contains a file.
207: *
208: * @param string $key
209: * @return bool
210: */
211: public static function has_file($key)
212: {
213: return strlen(static::file("{$key}.tmp_name", "")) > 0;
214: }
215:
216: /**
217: * Move an uploaded file to permanent storage.
218: *
219: * This method is simply a convenient wrapper around move_uploaded_file.
220: *
221: * <code>
222: * // Move the "picture" file to a new permanent location on disk
223: * Input::upload('picture', 'path/to/photos', 'picture.jpg');
224: * </code>
225: *
226: * @param string $key
227: * @param string $directory
228: * @param string $name
229: * @return Symfony\Component\HttpFoundation\File\File
230: */
231: public static function upload($key, $directory, $name = null)
232: {
233: if (is_null(static::file($key))) return false;
234:
235: return Request::foundation()->files->get($key)->move($directory, $name);
236: }
237:
238: /**
239: * Flash the input for the current request to the session.
240: *
241: * <code>
242: * // Flash all of the input to the session
243: * Input::flash();
244: *
245: * // Flash only a few input items to the session
246: * Input::flash('only', array('name', 'email'));
247: *
248: * // Flash all but a few input items to the session
249: * Input::flash('except', array('password', 'social_number'));
250: * </code>
251: *
252: * @param string $filter
253: * @param array $keys
254: * @return void
255: */
256: public static function flash($filter = null, $keys = array())
257: {
258: $flash = ( ! is_null($filter)) ? static::$filter($keys) : static::get();
259:
260: Session::flash(Input::old_input, $flash);
261: }
262:
263: /**
264: * Flush all of the old input from the session.
265: *
266: * @return void
267: */
268: public static function flush()
269: {
270: Session::flash(Input::old_input, array());
271: }
272:
273: /**
274: * Merge new input into the current request's input array.
275: *
276: * @param array $input
277: * @return void
278: */
279: public static function merge(array $input)
280: {
281: Request::foundation()->request->add($input);
282: }
283:
284: /**
285: * Replace the input for the current request.
286: *
287: * @param array $input
288: * @return void
289: */
290: public static function replace(array $input)
291: {
292: Request::foundation()->request->replace($input);
293: }
294:
295: /**
296: * Clear the input for the current request.
297: * @return void
298: */
299: public static function clear()
300: {
301: Request::foundation()->request->replace(array());
302: }
303:
304: }