input.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 ( ! is_null(static::get($key)) and 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. * @return void
  63. */
  64. public static function flash()
  65. {
  66. if (Config::$items['session']['driver'] !== '')
  67. {
  68. IoC::core('session')->flash(Input::old_input, static::get());
  69. }
  70. }
  71. /**
  72. * Flush the old input from the session.
  73. *
  74. * On a successful form submission, the application may redirect to another form.
  75. * If this is the case, it may be necessary to flush the old input so that the new
  76. * form does not have the previous form's data.
  77. *
  78. * @return void
  79. */
  80. public static function flush()
  81. {
  82. if (Config::$items['session']['driver'] !== '')
  83. {
  84. IoC::core('session')->flash(Input::old_input, array());
  85. }
  86. }
  87. /**
  88. * Determine if the old input data contains an item.
  89. *
  90. * @param string $key
  91. * @return bool
  92. */
  93. public static function had($key)
  94. {
  95. return ( ! is_null(static::old($key)) and trim((string) static::old($key)) !== '');
  96. }
  97. /**
  98. * Get input data from the previous request.
  99. *
  100. * <code>
  101. * // Get the "email" item from the old input
  102. * $email = Input::old('email');
  103. *
  104. * // Return a default value if the specified item doesn't exist
  105. * $email = Input::old('name', 'Taylor');
  106. * </code>
  107. *
  108. * @param string $key
  109. * @param mixed $default
  110. * @return string
  111. */
  112. public static function old($key = null, $default = null)
  113. {
  114. if (Config::get('session.driver') == '')
  115. {
  116. throw new \UnexpectedValueException('A session driver must be specified to access old input.');
  117. }
  118. $old = IoC::core('session')->get(Input::old_input, array());
  119. return Arr::get($old, $key, $default);
  120. }
  121. /**
  122. * Get an item from the uploaded file data.
  123. *
  124. * <code>
  125. * // Get the array of information for the "picture" upload
  126. * $picture = Input::file('picture');
  127. *
  128. * // Get a specific element from the file array
  129. * $size = Input::file('picture.size');
  130. * </code>
  131. *
  132. * @param string $key
  133. * @param mixed $default
  134. * @return array
  135. */
  136. public static function file($key = null, $default = null)
  137. {
  138. return Arr::get($_FILES, $key, $default);
  139. }
  140. /**
  141. * Move an uploaded file to permanent storage.
  142. *
  143. * This method is simply a convenient wrapper around move_uploaded_file.
  144. *
  145. * <code>
  146. * // Move the "picture" item from the $_FILES array to a permanent location
  147. * Input::upload('picture', 'path/to/storage/picture.jpg');
  148. * </code>
  149. *
  150. * @param string $key
  151. * @param string $path
  152. * @return bool
  153. */
  154. public static function upload($key, $path)
  155. {
  156. return array_key_exists($key, $_FILES) ? File::upload($key, $path, $_FILES) : false;
  157. }
  158. }