input.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. * @param bool $as_array
  92. * @return object
  93. */
  94. public static function json($as_array = false)
  95. {
  96. if ( ! is_null(static::$json)) return static::$json;
  97. return static::$json = json_decode(Request::foundation()->getContent(), $as_array);
  98. }
  99. /**
  100. * Get a subset of the items from the input data.
  101. *
  102. * <code>
  103. * // Get only the email from the input data
  104. * $value = Input::only('email');
  105. *
  106. * // Get only the username and email from the input data
  107. * $input = Input::only(array('username', 'email'));
  108. * </code>
  109. *
  110. * @param array $keys
  111. * @return array
  112. */
  113. public static function only($keys)
  114. {
  115. return array_only(static::get(), $keys);
  116. }
  117. /**
  118. * Get all of the input data except for a specified array of items.
  119. *
  120. * <code>
  121. * // Get all of the input data except for username
  122. * $input = Input::except('username');
  123. *
  124. * // Get all of the input data except for username and email
  125. * $input = Input::except(array('username', 'email'));
  126. * </code>
  127. *
  128. * @param array $keys
  129. * @return array
  130. */
  131. public static function except($keys)
  132. {
  133. return array_except(static::get(), $keys);
  134. }
  135. /**
  136. * Determine if the old input data contains an item.
  137. *
  138. * @param string $key
  139. * @return bool
  140. */
  141. public static function had($key)
  142. {
  143. return trim((string) static::old($key)) !== '';
  144. }
  145. /**
  146. * Get input data from the previous request.
  147. *
  148. * <code>
  149. * // Get the "email" item from the old input
  150. * $email = Input::old('email');
  151. *
  152. * // Return a default value if the specified item doesn't exist
  153. * $email = Input::old('name', 'Taylor');
  154. * </code>
  155. *
  156. * @param string $key
  157. * @param mixed $default
  158. * @return string
  159. */
  160. public static function old($key = null, $default = null)
  161. {
  162. return array_get(Session::get(Input::old_input, array()), $key, $default);
  163. }
  164. /**
  165. * Get an item from the uploaded file data.
  166. *
  167. * <code>
  168. * // Get the array of information for the "picture" upload
  169. * $picture = Input::file('picture');
  170. * </code>
  171. *
  172. * @param string $key
  173. * @param mixed $default
  174. * @return UploadedFile
  175. */
  176. public static function file($key = null, $default = null)
  177. {
  178. return array_get($_FILES, $key, $default);
  179. }
  180. /**
  181. * Determine if the uploaded data contains a file.
  182. *
  183. * @param string $key
  184. * @return bool
  185. */
  186. public static function has_file($key)
  187. {
  188. return strlen(static::file("{$key}.tmp_name", "")) > 0;
  189. }
  190. /**
  191. * Move an uploaded file to permanent storage.
  192. *
  193. * This method is simply a convenient wrapper around move_uploaded_file.
  194. *
  195. * <code>
  196. * // Move the "picture" file to a new permanent location on disk
  197. * Input::upload('picture', 'path/to/photos', 'picture.jpg');
  198. * </code>
  199. *
  200. * @param string $key
  201. * @param string $directory
  202. * @param string $name
  203. * @return bool
  204. */
  205. public static function upload($key, $directory, $name = null)
  206. {
  207. if (is_null(static::file($key))) return false;
  208. return Request::foundation()->files->get($key)->move($directory, $name);
  209. }
  210. /**
  211. * Flash the input for the current request to the session.
  212. *
  213. * <code>
  214. * // Flash all of the input to the session
  215. * Input::flash();
  216. *
  217. * // Flash only a few input items to the session
  218. * Input::flash('only', array('name', 'email'));
  219. *
  220. * // Flash all but a few input items to the session
  221. * Input::flash('except', array('password', 'social_number'));
  222. * </code>
  223. *
  224. * @param string $filter
  225. * @param array $keys
  226. * @return void
  227. */
  228. public static function flash($filter = null, $keys = array())
  229. {
  230. $flash = ( ! is_null($filter)) ? static::$filter($keys) : static::get();
  231. Session::flash(Input::old_input, $flash);
  232. }
  233. /**
  234. * Flush all of the old input from the session.
  235. *
  236. * @return void
  237. */
  238. public static function flush()
  239. {
  240. Session::flash(Input::old_input, array());
  241. }
  242. /**
  243. * Merge new input into the current request's input array.
  244. *
  245. * @param array $input
  246. * @return void
  247. */
  248. public static function merge(array $input)
  249. {
  250. Request::foundation()->request->add($input);
  251. }
  252. /**
  253. * Replace the input for the current request.
  254. *
  255. * @param array $input
  256. * @return void
  257. */
  258. public static function replace(array $input)
  259. {
  260. Request::foundation()->request->replace($input);
  261. }
  262. /**
  263. * Clear the input for the current request.
  264. * @return void
  265. */
  266. public static function clear()
  267. {
  268. Request::foundation()->request->replace(array());
  269. }
  270. }