This commit is contained in:
as2252258@163.com
2021-03-21 01:24:39 +08:00
parent 870ba65d54
commit 22aa0cce1e
+15 -22
View File
@@ -210,7 +210,7 @@ class HttpParams
* @return mixed * @return mixed
* @throws RequestException * @throws RequestException
*/ */
private function required($name, $isNeed = false): mixed private function required(string $name, bool $isNeed = false): mixed
{ {
$body = array_merge($this->body ?? [], $this->socket ?? []); $body = array_merge($this->body ?? [], $this->socket ?? []);
@@ -229,7 +229,7 @@ class HttpParams
* @return int|null * @return int|null
* @throws RequestException * @throws RequestException
*/ */
public function int($name, $isNeed = FALSE, $min = NULL, $max = NULL): ?int public function int(string $name, bool $isNeed = FALSE, array|int|null $min = NULL, int|null $max = NULL): ?int
{ {
$int = $this->required($name, $isNeed); $int = $this->required($name, $isNeed);
if (is_null($int)) return null; if (is_null($int)) return null;
@@ -251,7 +251,7 @@ class HttpParams
* @return float|null * @return float|null
* @throws RequestException * @throws RequestException
*/ */
public function float($name, $isNeed = FALSE, $round = 0): ?float public function float(string $name, bool $isNeed = FALSE, $round = 0): ?float
{ {
$int = $this->required($name, $isNeed); $int = $this->required($name, $isNeed);
if ($int === null) { if ($int === null) {
@@ -272,25 +272,16 @@ class HttpParams
* @return string * @return string
* @throws * @throws
*/ */
public function string($name, $isNeed = FALSE, $length = NULL): string public function string(string $name, bool $isNeed = FALSE, int|array|null $length = NULL): ?string
{ {
$string = $this->required($name, $isNeed); $string = $this->required($name, $isNeed);
if ($string === null || $length === null) { if ($string === null || $length === null) {
return $string; return $string;
} }
if (!is_string($string)) { if (is_numeric($length)) {
$string = json_encode($string, JSON_UNESCAPED_UNICODE); $length = [$length, $length];
} }
$_length = mb_strlen($string); return $this->between($string, $length[0], $length[1] ?? $length[0]);
if (is_array($length)) {
if (count($length) < 2) {
array_unshift($length, 0);
}
$this->between($_length, ...$length);
} else if (is_numeric($length) && $_length != $length) {
throw new RequestException("The length of the string must be $length characters");
}
return $string;
} }
/** /**
@@ -299,14 +290,16 @@ class HttpParams
* @param $max * @param $max
* @throws RequestException * @throws RequestException
*/ */
private function between($_length, $min, $max) private function between($string, $min, $max): mixed
{ {
$_length = mb_strlen($string);
if ($min !== NULL && $_length < $min) { if ($min !== NULL && $_length < $min) {
throw new RequestException("The minimum value cannot be lower than $min, has length $_length"); throw new RequestException("The minimum value cannot be lower than $min, has length $_length");
} }
if ($max !== NULL && $_length > $max) { if ($max !== NULL && $_length > $max) {
throw new RequestException("Maximum cannot exceed $max, has length " . $_length); throw new RequestException("Maximum cannot exceed $max, has length " . $_length);
} }
return $string;
} }
/** /**
@@ -316,7 +309,7 @@ class HttpParams
* @return string|null * @return string|null
* @throws RequestException * @throws RequestException
*/ */
public function email($name, $isNeed = FALSE): ?string public function email(string $name, bool $isNeed = FALSE): ?string
{ {
$email = $this->required($name, $isNeed); $email = $this->required($name, $isNeed);
if ($email === null) { if ($email === null) {
@@ -336,7 +329,7 @@ class HttpParams
* @return bool|string * @return bool|string
* @throws RequestException * @throws RequestException
*/ */
public function bool($name, $isNeed = FALSE): bool|string public function bool(string $name, bool $isNeed = FALSE): bool
{ {
$email = $this->required($name, $isNeed); $email = $this->required($name, $isNeed);
if ($email === null) { if ($email === null) {
@@ -352,7 +345,7 @@ class HttpParams
* @return int|string|null * @return int|string|null
* @throws RequestException * @throws RequestException
*/ */
public function timestamp($name, $default = NULL): null|int|string public function timestamp(string $name, int|null $default = NULL): null|int|string
{ {
$value = $this->required($name, false); $value = $this->required($name, false);
if ($value === null) { if ($value === null) {
@@ -377,7 +370,7 @@ class HttpParams
* @return mixed * @return mixed
* @throws RequestException * @throws RequestException
*/ */
public function datetime($name, $default = NULL): mixed public function datetime(string $name, string $default = NULL): string|null
{ {
$value = $this->required($name, false); $value = $this->required($name, false);
if ($value === null) { if ($value === null) {
@@ -398,7 +391,7 @@ class HttpParams
* @return mixed * @return mixed
* @throws RequestException * @throws RequestException
*/ */
public function ip($name, $default = NULL): mixed public function ip(string $name, string $default = NULL): string|null
{ {
$value = $this->required($name, false); $value = $this->required($name, false);
if ($value == NULL) { if ($value == NULL) {