InteractsWithInput.php 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. namespace Illuminate\Http\Concerns;
  3. use stdClass;
  4. use SplFileInfo;
  5. use Illuminate\Support\Arr;
  6. use Illuminate\Support\Str;
  7. use Illuminate\Http\UploadedFile;
  8. trait InteractsWithInput
  9. {
  10. /**
  11. * Retrieve a server variable from the request.
  12. *
  13. * @param string $key
  14. * @param string|array|null $default
  15. * @return string|array
  16. */
  17. public function server($key = null, $default = null)
  18. {
  19. return $this->retrieveItem('server', $key, $default);
  20. }
  21. /**
  22. * Determine if a header is set on the request.
  23. *
  24. * @param string $key
  25. * @return bool
  26. */
  27. public function hasHeader($key)
  28. {
  29. return ! is_null($this->header($key));
  30. }
  31. /**
  32. * Retrieve a header from the request.
  33. *
  34. * @param string $key
  35. * @param string|array|null $default
  36. * @return string|array
  37. */
  38. public function header($key = null, $default = null)
  39. {
  40. return $this->retrieveItem('headers', $key, $default);
  41. }
  42. /**
  43. * Get the bearer token from the request headers.
  44. *
  45. * @return string|null
  46. */
  47. public function bearerToken()
  48. {
  49. $header = $this->header('Authorization', '');
  50. if (Str::startsWith($header, 'Bearer ')) {
  51. return Str::substr($header, 7);
  52. }
  53. }
  54. /**
  55. * Determine if the request contains a given input item key.
  56. *
  57. * @param string|array $key
  58. * @return bool
  59. */
  60. public function exists($key)
  61. {
  62. return $this->has($key);
  63. }
  64. /**
  65. * Determine if the request contains a given input item key.
  66. *
  67. * @param string|array $key
  68. * @return bool
  69. */
  70. public function has($key)
  71. {
  72. $keys = is_array($key) ? $key : func_get_args();
  73. $input = $this->all();
  74. foreach ($keys as $value) {
  75. if (! Arr::has($input, $value)) {
  76. return false;
  77. }
  78. }
  79. return true;
  80. }
  81. /**
  82. * Determine if the request contains any of the given inputs.
  83. *
  84. * @param string|array $keys
  85. * @return bool
  86. */
  87. public function hasAny($keys)
  88. {
  89. $keys = is_array($keys) ? $keys : func_get_args();
  90. $input = $this->all();
  91. foreach ($keys as $key) {
  92. if (Arr::has($input, $key)) {
  93. return true;
  94. }
  95. }
  96. return false;
  97. }
  98. /**
  99. * Determine if the request contains a non-empty value for an input item.
  100. *
  101. * @param string|array $key
  102. * @return bool
  103. */
  104. public function filled($key)
  105. {
  106. $keys = is_array($key) ? $key : func_get_args();
  107. foreach ($keys as $value) {
  108. if ($this->isEmptyString($value)) {
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. /**
  115. * Determine if the request contains a non-empty value for any of the given inputs.
  116. *
  117. * @param string|array $keys
  118. * @return bool
  119. */
  120. public function anyFilled($keys)
  121. {
  122. $keys = is_array($keys) ? $keys : func_get_args();
  123. foreach ($keys as $key) {
  124. if ($this->filled($key)) {
  125. return true;
  126. }
  127. }
  128. return false;
  129. }
  130. /**
  131. * Determine if the given input key is an empty string for "has".
  132. *
  133. * @param string $key
  134. * @return bool
  135. */
  136. protected function isEmptyString($key)
  137. {
  138. $value = $this->input($key);
  139. return ! is_bool($value) && ! is_array($value) && trim((string) $value) === '';
  140. }
  141. /**
  142. * Get the keys for all of the input and files.
  143. *
  144. * @return array
  145. */
  146. public function keys()
  147. {
  148. return array_merge(array_keys($this->input()), $this->files->keys());
  149. }
  150. /**
  151. * Get all of the input and files for the request.
  152. *
  153. * @param array|mixed $keys
  154. * @return array
  155. */
  156. public function all($keys = null)
  157. {
  158. $input = array_replace_recursive($this->input(), $this->allFiles());
  159. if (! $keys) {
  160. return $input;
  161. }
  162. $results = [];
  163. foreach (is_array($keys) ? $keys : func_get_args() as $key) {
  164. Arr::set($results, $key, Arr::get($input, $key));
  165. }
  166. return $results;
  167. }
  168. /**
  169. * Retrieve an input item from the request.
  170. *
  171. * @param string|null $key
  172. * @param string|array|null $default
  173. * @return string|array|null
  174. */
  175. public function input($key = null, $default = null)
  176. {
  177. return data_get(
  178. $this->getInputSource()->all() + $this->query->all(), $key, $default
  179. );
  180. }
  181. /**
  182. * Get a subset containing the provided keys with values from the input data.
  183. *
  184. * @param array|mixed $keys
  185. * @return array
  186. */
  187. public function only($keys)
  188. {
  189. $results = [];
  190. $input = $this->all();
  191. $placeholder = new stdClass;
  192. foreach (is_array($keys) ? $keys : func_get_args() as $key) {
  193. $value = data_get($input, $key, $placeholder);
  194. if ($value !== $placeholder) {
  195. Arr::set($results, $key, $value);
  196. }
  197. }
  198. return $results;
  199. }
  200. /**
  201. * Get all of the input except for a specified array of items.
  202. *
  203. * @param array|mixed $keys
  204. * @return array
  205. */
  206. public function except($keys)
  207. {
  208. $keys = is_array($keys) ? $keys : func_get_args();
  209. $results = $this->all();
  210. Arr::forget($results, $keys);
  211. return $results;
  212. }
  213. /**
  214. * Retrieve a query string item from the request.
  215. *
  216. * @param string $key
  217. * @param string|array|null $default
  218. * @return string|array
  219. */
  220. public function query($key = null, $default = null)
  221. {
  222. return $this->retrieveItem('query', $key, $default);
  223. }
  224. /**
  225. * Retrieve a request payload item from the request.
  226. *
  227. * @param string $key
  228. * @param string|array|null $default
  229. *
  230. * @return string|array
  231. */
  232. public function post($key = null, $default = null)
  233. {
  234. return $this->retrieveItem('request', $key, $default);
  235. }
  236. /**
  237. * Determine if a cookie is set on the request.
  238. *
  239. * @param string $key
  240. * @return bool
  241. */
  242. public function hasCookie($key)
  243. {
  244. return ! is_null($this->cookie($key));
  245. }
  246. /**
  247. * Retrieve a cookie from the request.
  248. *
  249. * @param string $key
  250. * @param string|array|null $default
  251. * @return string|array
  252. */
  253. public function cookie($key = null, $default = null)
  254. {
  255. return $this->retrieveItem('cookies', $key, $default);
  256. }
  257. /**
  258. * Get an array of all of the files on the request.
  259. *
  260. * @return array
  261. */
  262. public function allFiles()
  263. {
  264. $files = $this->files->all();
  265. return $this->convertedFiles
  266. ? $this->convertedFiles
  267. : $this->convertedFiles = $this->convertUploadedFiles($files);
  268. }
  269. /**
  270. * Convert the given array of Symfony UploadedFiles to custom Laravel UploadedFiles.
  271. *
  272. * @param array $files
  273. * @return array
  274. */
  275. protected function convertUploadedFiles(array $files)
  276. {
  277. return array_map(function ($file) {
  278. if (is_null($file) || (is_array($file) && empty(array_filter($file)))) {
  279. return $file;
  280. }
  281. return is_array($file)
  282. ? $this->convertUploadedFiles($file)
  283. : UploadedFile::createFromBase($file);
  284. }, $files);
  285. }
  286. /**
  287. * Determine if the uploaded data contains a file.
  288. *
  289. * @param string $key
  290. * @return bool
  291. */
  292. public function hasFile($key)
  293. {
  294. if (! is_array($files = $this->file($key))) {
  295. $files = [$files];
  296. }
  297. foreach ($files as $file) {
  298. if ($this->isValidFile($file)) {
  299. return true;
  300. }
  301. }
  302. return false;
  303. }
  304. /**
  305. * Check that the given file is a valid file instance.
  306. *
  307. * @param mixed $file
  308. * @return bool
  309. */
  310. protected function isValidFile($file)
  311. {
  312. return $file instanceof SplFileInfo && $file->getPath() !== '';
  313. }
  314. /**
  315. * Retrieve a file from the request.
  316. *
  317. * @param string $key
  318. * @param mixed $default
  319. * @return \Illuminate\Http\UploadedFile|array|null
  320. */
  321. public function file($key = null, $default = null)
  322. {
  323. return data_get($this->allFiles(), $key, $default);
  324. }
  325. /**
  326. * Retrieve a parameter item from a given source.
  327. *
  328. * @param string $source
  329. * @param string $key
  330. * @param string|array|null $default
  331. * @return string|array
  332. */
  333. protected function retrieveItem($source, $key, $default)
  334. {
  335. if (is_null($key)) {
  336. return $this->$source->all();
  337. }
  338. return $this->$source->get($key, $default);
  339. }
  340. }