|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Created by PhpStorm. |
| 4 | + * User: inhere |
| 5 | + * Date: 2017/6/10 |
| 6 | + * Time: 下午12:26 |
| 7 | + */ |
| 8 | + |
| 9 | +namespace MyLib\Web\Util; |
| 10 | + |
| 11 | +/** |
| 12 | + * Class RequestHelper |
| 13 | + * @package MyLib\Web\Util |
| 14 | + */ |
| 15 | +class RequestUtil |
| 16 | +{ |
| 17 | + /** |
| 18 | + * 本次请求开始时间 |
| 19 | + * @param bool $float |
| 20 | + * @return mixed |
| 21 | + */ |
| 22 | + public static function time($float = true) |
| 23 | + { |
| 24 | + if ((bool)$float) { |
| 25 | + return $_SERVER['REQUEST_TIME_FLOAT']; |
| 26 | + } |
| 27 | + |
| 28 | + return $_SERVER['REQUEST_TIME']; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Get a value from $_POST / $_GET |
| 33 | + * if unavailable, take a default value |
| 34 | + * @param string $key Value key |
| 35 | + * @param mixed $default (optional) |
| 36 | + * @return mixed Value |
| 37 | + */ |
| 38 | + public static function param($key, $default = null) |
| 39 | + { |
| 40 | + if (!$key || !\is_string($key)) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + $ret = $_POST[$key] ?? $_GET[$key] ?? $default; |
| 45 | + |
| 46 | + if (\is_string($ret)) { |
| 47 | + return stripslashes(urldecode(preg_replace('/((\%5C0+)|(\%00+))/i', '', urlencode($ret)))); |
| 48 | + } |
| 49 | + |
| 50 | + return $ret; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param null|string $name |
| 55 | + * @param mixed $default |
| 56 | + * @return mixed |
| 57 | + */ |
| 58 | + public static function get($name = null, $default = null) |
| 59 | + { |
| 60 | + if (null === $name) { |
| 61 | + return $_GET; |
| 62 | + } |
| 63 | + |
| 64 | + return $_GET[$name] ?? $default; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @param string|null $name |
| 69 | + * @param mixed $default |
| 70 | + * @return mixed |
| 71 | + */ |
| 72 | + public static function post($name = null, $default = null) |
| 73 | + { |
| 74 | + $body = self::getParsedBody(); |
| 75 | + |
| 76 | + if (null === $name) { |
| 77 | + return $body; |
| 78 | + } |
| 79 | + |
| 80 | + return $body[$name] ?? $default; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @var false|array |
| 85 | + */ |
| 86 | + private static $parsedBody = false; |
| 87 | + |
| 88 | + /** |
| 89 | + * @return array |
| 90 | + */ |
| 91 | + public static function getParsedBody() |
| 92 | + { |
| 93 | + if (self::$parsedBody === false) { |
| 94 | + // post data is json |
| 95 | + if ( |
| 96 | + !isset($_SERVER['HTTP_CONTENT_TYPE']) || !($type = $_SERVER['HTTP_CONTENT_TYPE']) || strpos($type, |
| 97 | + '/json') <= 0 |
| 98 | + ) { |
| 99 | + self::$parsedBody = &$_POST; |
| 100 | + } else { |
| 101 | + self::$parsedBody = json_decode(file_get_contents('php://input'), true); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return self::$parsedBody; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param $key |
| 110 | + * @return bool |
| 111 | + */ |
| 112 | + public static function hasParam($key) |
| 113 | + { |
| 114 | + if (!$key || !\is_string($key)) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + return isset($_POST[$key]) ? true : isset($_GET[$key]); |
| 119 | + } |
| 120 | + |
| 121 | + public static function safePostVars() |
| 122 | + { |
| 123 | + if (!$_POST || !\is_array($_POST)) { |
| 124 | + $_POST = []; |
| 125 | + } else { |
| 126 | + $_POST = array_map(array(DataHelper::class, 'htmlentitiesUTF8'), $_POST); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Get all values from $_POST/$_GET |
| 132 | + * @return mixed |
| 133 | + */ |
| 134 | + public static function getAll() |
| 135 | + { |
| 136 | + return $_POST + $_GET; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @param $data |
| 141 | + * @param $separator |
| 142 | + * @example |
| 143 | + * /status/active/id/12 |
| 144 | + * => |
| 145 | + * [ |
| 146 | + * 'status' => 'active', |
| 147 | + * 'id' => '12', |
| 148 | + * ] |
| 149 | + * @return array |
| 150 | + */ |
| 151 | + public static function buildQueryParams($data, $separator = '/') |
| 152 | + { |
| 153 | + $arrData = \is_string($data) ? explode($separator, $data) : $data; |
| 154 | + $arrData = array_values(array_filter($arrData)); |
| 155 | + $newArr = []; |
| 156 | + $count = \count($arrData); #统计 |
| 157 | + |
| 158 | + // $arrData 中的 奇数位--变为键,偶数位---变为前一个奇数 键的值 array('前一个奇数'=>'偶数位') |
| 159 | + for ($i = 0; $i < $count; $i += 2) { |
| 160 | + $newArr[$arrData[$i]] = $arrData[$i + 1] ?? ''; |
| 161 | + } |
| 162 | + |
| 163 | + unset($arrData); |
| 164 | + |
| 165 | + return $newArr; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * @param $name |
| 170 | + * @param string $default |
| 171 | + * @return mixed |
| 172 | + */ |
| 173 | + public static function serverParam($name, $default = '') |
| 174 | + { |
| 175 | + return self::server($name, $default); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * get $_SERVER value |
| 180 | + * @param string $name |
| 181 | + * @param string $default |
| 182 | + * @return mixed |
| 183 | + */ |
| 184 | + public static function server($name, $default = '') |
| 185 | + { |
| 186 | + $name = strtoupper($name); |
| 187 | + |
| 188 | + return isset($_SERVER[$name]) ? trim($_SERVER[$name]) : $default; |
| 189 | + } |
| 190 | +} |
0 commit comments