input.php 2.9 KB

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