input.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 $_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 engine 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 array $input
  37. * @param array $files
  38. * @param Cookie $cookies
  39. * @return void
  40. */
  41. public function __construct($input, $files, Cookie $cookies)
  42. {
  43. $this->input = $input;
  44. $this->files = $files;
  45. $this->cookies = $cookies;
  46. }
  47. /**
  48. * Get all of the input data for the request.
  49. *
  50. * This method returns a merged array containing $input->get() and $input->files().
  51. *
  52. * @return array
  53. */
  54. public function all()
  55. {
  56. return array_merge($this->get(), $this->file());
  57. }
  58. /**
  59. * Determine if the input data contains an item.
  60. *
  61. * @param string $key
  62. * @return bool
  63. */
  64. public function has($key)
  65. {
  66. return ( ! is_null($this->get($key)) and trim((string) $this->get($key)) !== '');
  67. }
  68. /**
  69. * Get an item from the input data.
  70. *
  71. * This method should be used for all request methods (GET, POST, PUT, and DELETE).
  72. *
  73. * @param string $key
  74. * @param mixed $default
  75. * @return mixed
  76. */
  77. public function get($key = null, $default = null)
  78. {
  79. return Arr::get($this->input, $key, $default);
  80. }
  81. /**
  82. * Determine if the old input data contains an item.
  83. *
  84. * @param string $key
  85. * @return bool
  86. */
  87. public function had($key)
  88. {
  89. return ( ! is_null($this->old($key)) and trim((string) $this->old($key)) !== '');
  90. }
  91. /**
  92. * Get input data from the previous request.
  93. *
  94. * @param string $key
  95. * @param mixed $default
  96. * @return string
  97. */
  98. public function old($key = null, $default = null)
  99. {
  100. $driver = IoC::container()->resolve('laravel.session');
  101. return Arr::get($driver->get('laravel_old_input', array()), $key, $default);
  102. }
  103. /**
  104. * Get an item from the uploaded file data.
  105. *
  106. * "Dot" syntax may be used to get a specific item from the file array.
  107. *
  108. * @param string $key
  109. * @param mixed $default
  110. * @return array
  111. */
  112. public function file($key = null, $default = null)
  113. {
  114. return Arr::get($this->files, $key, $default);
  115. }
  116. /**
  117. * Move an uploaded file to permanent storage.
  118. *
  119. * This method is simply a convenient wrapper around move_uploaded_file.
  120. *
  121. * @param string $key
  122. * @param string $path
  123. * @return bool
  124. */
  125. public function upload($key, $path)
  126. {
  127. return array_key_exists($key, $this->files) ? move_uploaded_file($this->files[$key]['tmp_name'], $path) : false;
  128. }
  129. /**
  130. * Magic Method for retrieving items from the request input.
  131. */
  132. public function __get($key)
  133. {
  134. return $this->get($key);
  135. }
  136. }