input.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. Session::flash(Input::old_input, static::get());
  67. }
  68. /**
  69. * Determine if the old input data contains an item.
  70. *
  71. * @param string $key
  72. * @return bool
  73. */
  74. public static function had($key)
  75. {
  76. return ( ! is_null(static::old($key)) and trim((string) static::old($key)) !== '');
  77. }
  78. /**
  79. * Get input data from the previous request.
  80. *
  81. * <code>
  82. * // Get the "email" item from the old input
  83. * $email = Input::old('email');
  84. *
  85. * // Return a default value if the specified item doesn't exist
  86. * $email = Input::old('name', 'Taylor');
  87. * </code>
  88. *
  89. * @param string $key
  90. * @param mixed $default
  91. * @return string
  92. */
  93. public static function old($key = null, $default = null)
  94. {
  95. if (Config::get('session.driver') == '')
  96. {
  97. throw new \Exception('A session driver must be specified in order to access old input.');
  98. }
  99. return Arr::get(Session::get(Input::old_input, array()), $key, $default);
  100. }
  101. /**
  102. * Get an item from the uploaded file data.
  103. *
  104. * <code>
  105. * // Get the array of information for the "picture" upload
  106. * $picture = Input::file('picture');
  107. *
  108. * // Get a specific element from the file array
  109. * $size = Input::file('picture.size');
  110. * </code>
  111. *
  112. * @param string $key
  113. * @param mixed $default
  114. * @return array
  115. */
  116. public static function file($key = null, $default = null)
  117. {
  118. return Arr::get($_FILES, $key, $default);
  119. }
  120. /**
  121. * Move an uploaded file to permanent storage.
  122. *
  123. * This method is simply a convenient wrapper around move_uploaded_file.
  124. *
  125. * <code>
  126. * // Move the "picture" item from the $_FILES array to a permanent location
  127. * Input::upload('picture', 'path/to/storage/picture.jpg');
  128. * </code>
  129. *
  130. * @param string $key
  131. * @param string $path
  132. * @return bool
  133. */
  134. public static function upload($key, $path)
  135. {
  136. return array_key_exists($key, $_FILES) ? File::upload($key, $path, $_FILES) : false;
  137. }
  138. }