input.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. $input = Request::foundation()->request->all();
  52. if (is_null($key))
  53. {
  54. return array_merge($input, static::query());
  55. }
  56. $value = array_get($input, $key);
  57. if (is_null($value))
  58. {
  59. return array_get(static::query(), $key, $default);
  60. }
  61. return $value;
  62. }
  63. /**
  64. * Get an item from the query string.
  65. *
  66. * <code>
  67. * // Get the "email" item from the query string
  68. * $email = Input::query('email');
  69. *
  70. * // Return a default value if the specified item doesn't exist
  71. * $email = Input::query('name', 'Taylor');
  72. * </code>
  73. *
  74. * @param string $key
  75. * @param mixed $default
  76. * @return mixed
  77. */
  78. public static function query($key = null, $default = null)
  79. {
  80. return array_get(Request::foundation()->query->all(), $key, $default);
  81. }
  82. /**
  83. * Get a subset of the items from the input data.
  84. *
  85. * <code>
  86. * // Get only the email from the input data
  87. * $value = Input::only('email');
  88. *
  89. * // Get only the username and email from the input data
  90. * $input = Input::only(array('username', 'email'));
  91. * </code>
  92. *
  93. * @param array $keys
  94. * @return array
  95. */
  96. public static function only($keys)
  97. {
  98. return array_intersect_key(static::get(), array_flip((array) $keys));
  99. }
  100. /**
  101. * Get all of the input data except for a specified array of items.
  102. *
  103. * <code>
  104. * // Get all of the input data except for username
  105. * $input = Input::except('username');
  106. *
  107. * // Get all of the input data except for username and email
  108. * $input = Input::except(array('username', 'email'));
  109. * </code>
  110. *
  111. * @param array $keys
  112. * @return array
  113. */
  114. public static function except($keys)
  115. {
  116. return array_diff_key(static::get(), array_flip((array) $keys));
  117. }
  118. /**
  119. * Determine if the old input data contains an item.
  120. *
  121. * @param string $key
  122. * @return bool
  123. */
  124. public static function had($key)
  125. {
  126. return trim((string) static::old($key)) !== '';
  127. }
  128. /**
  129. * Get input data from the previous request.
  130. *
  131. * <code>
  132. * // Get the "email" item from the old input
  133. * $email = Input::old('email');
  134. *
  135. * // Return a default value if the specified item doesn't exist
  136. * $email = Input::old('name', 'Taylor');
  137. * </code>
  138. *
  139. * @param string $key
  140. * @param mixed $default
  141. * @return string
  142. */
  143. public static function old($key = null, $default = null)
  144. {
  145. return array_get(Session::get(Input::old_input, array()), $key, $default);
  146. }
  147. /**
  148. * Get an item from the uploaded file data.
  149. *
  150. * <code>
  151. * // Get the array of information for the "picture" upload
  152. * $picture = Input::file('picture');
  153. * </code>
  154. *
  155. * @param string $key
  156. * @param mixed $default
  157. * @return UploadedFile
  158. */
  159. public static function file($key = null, $default = null)
  160. {
  161. return array_get($_FILES, $key, $default);
  162. }
  163. /**
  164. * Move an uploaded file to permanent storage.
  165. *
  166. * This method is simply a convenient wrapper around move_uploaded_file.
  167. *
  168. * <code>
  169. * // Move the "picture" file to a new permanent location on disk
  170. * Input::upload('picture', 'path/to/photos', 'picture.jpg');
  171. * </code>
  172. *
  173. * @param string $key
  174. * @param string $directory
  175. * @param string $name
  176. * @return bool
  177. */
  178. public static function upload($key, $directory, $name = null)
  179. {
  180. if (is_null(static::file($key))) return false;
  181. return Request::foundation()->files->get($key)->move($directory, $name);
  182. }
  183. /**
  184. * Flash the input for the current request to the session.
  185. *
  186. * <code>
  187. * // Flash all of the input to the session
  188. * Input::flash();
  189. *
  190. * // Flash only a few input items to the session
  191. * Input::flash('only', array('name', 'email'));
  192. *
  193. * // Flash all but a few input items to the session
  194. * Input::flash('except', array('password', 'social_number'));
  195. * </code>
  196. *
  197. * @param string $filter
  198. * @param array $keys
  199. * @return void
  200. */
  201. public static function flash($filter = null, $keys = array())
  202. {
  203. $flash = ( ! is_null($filter)) ? static::$filter($keys) : static::get();
  204. Session::flash(Input::old_input, $flash);
  205. }
  206. /**
  207. * Flush all of the old input from the session.
  208. *
  209. * @return void
  210. */
  211. public static function flush()
  212. {
  213. Session::flash(Input::old_input, array());
  214. }
  215. /**
  216. * Merge new input into the current request's input array.
  217. *
  218. * @param array $input
  219. * @return void
  220. */
  221. public static function merge(array $input)
  222. {
  223. Request::foundation()->request->add($input);
  224. }
  225. /**
  226. * Replace the input for the current request.
  227. *
  228. * @param array $input
  229. * @return void
  230. */
  231. public static function replace(array $input)
  232. {
  233. Request::foundation()->request->replace($input);
  234. }
  235. }