input.php 3.5 KB

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