input.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 request.
  11. *
  12. * @var array
  13. */
  14. protected $files;
  15. /**
  16. * The file manager instance.
  17. *
  18. * @var File
  19. */
  20. protected $file;
  21. /**
  22. * The cookie engine instance.
  23. *
  24. * @var Cookie
  25. */
  26. public $cookies;
  27. /**
  28. * Create a new Input manager instance.
  29. *
  30. * @param Cookie $cookies
  31. * @param array $input
  32. * @param array $files
  33. * @return void
  34. */
  35. public function __construct(File $file, Cookie $cookies, $input, $files)
  36. {
  37. $this->file = $file;
  38. $this->input = $input;
  39. $this->files = $files;
  40. $this->cookies = $cookies;
  41. }
  42. /**
  43. * Get all of the input data for the request.
  44. *
  45. * This method returns a merged array containing $input->get() and $input->files().
  46. *
  47. * @return array
  48. */
  49. public function all()
  50. {
  51. return array_merge($this->get(), $this->file());
  52. }
  53. /**
  54. * Determine if the input data contains an item.
  55. *
  56. * @param string $key
  57. * @return bool
  58. */
  59. public function has($key)
  60. {
  61. return ( ! is_null($this->get($key)) and trim((string) $this->get($key)) !== '');
  62. }
  63. /**
  64. * Get an item from the input data.
  65. *
  66. * This method should be used for all request methods (GET, POST, PUT, and DELETE).
  67. *
  68. * <code>
  69. * // Get an item from the input to the application
  70. * $value = Input::get('name');
  71. *
  72. * // Get an item from the input and return "Fred" if the item doesn't exist
  73. * $value = Input::get('name', 'Fred');
  74. * </code>
  75. *
  76. * @param string $key
  77. * @param mixed $default
  78. * @return mixed
  79. */
  80. public function get($key = null, $default = null)
  81. {
  82. return Arr::get($this->input, $key, $default);
  83. }
  84. /**
  85. * Determine if the old input data contains an item.
  86. *
  87. * @param string $key
  88. * @return bool
  89. */
  90. public function had($key)
  91. {
  92. return ( ! is_null($this->old($key)) and trim((string) $this->old($key)) !== '');
  93. }
  94. /**
  95. * Get input data from the previous request.
  96. *
  97. * <code>
  98. * // Get an item from the previous request's input
  99. * $value = Input::old('name');
  100. *
  101. * // Get an item from the previous request's input and return "Fred" if it doesn't exist.
  102. * $value = Input::old('name', 'Fred');
  103. * </code>
  104. *
  105. * @param string $key
  106. * @param mixed $default
  107. * @return string
  108. */
  109. public function old($key = null, $default = null)
  110. {
  111. if (IoC::container()->resolve('laravel.config')->get('session.driver') == '')
  112. {
  113. throw new \Exception('A session driver must be specified in order to access old input.');
  114. }
  115. $driver = IoC::container()->resolve('laravel.session');
  116. return Arr::get($driver->get('laravel_old_input', array()), $key, $default);
  117. }
  118. /**
  119. * Get an item from the uploaded file data.
  120. *
  121. * "Dot" syntax may be used to get a specific item from the file array.
  122. *
  123. * <code>
  124. * // Get the array of information regarding an uploaded file
  125. * $file = Input::file('picture');
  126. *
  127. * // Get an element from the array of information regarding an uploaded file
  128. * $size = Input::file('picture.size');
  129. * </code>
  130. *
  131. * @param string $key
  132. * @param mixed $default
  133. * @return array
  134. */
  135. public function file($key = null, $default = null)
  136. {
  137. return Arr::get($this->files, $key, $default);
  138. }
  139. /**
  140. * Move an uploaded file to permanent storage.
  141. *
  142. * This method is simply a convenient wrapper around move_uploaded_file.
  143. *
  144. * <code>
  145. * // Move the "picture" file to a permament location on disk
  146. * Input::upload('picture', PUBLIC_PATH.'img/picture.jpg');
  147. * </code>
  148. *
  149. * @param string $key
  150. * @param string $path
  151. * @return bool
  152. */
  153. public function upload($key, $path)
  154. {
  155. return array_key_exists($key, $this->files) ? $this->file->upload($key, $path, $this->files) : false;
  156. }
  157. /**
  158. * Magic Method for retrieving items from the request input.
  159. *
  160. * This method is particularly helpful in controllers where access to the IoC container
  161. * is provided through the controller's magic __get method.
  162. *
  163. * <code>
  164. * // Retrieve the "name" input item from a controller method
  165. * $name = $this->input->name;
  166. * </code>
  167. */
  168. public function __get($key)
  169. {
  170. return $this->get($key);
  171. }
  172. }