/vendor/laravel/framework/src/Illuminate/Http/Concerns/InteractsWithInput.php

https://bitbucket.org/rogersantosferreira/bigfeel_web · PHP · 375 lines · 172 code · 52 blank · 151 comment · 15 complexity · 5e8280720a3249b255074d7400ebea1b MD5 · raw file

  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 dynamic $key
  85. * @return bool
  86. */
  87. public function hasAny(...$keys)
  88. {
  89. $input = $this->all();
  90. foreach ($keys as $key) {
  91. if (Arr::has($input, $key)) {
  92. return true;
  93. }
  94. }
  95. return false;
  96. }
  97. /**
  98. * Determine if the request contains a non-empty value for an input item.
  99. *
  100. * @param string|array $key
  101. * @return bool
  102. */
  103. public function filled($key)
  104. {
  105. $keys = is_array($key) ? $key : func_get_args();
  106. foreach ($keys as $value) {
  107. if ($this->isEmptyString($value)) {
  108. return false;
  109. }
  110. }
  111. return true;
  112. }
  113. /**
  114. * Determine if the given input key is an empty string for "has".
  115. *
  116. * @param string $key
  117. * @return bool
  118. */
  119. protected function isEmptyString($key)
  120. {
  121. $value = $this->input($key);
  122. return ! is_bool($value) && ! is_array($value) && trim((string) $value) === '';
  123. }
  124. /**
  125. * Get the keys for all of the input and files.
  126. *
  127. * @return array
  128. */
  129. public function keys()
  130. {
  131. return array_merge(array_keys($this->input()), $this->files->keys());
  132. }
  133. /**
  134. * Get all of the input and files for the request.
  135. *
  136. * @param array|mixed $keys
  137. * @return array
  138. */
  139. public function all($keys = null)
  140. {
  141. $input = array_replace_recursive($this->input(), $this->allFiles());
  142. if (! $keys) {
  143. return $input;
  144. }
  145. $results = [];
  146. foreach (is_array($keys) ? $keys : func_get_args() as $key) {
  147. Arr::set($results, $key, Arr::get($input, $key));
  148. }
  149. return $results;
  150. }
  151. /**
  152. * Retrieve an input item from the request.
  153. *
  154. * @param string $key
  155. * @param string|array|null $default
  156. * @return string|array
  157. */
  158. public function input($key = null, $default = null)
  159. {
  160. return data_get(
  161. $this->getInputSource()->all() + $this->query->all(), $key, $default
  162. );
  163. }
  164. /**
  165. * Get a subset containing the provided keys with values from the input data.
  166. *
  167. * @param array|mixed $keys
  168. * @return array
  169. */
  170. public function only($keys)
  171. {
  172. $results = [];
  173. $input = $this->all();
  174. $placeholder = new stdClass;
  175. foreach (is_array($keys) ? $keys : func_get_args() as $key) {
  176. $value = data_get($input, $key, $placeholder);
  177. if ($value !== $placeholder) {
  178. Arr::set($results, $key, $value);
  179. }
  180. }
  181. return $results;
  182. }
  183. /**
  184. * Get all of the input except for a specified array of items.
  185. *
  186. * @param array|mixed $keys
  187. * @return array
  188. */
  189. public function except($keys)
  190. {
  191. $keys = is_array($keys) ? $keys : func_get_args();
  192. $results = $this->all();
  193. Arr::forget($results, $keys);
  194. return $results;
  195. }
  196. /**
  197. * Retrieve a query string item from the request.
  198. *
  199. * @param string $key
  200. * @param string|array|null $default
  201. * @return string|array
  202. */
  203. public function query($key = null, $default = null)
  204. {
  205. return $this->retrieveItem('query', $key, $default);
  206. }
  207. /**
  208. * Retrieve a request payload item from the request.
  209. *
  210. * @param string $key
  211. * @param string|array|null $default
  212. *
  213. * @return string|array
  214. */
  215. public function post($key = null, $default = null)
  216. {
  217. return $this->retrieveItem('request', $key, $default);
  218. }
  219. /**
  220. * Determine if a cookie is set on the request.
  221. *
  222. * @param string $key
  223. * @return bool
  224. */
  225. public function hasCookie($key)
  226. {
  227. return ! is_null($this->cookie($key));
  228. }
  229. /**
  230. * Retrieve a cookie from the request.
  231. *
  232. * @param string $key
  233. * @param string|array|null $default
  234. * @return string|array
  235. */
  236. public function cookie($key = null, $default = null)
  237. {
  238. return $this->retrieveItem('cookies', $key, $default);
  239. }
  240. /**
  241. * Get an array of all of the files on the request.
  242. *
  243. * @return array
  244. */
  245. public function allFiles()
  246. {
  247. $files = $this->files->all();
  248. return $this->convertedFiles
  249. ? $this->convertedFiles
  250. : $this->convertedFiles = $this->convertUploadedFiles($files);
  251. }
  252. /**
  253. * Convert the given array of Symfony UploadedFiles to custom Laravel UploadedFiles.
  254. *
  255. * @param array $files
  256. * @return array
  257. */
  258. protected function convertUploadedFiles(array $files)
  259. {
  260. return array_map(function ($file) {
  261. if (is_null($file) || (is_array($file) && empty(array_filter($file)))) {
  262. return $file;
  263. }
  264. return is_array($file)
  265. ? $this->convertUploadedFiles($file)
  266. : UploadedFile::createFromBase($file);
  267. }, $files);
  268. }
  269. /**
  270. * Determine if the uploaded data contains a file.
  271. *
  272. * @param string $key
  273. * @return bool
  274. */
  275. public function hasFile($key)
  276. {
  277. if (! is_array($files = $this->file($key))) {
  278. $files = [$files];
  279. }
  280. foreach ($files as $file) {
  281. if ($this->isValidFile($file)) {
  282. return true;
  283. }
  284. }
  285. return false;
  286. }
  287. /**
  288. * Check that the given file is a valid file instance.
  289. *
  290. * @param mixed $file
  291. * @return bool
  292. */
  293. protected function isValidFile($file)
  294. {
  295. return $file instanceof SplFileInfo && $file->getPath() !== '';
  296. }
  297. /**
  298. * Retrieve a file from the request.
  299. *
  300. * @param string $key
  301. * @param mixed $default
  302. * @return \Illuminate\Http\UploadedFile|array|null
  303. */
  304. public function file($key = null, $default = null)
  305. {
  306. return data_get($this->allFiles(), $key, $default);
  307. }
  308. /**
  309. * Retrieve a parameter item from a given source.
  310. *
  311. * @param string $source
  312. * @param string $key
  313. * @param string|array|null $default
  314. * @return string|array
  315. */
  316. protected function retrieveItem($source, $key, $default)
  317. {
  318. if (is_null($key)) {
  319. return $this->$source->all();
  320. }
  321. return $this->$source->get($key, $default);
  322. }
  323. }