input.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php namespace Laravel;
  2. class Input {
  3. /**
  4. * The key used to store old input in the session.
  5. *
  6. * @var string
  7. */
  8. const old_input = 'laravel_old_input';
  9. /**
  10. * Get all of the input data for the request, including files.
  11. *
  12. * @return array
  13. */
  14. public static function all()
  15. {
  16. $input = array_merge_recursive(static::get(), static::query(), static::file());
  17. unset($input[Request::spoofer]);
  18. return $input;
  19. }
  20. /**
  21. * Determine if the input data contains an item.
  22. *
  23. * If the input item is an empty string, false will be returned.
  24. *
  25. * @param string $key
  26. * @return bool
  27. */
  28. public static function has($key)
  29. {
  30. return trim((string) static::get($key)) !== '';
  31. }
  32. /**
  33. * Get an item from the input data.
  34. *
  35. * This method is used for all request verbs (GET, POST, PUT, and DELETE).
  36. *
  37. * <code>
  38. * // Get the "email" item from the input array
  39. * $email = Input::get('email');
  40. *
  41. * // Return a default value if the specified item doesn't exist
  42. * $email = Input::get('name', 'Taylor');
  43. * </code>
  44. *
  45. * @param string $key
  46. * @param mixed $default
  47. * @return mixed
  48. */
  49. public static function get($key = null, $default = null)
  50. {
  51. $value = array_get(Request::foundation()->request->all(), $key);
  52. if (is_null($value))
  53. {
  54. return array_get(static::query(), $key, $default);
  55. }
  56. return $value;
  57. }
  58. /**
  59. * Get an item from the query string.
  60. *
  61. * <code>
  62. * // Get the "email" item from the query string
  63. * $email = Input::query('email');
  64. *
  65. * // Return a default value if the specified item doesn't exist
  66. * $email = Input::query('name', 'Taylor');
  67. * </code>
  68. *
  69. * @param string $key
  70. * @param mixed $default
  71. * @return mixed
  72. */
  73. public static function query($key = null, $default = null)
  74. {
  75. return array_get(Request::foundation()->query->all(), $key, $default);
  76. }
  77. /**
  78. * Get a subset of the items from the input data.
  79. *
  80. * <code>
  81. * // Get only the email from the input data
  82. * $value = Input::only('email');
  83. *
  84. * // Get only the username and email from the input data
  85. * $input = Input::only(array('username', 'email'));
  86. * </code>
  87. *
  88. * @param array $keys
  89. * @return array
  90. */
  91. public static function only($keys)
  92. {
  93. return array_intersect_key(static::get(), array_flip((array) $keys));
  94. }
  95. /**
  96. * Get all of the input data except for a specified array of items.
  97. *
  98. * <code>
  99. * // Get all of the input data except for username
  100. * $input = Input::except('username');
  101. *
  102. * // Get all of the input data except for username and email
  103. * $input = Input::except(array('username', 'email'));
  104. * </code>
  105. *
  106. * @param array $keys
  107. * @return array
  108. */
  109. public static function except($keys)
  110. {
  111. return array_diff_key(static::get(), array_flip((array) $keys));
  112. }
  113. /**
  114. * Determine if the old input data contains an item.
  115. *
  116. * @param string $key
  117. * @return bool
  118. */
  119. public static function had($key)
  120. {
  121. return trim((string) static::old($key)) !== '';
  122. }
  123. /**
  124. * Get input data from the previous request.
  125. *
  126. * <code>
  127. * // Get the "email" item from the old input
  128. * $email = Input::old('email');
  129. *
  130. * // Return a default value if the specified item doesn't exist
  131. * $email = Input::old('name', 'Taylor');
  132. * </code>
  133. *
  134. * @param string $key
  135. * @param mixed $default
  136. * @return string
  137. */
  138. public static function old($key = null, $default = null)
  139. {
  140. return array_get(Session::get(Input::old_input, array()), $key, $default);
  141. }
  142. /**
  143. * Get an item from the uploaded file data.
  144. *
  145. * <code>
  146. * // Get the array of information for the "picture" upload
  147. * $picture = Input::file('picture');
  148. * </code>
  149. *
  150. * @param string $key
  151. * @param mixed $default
  152. * @return UploadedFile
  153. */
  154. public static function file($key = null, $default = null)
  155. {
  156. return array_get($_FILES, $key, $default);
  157. }
  158. /**
  159. * Move an uploaded file to permanent storage.
  160. *
  161. * This method is simply a convenient wrapper around move_uploaded_file.
  162. *
  163. * <code>
  164. * // Move the "picture" file to a new permanent location on disk
  165. * Input::upload('picture', 'path/to/photos', 'picture.jpg');
  166. * </code>
  167. *
  168. * @param string $key
  169. * @param string $directory
  170. * @param string $name
  171. * @return bool
  172. */
  173. public static function upload($key, $directory, $name = null)
  174. {
  175. if (is_null(static::file($key))) return false;
  176. return Request::foundation()->files->get($key)->move($directory, $name);
  177. }
  178. /**
  179. * Flash the input for the current request to the session.
  180. *
  181. * <code>
  182. * // Flash all of the input to the session
  183. * Input::flash();
  184. *
  185. * // Flash only a few input items to the session
  186. * Input::flash('only', array('name', 'email'));
  187. *
  188. * // Flash all but a few input items to the session
  189. * Input::flash('except', array('password', 'social_number'));
  190. * </code>
  191. *
  192. * @param string $filter
  193. * @param array $keys
  194. * @return void
  195. */
  196. public static function flash($filter = null, $keys = array())
  197. {
  198. $flash = ( ! is_null($filter)) ? static::$filter($keys) : static::get();
  199. Session::flash(Input::old_input, $flash);
  200. }
  201. /**
  202. * Flush all of the old input from the session.
  203. *
  204. * @return void
  205. */
  206. public static function flush()
  207. {
  208. Session::flash(Input::old_input, array());
  209. }
  210. }