input.php 4.0 KB

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