input.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. if (is_array(static::old($key))) return true;
  145. return trim((string) static::old($key)) !== '';
  146. }
  147. /**
  148. * Get input data from the previous request.
  149. *
  150. * <code>
  151. * // Get the "email" item from the old input
  152. * $email = Input::old('email');
  153. *
  154. * // Return a default value if the specified item doesn't exist
  155. * $email = Input::old('name', 'Taylor');
  156. * </code>
  157. *
  158. * @param string $key
  159. * @param mixed $default
  160. * @return string
  161. */
  162. public static function old($key = null, $default = null)
  163. {
  164. return array_get(Session::get(Input::old_input, array()), $key, $default);
  165. }
  166. /**
  167. * Get an item from the uploaded file data.
  168. *
  169. * <code>
  170. * // Get the array of information for the "picture" upload
  171. * $picture = Input::file('picture');
  172. * </code>
  173. *
  174. * @param string $key
  175. * @param mixed $default
  176. * @return UploadedFile
  177. */
  178. public static function file($key = null, $default = null)
  179. {
  180. return array_get($_FILES, $key, $default);
  181. }
  182. /**
  183. * Determine if the uploaded data contains a file.
  184. *
  185. * @param string $key
  186. * @return bool
  187. */
  188. public static function has_file($key)
  189. {
  190. return strlen(static::file("{$key}.tmp_name", "")) > 0;
  191. }
  192. /**
  193. * Move an uploaded file to permanent storage.
  194. *
  195. * This method is simply a convenient wrapper around move_uploaded_file.
  196. *
  197. * <code>
  198. * // Move the "picture" file to a new permanent location on disk
  199. * Input::upload('picture', 'path/to/photos', 'picture.jpg');
  200. * </code>
  201. *
  202. * @param string $key
  203. * @param string $directory
  204. * @param string $name
  205. * @return Symfony\Component\HttpFoundation\File\File
  206. */
  207. public static function upload($key, $directory, $name = null)
  208. {
  209. if (is_null(static::file($key))) return false;
  210. return Request::foundation()->files->get($key)->move($directory, $name);
  211. }
  212. /**
  213. * Flash the input for the current request to the session.
  214. *
  215. * <code>
  216. * // Flash all of the input to the session
  217. * Input::flash();
  218. *
  219. * // Flash only a few input items to the session
  220. * Input::flash('only', array('name', 'email'));
  221. *
  222. * // Flash all but a few input items to the session
  223. * Input::flash('except', array('password', 'social_number'));
  224. * </code>
  225. *
  226. * @param string $filter
  227. * @param array $keys
  228. * @return void
  229. */
  230. public static function flash($filter = null, $keys = array())
  231. {
  232. $flash = ( ! is_null($filter)) ? static::$filter($keys) : static::get();
  233. Session::flash(Input::old_input, $flash);
  234. }
  235. /**
  236. * Flush all of the old input from the session.
  237. *
  238. * @return void
  239. */
  240. public static function flush()
  241. {
  242. Session::flash(Input::old_input, array());
  243. }
  244. /**
  245. * Merge new input into the current request's input array.
  246. *
  247. * @param array $input
  248. * @return void
  249. */
  250. public static function merge(array $input)
  251. {
  252. Request::foundation()->request->add($input);
  253. }
  254. /**
  255. * Replace the input for the current request.
  256. *
  257. * @param array $input
  258. * @return void
  259. */
  260. public static function replace(array $input)
  261. {
  262. Request::foundation()->request->replace($input);
  263. }
  264. /**
  265. * Clear the input for the current request.
  266. * @return void
  267. */
  268. public static function clear()
  269. {
  270. Request::foundation()->request->replace(array());
  271. }
  272. }