PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/laravel/input.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 187 lines | 51 code | 16 blank | 120 comment | 3 complexity | 313c0428463c6fdb9adb26c136c3382a MD5 | raw file
  1. <?php namespace Laravel;
  2. class Input {
  3. /**
  4. * The applicable input for the request.
  5. *
  6. * @var array
  7. */
  8. public static $input;
  9. /**
  10. * The key used to store old input in the session.
  11. *
  12. * @var string
  13. */
  14. const old_input = 'laravel_old_input';
  15. /**
  16. * Get all of the input data for the request.
  17. *
  18. * This method returns a merged array containing Input::get() and Input::files().
  19. *
  20. * @return array
  21. */
  22. public static function all()
  23. {
  24. return array_merge(static::get(), static::file());
  25. }
  26. /**
  27. * Determine if the input data contains an item.
  28. *
  29. * If the item is in the input array, but is an empty string, false will be returned.
  30. *
  31. * @param string $key
  32. * @return bool
  33. */
  34. public static function has($key)
  35. {
  36. return trim((string) static::get($key)) !== '';
  37. }
  38. /**
  39. * Get an item from the input data.
  40. *
  41. * This method should be used for all request methods (GET, POST, PUT, and DELETE).
  42. *
  43. * <code>
  44. * // Get the "email" item from the input array
  45. * $email = Input::get('email');
  46. *
  47. * // Return a default value if the specified item doesn't exist
  48. * $email = Input::get('name', 'Taylor');
  49. * </code>
  50. *
  51. * @param string $key
  52. * @param mixed $default
  53. * @return mixed
  54. */
  55. public static function get($key = null, $default = null)
  56. {
  57. return Arr::get(static::$input, $key, $default);
  58. }
  59. /**
  60. * Flash the input for the current request to the session.
  61. *
  62. * The input data to be flashed may be controlled by using a filter and an array
  63. * of included or excluded input data. This provides a convenient way of keeping
  64. * sensitive information like passwords out of the session.
  65. *
  66. * <code>
  67. * // Flash all of the input data to the session
  68. * Input::flash();
  69. *
  70. * // Flash only a few input items to the session
  71. * Input::flash('only', array('name', 'email'));
  72. *
  73. * // Flash all but a few input items to the session
  74. * Input::flash('except', array('password'));
  75. * </code>
  76. *
  77. * @return void
  78. */
  79. public static function flash($filter = null, $items = array())
  80. {
  81. $flash = static::get();
  82. // Since the items flashed to the session can be filtered, we will iterate
  83. // all of the input data and either remove or include the input item based
  84. // on the specified filter and array of items to be flashed.
  85. if ($filter == 'only')
  86. {
  87. $flash = array_intersect_key($flash, array_flip($items));
  88. }
  89. elseif ($filter == 'except')
  90. {
  91. $flash = array_diff_key($flash, array_flip($items));
  92. }
  93. IoC::core('session')->flash(Input::old_input, $flash);
  94. }
  95. /**
  96. * Flush the old input from the session.
  97. *
  98. * @return void
  99. */
  100. public static function flush()
  101. {
  102. IoC::core('session')->flash(Input::old_input, array());
  103. }
  104. /**
  105. * Determine if the old input data contains an item.
  106. *
  107. * @param string $key
  108. * @return bool
  109. */
  110. public static function had($key)
  111. {
  112. return trim((string) static::old($key)) !== '';
  113. }
  114. /**
  115. * Get input data from the previous request.
  116. *
  117. * <code>
  118. * // Get the "email" item from the old input
  119. * $email = Input::old('email');
  120. *
  121. * // Return a default value if the specified item doesn't exist
  122. * $email = Input::old('name', 'Taylor');
  123. * </code>
  124. *
  125. * @param string $key
  126. * @param mixed $default
  127. * @return string
  128. */
  129. public static function old($key = null, $default = null)
  130. {
  131. $old = IoC::core('session')->get(Input::old_input, array());
  132. return Arr::get($old, $key, $default);
  133. }
  134. /**
  135. * Get an item from the uploaded file data.
  136. *
  137. * <code>
  138. * // Get the array of information for the "picture" upload
  139. * $picture = Input::file('picture');
  140. *
  141. * // Get a specific element from the file array
  142. * $size = Input::file('picture.size');
  143. * </code>
  144. *
  145. * @param string $key
  146. * @param mixed $default
  147. * @return array
  148. */
  149. public static function file($key = null, $default = null)
  150. {
  151. return Arr::get($_FILES, $key, $default);
  152. }
  153. /**
  154. * Move an uploaded file to permanent storage.
  155. *
  156. * This method is simply a convenient wrapper around move_uploaded_file.
  157. *
  158. * <code>
  159. * // Move the "picture" item from the $_FILES array to a permanent location
  160. * Input::upload('picture', 'path/to/storage/picture.jpg');
  161. * </code>
  162. *
  163. * @param string $key
  164. * @param string $path
  165. * @return bool
  166. */
  167. public static function upload($key, $path)
  168. {
  169. return File::upload($key, $path);
  170. }
  171. }