input.php 6.2 KB

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