PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/jjpa2018/dashboard
PHP | 540 lines | 243 code | 78 blank | 219 comment | 22 complexity | 969f9794e61158d05b3a6512f10bca0b MD5 | raw file
  1. <?php
  2. namespace Illuminate\Http\Concerns;
  3. use Illuminate\Http\UploadedFile;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Support\Facades\Date;
  6. use SplFileInfo;
  7. use stdClass;
  8. use Symfony\Component\VarDumper\VarDumper;
  9. trait InteractsWithInput
  10. {
  11. /**
  12. * Retrieve a server variable from the request.
  13. *
  14. * @param string|null $key
  15. * @param string|array|null $default
  16. * @return string|array|null
  17. */
  18. public function server($key = null, $default = null)
  19. {
  20. return $this->retrieveItem('server', $key, $default);
  21. }
  22. /**
  23. * Determine if a header is set on the request.
  24. *
  25. * @param string $key
  26. * @return bool
  27. */
  28. public function hasHeader($key)
  29. {
  30. return ! is_null($this->header($key));
  31. }
  32. /**
  33. * Retrieve a header from the request.
  34. *
  35. * @param string|null $key
  36. * @param string|array|null $default
  37. * @return string|array|null
  38. */
  39. public function header($key = null, $default = null)
  40. {
  41. return $this->retrieveItem('headers', $key, $default);
  42. }
  43. /**
  44. * Get the bearer token from the request headers.
  45. *
  46. * @return string|null
  47. */
  48. public function bearerToken()
  49. {
  50. $header = $this->header('Authorization', '');
  51. $position = strrpos($header, 'Bearer ');
  52. if ($position !== false) {
  53. $header = substr($header, $position + 7);
  54. return strpos($header, ',') !== false ? strstr(',', $header, true) : $header;
  55. }
  56. }
  57. /**
  58. * Determine if the request contains a given input item key.
  59. *
  60. * @param string|array $key
  61. * @return bool
  62. */
  63. public function exists($key)
  64. {
  65. return $this->has($key);
  66. }
  67. /**
  68. * Determine if the request contains a given input item key.
  69. *
  70. * @param string|array $key
  71. * @return bool
  72. */
  73. public function has($key)
  74. {
  75. $keys = is_array($key) ? $key : func_get_args();
  76. $input = $this->all();
  77. foreach ($keys as $value) {
  78. if (! Arr::has($input, $value)) {
  79. return false;
  80. }
  81. }
  82. return true;
  83. }
  84. /**
  85. * Determine if the request contains any of the given inputs.
  86. *
  87. * @param string|array $keys
  88. * @return bool
  89. */
  90. public function hasAny($keys)
  91. {
  92. $keys = is_array($keys) ? $keys : func_get_args();
  93. $input = $this->all();
  94. return Arr::hasAny($input, $keys);
  95. }
  96. /**
  97. * Apply the callback if the request contains the given input item key.
  98. *
  99. * @param string $key
  100. * @param callable $callback
  101. * @param callable|null $default
  102. * @return $this|mixed
  103. */
  104. public function whenHas($key, callable $callback, callable $default = null)
  105. {
  106. if ($this->has($key)) {
  107. return $callback(data_get($this->all(), $key)) ?: $this;
  108. }
  109. if ($default) {
  110. return $default();
  111. }
  112. return $this;
  113. }
  114. /**
  115. * Determine if the request contains a non-empty value for an input item.
  116. *
  117. * @param string|array $key
  118. * @return bool
  119. */
  120. public function filled($key)
  121. {
  122. $keys = is_array($key) ? $key : func_get_args();
  123. foreach ($keys as $value) {
  124. if ($this->isEmptyString($value)) {
  125. return false;
  126. }
  127. }
  128. return true;
  129. }
  130. /**
  131. * Determine if the request contains an empty value for an input item.
  132. *
  133. * @param string|array $key
  134. * @return bool
  135. */
  136. public function isNotFilled($key)
  137. {
  138. $keys = is_array($key) ? $key : func_get_args();
  139. foreach ($keys as $value) {
  140. if (! $this->isEmptyString($value)) {
  141. return false;
  142. }
  143. }
  144. return true;
  145. }
  146. /**
  147. * Determine if the request contains a non-empty value for any of the given inputs.
  148. *
  149. * @param string|array $keys
  150. * @return bool
  151. */
  152. public function anyFilled($keys)
  153. {
  154. $keys = is_array($keys) ? $keys : func_get_args();
  155. foreach ($keys as $key) {
  156. if ($this->filled($key)) {
  157. return true;
  158. }
  159. }
  160. return false;
  161. }
  162. /**
  163. * Apply the callback if the request contains a non-empty value for the given input item key.
  164. *
  165. * @param string $key
  166. * @param callable $callback
  167. * @param callable|null $default
  168. * @return $this|mixed
  169. */
  170. public function whenFilled($key, callable $callback, callable $default = null)
  171. {
  172. if ($this->filled($key)) {
  173. return $callback(data_get($this->all(), $key)) ?: $this;
  174. }
  175. if ($default) {
  176. return $default();
  177. }
  178. return $this;
  179. }
  180. /**
  181. * Determine if the request is missing a given input item key.
  182. *
  183. * @param string|array $key
  184. * @return bool
  185. */
  186. public function missing($key)
  187. {
  188. $keys = is_array($key) ? $key : func_get_args();
  189. return ! $this->has($keys);
  190. }
  191. /**
  192. * Determine if the given input key is an empty string for "has".
  193. *
  194. * @param string $key
  195. * @return bool
  196. */
  197. protected function isEmptyString($key)
  198. {
  199. $value = $this->input($key);
  200. return ! is_bool($value) && ! is_array($value) && trim((string) $value) === '';
  201. }
  202. /**
  203. * Get the keys for all of the input and files.
  204. *
  205. * @return array
  206. */
  207. public function keys()
  208. {
  209. return array_merge(array_keys($this->input()), $this->files->keys());
  210. }
  211. /**
  212. * Get all of the input and files for the request.
  213. *
  214. * @param array|mixed|null $keys
  215. * @return array
  216. */
  217. public function all($keys = null)
  218. {
  219. $input = array_replace_recursive($this->input(), $this->allFiles());
  220. if (! $keys) {
  221. return $input;
  222. }
  223. $results = [];
  224. foreach (is_array($keys) ? $keys : func_get_args() as $key) {
  225. Arr::set($results, $key, Arr::get($input, $key));
  226. }
  227. return $results;
  228. }
  229. /**
  230. * Retrieve an input item from the request.
  231. *
  232. * @param string|null $key
  233. * @param mixed $default
  234. * @return mixed
  235. */
  236. public function input($key = null, $default = null)
  237. {
  238. return data_get(
  239. $this->getInputSource()->all() + $this->query->all(), $key, $default
  240. );
  241. }
  242. /**
  243. * Retrieve input as a boolean value.
  244. *
  245. * Returns true when value is "1", "true", "on", and "yes". Otherwise, returns false.
  246. *
  247. * @param string|null $key
  248. * @param bool $default
  249. * @return bool
  250. */
  251. public function boolean($key = null, $default = false)
  252. {
  253. return filter_var($this->input($key, $default), FILTER_VALIDATE_BOOLEAN);
  254. }
  255. /**
  256. * Retrieve input from the request as a Carbon instance.
  257. *
  258. * @param string $key
  259. * @param string|null $format
  260. * @param string|null $tz
  261. * @return \Illuminate\Support\Carbon|null
  262. */
  263. public function date($key, $format = null, $tz = null)
  264. {
  265. if ($this->isNotFilled($key)) {
  266. return null;
  267. }
  268. if (is_null($format)) {
  269. return Date::parse($this->input($key), $tz);
  270. }
  271. return Date::createFromFormat($format, $this->input($key), $tz);
  272. }
  273. /**
  274. * Retrieve input from the request as a collection.
  275. *
  276. * @param array|string|null $key
  277. * @return \Illuminate\Support\Collection
  278. */
  279. public function collect($key = null)
  280. {
  281. return collect(is_array($key) ? $this->only($key) : $this->input($key));
  282. }
  283. /**
  284. * Get a subset containing the provided keys with values from the input data.
  285. *
  286. * @param array|mixed $keys
  287. * @return array
  288. */
  289. public function only($keys)
  290. {
  291. $results = [];
  292. $input = $this->all();
  293. $placeholder = new stdClass;
  294. foreach (is_array($keys) ? $keys : func_get_args() as $key) {
  295. $value = data_get($input, $key, $placeholder);
  296. if ($value !== $placeholder) {
  297. Arr::set($results, $key, $value);
  298. }
  299. }
  300. return $results;
  301. }
  302. /**
  303. * Get all of the input except for a specified array of items.
  304. *
  305. * @param array|mixed $keys
  306. * @return array
  307. */
  308. public function except($keys)
  309. {
  310. $keys = is_array($keys) ? $keys : func_get_args();
  311. $results = $this->all();
  312. Arr::forget($results, $keys);
  313. return $results;
  314. }
  315. /**
  316. * Retrieve a query string item from the request.
  317. *
  318. * @param string|null $key
  319. * @param string|array|null $default
  320. * @return string|array|null
  321. */
  322. public function query($key = null, $default = null)
  323. {
  324. return $this->retrieveItem('query', $key, $default);
  325. }
  326. /**
  327. * Retrieve a request payload item from the request.
  328. *
  329. * @param string|null $key
  330. * @param string|array|null $default
  331. * @return string|array|null
  332. */
  333. public function post($key = null, $default = null)
  334. {
  335. return $this->retrieveItem('request', $key, $default);
  336. }
  337. /**
  338. * Determine if a cookie is set on the request.
  339. *
  340. * @param string $key
  341. * @return bool
  342. */
  343. public function hasCookie($key)
  344. {
  345. return ! is_null($this->cookie($key));
  346. }
  347. /**
  348. * Retrieve a cookie from the request.
  349. *
  350. * @param string|null $key
  351. * @param string|array|null $default
  352. * @return string|array|null
  353. */
  354. public function cookie($key = null, $default = null)
  355. {
  356. return $this->retrieveItem('cookies', $key, $default);
  357. }
  358. /**
  359. * Get an array of all of the files on the request.
  360. *
  361. * @return array
  362. */
  363. public function allFiles()
  364. {
  365. $files = $this->files->all();
  366. return $this->convertedFiles = $this->convertedFiles ?? $this->convertUploadedFiles($files);
  367. }
  368. /**
  369. * Convert the given array of Symfony UploadedFiles to custom Laravel UploadedFiles.
  370. *
  371. * @param array $files
  372. * @return array
  373. */
  374. protected function convertUploadedFiles(array $files)
  375. {
  376. return array_map(function ($file) {
  377. if (is_null($file) || (is_array($file) && empty(array_filter($file)))) {
  378. return $file;
  379. }
  380. return is_array($file)
  381. ? $this->convertUploadedFiles($file)
  382. : UploadedFile::createFromBase($file);
  383. }, $files);
  384. }
  385. /**
  386. * Determine if the uploaded data contains a file.
  387. *
  388. * @param string $key
  389. * @return bool
  390. */
  391. public function hasFile($key)
  392. {
  393. if (! is_array($files = $this->file($key))) {
  394. $files = [$files];
  395. }
  396. foreach ($files as $file) {
  397. if ($this->isValidFile($file)) {
  398. return true;
  399. }
  400. }
  401. return false;
  402. }
  403. /**
  404. * Check that the given file is a valid file instance.
  405. *
  406. * @param mixed $file
  407. * @return bool
  408. */
  409. protected function isValidFile($file)
  410. {
  411. return $file instanceof SplFileInfo && $file->getPath() !== '';
  412. }
  413. /**
  414. * Retrieve a file from the request.
  415. *
  416. * @param string|null $key
  417. * @param mixed $default
  418. * @return \Illuminate\Http\UploadedFile|\Illuminate\Http\UploadedFile[]|array|null
  419. */
  420. public function file($key = null, $default = null)
  421. {
  422. return data_get($this->allFiles(), $key, $default);
  423. }
  424. /**
  425. * Retrieve a parameter item from a given source.
  426. *
  427. * @param string $source
  428. * @param string $key
  429. * @param string|array|null $default
  430. * @return string|array|null
  431. */
  432. protected function retrieveItem($source, $key, $default)
  433. {
  434. if (is_null($key)) {
  435. return $this->$source->all();
  436. }
  437. return $this->$source->get($key, $default);
  438. }
  439. /**
  440. * Dump the request items and end the script.
  441. *
  442. * @param mixed $keys
  443. * @return void
  444. */
  445. public function dd(...$keys)
  446. {
  447. $this->dump(...$keys);
  448. exit(1);
  449. }
  450. /**
  451. * Dump the items.
  452. *
  453. * @param mixed $keys
  454. * @return $this
  455. */
  456. public function dump($keys = [])
  457. {
  458. $keys = is_array($keys) ? $keys : func_get_args();
  459. VarDumper::dump(count($keys) > 0 ? $this->only($keys) : $this->all());
  460. return $this;
  461. }
  462. }