You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

142 lines
3.4 KiB

  1. <?php
  2. namespace Kuxin;
  3. /**
  4. * Class Input
  5. *
  6. * @package Kuxin
  7. * @author Pakey <pakey@qq.com>
  8. */
  9. class Input
  10. {
  11. /**
  12. * 获取$_GET
  13. *
  14. * @param $name
  15. * @param $type
  16. * @param mixed $default
  17. * @return array|float|int|mixed|null|string
  18. */
  19. public static function get(string $name = '', $type = '', $default = null)
  20. {
  21. return self::param($name, $type, $default, $_GET);
  22. }
  23. /**
  24. * 获取$_POST
  25. *
  26. * @param $name
  27. * @param $type
  28. * @param mixed $default
  29. * @return array|float|int|mixed|null|string
  30. */
  31. public static function post(string $name = '', $type = '', $default = null)
  32. {
  33. return self::param($name, $type, $default, $_POST);
  34. }
  35. /**
  36. * 获取$_REQUEST
  37. *
  38. * @param $name
  39. * @param $type
  40. * @param mixed $default
  41. * @return array|float|int|mixed|null|string
  42. */
  43. public static function request(string $name = '', $type = '', $default = null)
  44. {
  45. return self::param($name, $type, $default, $_REQUEST);
  46. }
  47. /**
  48. * 获取put
  49. *
  50. * @param $name
  51. * @param $type
  52. * @param mixed $default
  53. * @return array|float|int|mixed|null|string
  54. */
  55. public static function put(string $name = '', $type = '', $default = null)
  56. {
  57. parse_str(file_get_contents('php://input'), $input);
  58. return self::param($name, $type, $default, $input);
  59. }
  60. /**
  61. * 获取$_SERVER
  62. *
  63. * @param $name
  64. * @param $type
  65. * @param mixed $default
  66. * @return array|float|int|mixed|null|string
  67. */
  68. public static function server(string $name = '', $type = '', $default = null)
  69. {
  70. return self::param($name, $type, $default, $_SERVER);
  71. }
  72. /**
  73. * 获取$GLOBALS
  74. *
  75. * @param $name
  76. * @param $type
  77. * @param mixed $default
  78. * @return array|float|int|mixed|null|string
  79. */
  80. public static function globals(string $name = '', $type = '', $default = null)
  81. {
  82. return self::param($name, $type, $default, $GLOBALS);
  83. }
  84. /**
  85. * 获取$_FILES
  86. *
  87. * @param $name
  88. * @param $type
  89. * @param mixed $default
  90. * @return array|float|int|mixed|null|string
  91. */
  92. public static function files(string $name = '', $type = '', $default = null)
  93. {
  94. return self::param($name, $type, $default, $_FILES);
  95. }
  96. /**
  97. * 判断$_REQUEST 是否有某个值
  98. *
  99. * @param $name
  100. * @param array $type
  101. * @return bool
  102. */
  103. public static function has(string $name, array $type = null): bool
  104. {
  105. $type = $type ?: $_REQUEST;
  106. return isset($type[$name]);
  107. }
  108. /**
  109. * @param string $name
  110. * @param $filter
  111. * @param mixed $default
  112. * @param array $param
  113. * @return array|float|int|mixed|null|string
  114. */
  115. public static function param(string $name, $filter = '', $default = null, array $param = [])
  116. {
  117. if ($name == '') {
  118. return $param;
  119. }
  120. if (!isset($param[$name])) {
  121. return is_callable($default) ? $default($name) : $default;
  122. } else {
  123. $value = $param[$name];
  124. if($filter){
  125. $value = Filter::check($value, $filter);
  126. }
  127. }
  128. return $value;
  129. }
  130. }