input.php 3.5 KB

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