input.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. * @param string $key
  48. * @return bool
  49. */
  50. public function has($key)
  51. {
  52. return ( ! is_null($this->get($key)) and trim((string) $this->get($key)) !== '');
  53. }
  54. /**
  55. * Get an item from the input data.
  56. *
  57. * This method should be used for all request methods (GET, POST, PUT, and DELETE).
  58. *
  59. * @param string $key
  60. * @param mixed $default
  61. * @return mixed
  62. */
  63. public function get($key = null, $default = null)
  64. {
  65. return Arr::get($this->input, $key, $default);
  66. }
  67. /**
  68. * Determine if the old input data contains an item.
  69. *
  70. * @param string $key
  71. * @return bool
  72. */
  73. public function had($key)
  74. {
  75. return ( ! is_null($this->old($key)) and trim((string) $this->old($key)) !== '');
  76. }
  77. /**
  78. * Get input data from the previous request.
  79. *
  80. * @param string $key
  81. * @param mixed $default
  82. * @return string
  83. */
  84. public 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. $driver = IoC::container()->resolve('laravel.session');
  91. return Arr::get($driver->get(Input::old_input, array()), $key, $default);
  92. }
  93. /**
  94. * Get an item from the uploaded file data.
  95. *
  96. * @param string $key
  97. * @param mixed $default
  98. * @return array
  99. */
  100. public function file($key = null, $default = null)
  101. {
  102. return Arr::get($this->files, $key, $default);
  103. }
  104. /**
  105. * Move an uploaded file to permanent storage.
  106. *
  107. * This method is simply a convenient wrapper around move_uploaded_file.
  108. *
  109. * @param string $key
  110. * @param string $path
  111. * @return bool
  112. */
  113. public function upload($key, $path)
  114. {
  115. return array_key_exists($key, $this->files) ? File::upload($key, $path, $this->files) : false;
  116. }
  117. }