input.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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)));
  18. }
  19. /**
  20. * Determine if the old input data contains an item.
  21. *
  22. * @param string $key
  23. * @return bool
  24. */
  25. public static function has_old($key)
  26. {
  27. return ( ! is_null(static::old($key)));
  28. }
  29. /**
  30. * Get an item from the input data.
  31. *
  32. * @param string $key
  33. * @param mixed $default
  34. * @return string
  35. */
  36. public static function get($key = null, $default = null)
  37. {
  38. static::hydrate();
  39. return static::from_array(static::$input, $key, $default);
  40. }
  41. /**
  42. * Get input data from the previous request.
  43. *
  44. * @param string $key
  45. * @param mixed $default
  46. * @return string
  47. */
  48. public static function old($key = null, $default = null)
  49. {
  50. if (Config::get('session.driver') == '')
  51. {
  52. throw new \Exception("Sessions must be enabled to retrieve old input data.");
  53. }
  54. return static::from_array(Session::get('laravel_old_input', array()), $key, $default);
  55. }
  56. /**
  57. * Get an item from an array. If no key is specified, the entire array will be returned.
  58. *
  59. * @param array $array
  60. * @param string $key
  61. * @param mixed $default
  62. * @return string
  63. */
  64. private static function from_array($array, $key, $default)
  65. {
  66. if (is_null($key))
  67. {
  68. return $array;
  69. }
  70. return (array_key_exists($key, $array)) ? $array[$key] : $default;
  71. }
  72. /**
  73. * Hydrate the input data for the request.
  74. *
  75. * @return void
  76. */
  77. public static function hydrate()
  78. {
  79. if (is_null(static::$input))
  80. {
  81. switch (Request::method())
  82. {
  83. case 'GET':
  84. static::$input =& $_GET;
  85. break;
  86. case 'POST':
  87. static::$input =& $_POST;
  88. break;
  89. case 'PUT':
  90. case 'DELETE':
  91. // ----------------------------------------------------------------------
  92. // Typically, browsers do not support PUT and DELETE methods on HTML
  93. // forms. So, we simulate them using a hidden POST variable.
  94. //
  95. // If the request method is being "spoofed", we'll move the POST array
  96. // into the PUT / DELETE array.
  97. // ----------------------------------------------------------------------
  98. if (isset($_POST['request_method']) and ($_POST['request_method'] == 'PUT' or $_POST['request_method'] == 'DELETE'))
  99. {
  100. static::$input =& $_POST;
  101. }
  102. // ----------------------------------------------------------------------
  103. // If the request is a true PUT request, read the php://input file.
  104. // ----------------------------------------------------------------------
  105. else
  106. {
  107. parse_str(file_get_contents('php://input'), static::$input);
  108. }
  109. }
  110. }
  111. }
  112. }