input.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php namespace System;
  2. class Input {
  3. /**
  4. * The input data for the request.
  5. *
  6. * @var array
  7. */
  8. public static $input;
  9. /**
  10. * Determine if the input data contains an item.
  11. *
  12. * @param string $key
  13. * @return bool
  14. */
  15. public static function has($key)
  16. {
  17. return ( ! is_null(static::get($key)) and trim((string) static::get($key)) !== '');
  18. }
  19. /**
  20. * Get an item from the input data.
  21. *
  22. * @param string $key
  23. * @param mixed $default
  24. * @return string
  25. */
  26. public static function get($key = null, $default = null)
  27. {
  28. if (is_null(static::$input))
  29. {
  30. static::hydrate();
  31. }
  32. return Arr::get(static::$input, $key, $default);
  33. }
  34. /**
  35. * Determine if the old input data contains an item.
  36. *
  37. * @param string $key
  38. * @return bool
  39. */
  40. public static function had($key)
  41. {
  42. return ( ! is_null(static::old($key)) and trim((string) static::old($key)) !== '');
  43. }
  44. /**
  45. * Get input data from the previous request.
  46. *
  47. * @param string $key
  48. * @param mixed $default
  49. * @return string
  50. */
  51. public static function old($key = null, $default = null)
  52. {
  53. // ----------------------------------------------------------
  54. // Since old input data is flashed to the session, we need
  55. // to make sure a session driver has been specified.
  56. // ----------------------------------------------------------
  57. if (Config::get('session.driver') == '')
  58. {
  59. throw new \Exception("Sessions must be enabled to retrieve old input data.");
  60. }
  61. return Arr::get(Session::get('laravel_old_input', array()), $key, $default);
  62. }
  63. /**
  64. * Hydrate the input data for the request.
  65. *
  66. * @return void
  67. */
  68. public static function hydrate()
  69. {
  70. switch (Request::method())
  71. {
  72. case 'GET':
  73. static::$input =& $_GET;
  74. break;
  75. case 'POST':
  76. static::$input =& $_POST;
  77. break;
  78. case 'PUT':
  79. case 'DELETE':
  80. // ----------------------------------------------------------------------
  81. // Typically, browsers do not support PUT and DELETE methods on HTML
  82. // forms. So, we simulate them using a hidden POST variable.
  83. //
  84. // If the request method is being "spoofed", we'll move the POST array
  85. // into the PUT / DELETE array.
  86. // ----------------------------------------------------------------------
  87. if (isset($_POST['request_method']) and ($_POST['request_method'] == 'PUT' or $_POST['request_method'] == 'DELETE'))
  88. {
  89. static::$input =& $_POST;
  90. }
  91. // ----------------------------------------------------------------------
  92. // If the request is a true PUT request, read the php://input file.
  93. // ----------------------------------------------------------------------
  94. else
  95. {
  96. parse_str(file_get_contents('php://input'), static::$input);
  97. }
  98. }
  99. }
  100. }