input.php 3.2 KB

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