input.php 6.5 KB

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