input.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php namespace Laravel;
  2. class Input {
  3. /**
  4. * The applicable input for the request.
  5. *
  6. * @var array
  7. */
  8. public static $input;
  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.
  17. *
  18. * This method returns a merged array containing Input::get() and Input::files().
  19. *
  20. * @return array
  21. */
  22. public static function all()
  23. {
  24. return array_merge(static::get(), static::file());
  25. }
  26. /**
  27. * Determine if the input data contains an item.
  28. *
  29. * If the item is in the input array, but 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 should be used for all request methods (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. return Arr::get(static::$input, $key, $default);
  58. }
  59. /**
  60. * Flash the input for the current request to the session.
  61. *
  62. * The input data to be flashed may be controlled by using a filter and an array
  63. * of included or excluded input data. This provides a convenient way of keeping
  64. * sensitive information like passwords out of the session.
  65. *
  66. * <code>
  67. * // Flash all of the input data to the session
  68. * Input::flash();
  69. *
  70. * // Flash only a few input items to the session
  71. * Input::flash('only', array('name', 'email'));
  72. *
  73. * // Flash all but a few input items to the session
  74. * Input::flash('except', array('password'));
  75. * </code>
  76. *
  77. * @return void
  78. */
  79. public static function flash($filter = null, $items = array())
  80. {
  81. $flash = static::get();
  82. // Since the items flashed to the session can be filtered, we will iterate
  83. // all of the input data and either remove or include the input item based
  84. // on the specified filter and array of items to be flashed.
  85. if ($filter == 'only')
  86. {
  87. $flash = array_intersect_key($flash, array_flip($items));
  88. }
  89. elseif ($filter == 'except')
  90. {
  91. $flash = array_diff_key($flash, array_flip($items));
  92. }
  93. IoC::core('session')->flash(Input::old_input, $flash);
  94. }
  95. /**
  96. * Flush the old input from the session.
  97. *
  98. * @return void
  99. */
  100. public static function flush()
  101. {
  102. IoC::core('session')->flash(Input::old_input, array());
  103. }
  104. /**
  105. * Determine if the old input data contains an item.
  106. *
  107. * @param string $key
  108. * @return bool
  109. */
  110. public static function had($key)
  111. {
  112. return trim((string) static::old($key)) !== '';
  113. }
  114. /**
  115. * Get input data from the previous request.
  116. *
  117. * <code>
  118. * // Get the "email" item from the old input
  119. * $email = Input::old('email');
  120. *
  121. * // Return a default value if the specified item doesn't exist
  122. * $email = Input::old('name', 'Taylor');
  123. * </code>
  124. *
  125. * @param string $key
  126. * @param mixed $default
  127. * @return string
  128. */
  129. public static function old($key = null, $default = null)
  130. {
  131. $old = IoC::core('session')->get(Input::old_input, array());
  132. return Arr::get($old, $key, $default);
  133. }
  134. /**
  135. * Get an item from the uploaded file data.
  136. *
  137. * <code>
  138. * // Get the array of information for the "picture" upload
  139. * $picture = Input::file('picture');
  140. *
  141. * // Get a specific element from the file array
  142. * $size = Input::file('picture.size');
  143. * </code>
  144. *
  145. * @param string $key
  146. * @param mixed $default
  147. * @return array
  148. */
  149. public static function file($key = null, $default = null)
  150. {
  151. return Arr::get($_FILES, $key, $default);
  152. }
  153. /**
  154. * Move an uploaded file to permanent storage.
  155. *
  156. * This method is simply a convenient wrapper around move_uploaded_file.
  157. *
  158. * <code>
  159. * // Move the "picture" item from the $_FILES array to a permanent location
  160. * Input::upload('picture', 'path/to/storage/picture.jpg');
  161. * </code>
  162. *
  163. * @param string $key
  164. * @param string $path
  165. * @return bool
  166. */
  167. public static function upload($key, $path)
  168. {
  169. return File::upload($key, $path);
  170. }
  171. }