input.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php namespace Laravel;
  2. class Input {
  3. /**
  4. * The applicable input for the request.
  5. *
  6. * @var array
  7. */
  8. protected 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. * Set the input for the current request.
  17. *
  18. * @param array $input
  19. * @return void
  20. */
  21. public static function set($input)
  22. {
  23. static::$input = $input;
  24. }
  25. /**
  26. * Get all of the input data for the request.
  27. *
  28. * This method returns a merged array containing Input::get() and Input::files().
  29. *
  30. * @return array
  31. */
  32. public static function all()
  33. {
  34. return array_merge(static::get(), static::file());
  35. }
  36. /**
  37. * Determine if the input data contains an item.
  38. *
  39. * If the item is in the input array, but is an empty string, false will be returned.
  40. *
  41. * @param string $key
  42. * @return bool
  43. */
  44. public static function has($key)
  45. {
  46. return ( ! is_null(static::get($key)) and trim((string) static::get($key)) !== '');
  47. }
  48. /**
  49. * Get an item from the input data.
  50. *
  51. * This method should be used for all request methods (GET, POST, PUT, and DELETE).
  52. *
  53. * <code>
  54. * // Get the "email" item from the input array
  55. * $email = Input::get('email');
  56. *
  57. * // Return a default value if the specified item doesn't exist
  58. * $email = Input::get('name', 'Taylor');
  59. * </code>
  60. *
  61. * @param string $key
  62. * @param mixed $default
  63. * @return mixed
  64. */
  65. public static function get($key = null, $default = null)
  66. {
  67. return Arr::get(static::$input, $key, $default);
  68. }
  69. /**
  70. * Determine if the old input data contains an item.
  71. *
  72. * @param string $key
  73. * @return bool
  74. */
  75. public static function had($key)
  76. {
  77. return ( ! is_null(static::old($key)) and trim((string) static::old($key)) !== '');
  78. }
  79. /**
  80. * Get input data from the previous request.
  81. *
  82. * <code>
  83. * // Get the "email" item from the old input
  84. * $email = Input::old('email');
  85. *
  86. * // Return a default value if the specified item doesn't exist
  87. * $email = Input::old('name', 'Taylor');
  88. * </code>
  89. *
  90. * @param string $key
  91. * @param mixed $default
  92. * @return string
  93. */
  94. public static function old($key = null, $default = null)
  95. {
  96. if (Config::get('session.driver') == '')
  97. {
  98. throw new \Exception('A session driver must be specified in order to access old input.');
  99. }
  100. $driver = IoC::container()->core('session');
  101. return Arr::get($driver->get(Input::old_input, array()), $key, $default);
  102. }
  103. /**
  104. * Get an item from the uploaded file data.
  105. *
  106. * <code>
  107. * // Get the array of information for the "picture" upload
  108. * $picture = Input::file('picture');
  109. *
  110. * // Get a specific element from the file array
  111. * $size = Input::file('picture.size');
  112. * </code>
  113. *
  114. * @param string $key
  115. * @param mixed $default
  116. * @return array
  117. */
  118. public static function file($key = null, $default = null)
  119. {
  120. return Arr::get($_FILES, $key, $default);
  121. }
  122. /**
  123. * Move an uploaded file to permanent storage.
  124. *
  125. * This method is simply a convenient wrapper around move_uploaded_file.
  126. *
  127. * <code>
  128. * // Move the "picture" item from the $_FILES array to a permanent location
  129. * Input::upload('picture', 'path/to/storage/picture.jpg');
  130. * </code>
  131. *
  132. * @param string $key
  133. * @param string $path
  134. * @return bool
  135. */
  136. public static function upload($key, $path)
  137. {
  138. return array_key_exists($key, $_FILES) ? File::upload($key, $path, $_FILES) : false;
  139. }
  140. }