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
* @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 ?? []);
@@ -229,7 +229,7 @@ class HttpParams
* @return int|null
* @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);
if (is_null($int)) return null;
@@ -251,7 +251,7 @@ class HttpParams
* @return float|null
* @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);
if ($int === null) {
@@ -272,25 +272,16 @@ class HttpParams
* @return string
* @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);
if ($string === null || $length === null) {
return $string;
}
if (!is_string($string)) {
$string = json_encode($string, JSON_UNESCAPED_UNICODE);
if (is_numeric($length)) {
$length = [$length, $length];
}
$_length = mb_strlen($string);
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;
return $this->between($string, $length[0], $length[1] ?? $length[0]);
}
/**
@@ -299,14 +290,16 @@ class HttpParams
* @param $max
* @throws RequestException
*/
private function between($_length, $min, $max)
private function between($string, $min, $max): mixed
{
$_length = mb_strlen($string);
if ($min !== NULL && $_length < $min) {
throw new RequestException("The minimum value cannot be lower than $min, has length $_length");
}
if ($max !== NULL && $_length > $max) {
throw new RequestException("Maximum cannot exceed $max, has length " . $_length);
}
return $string;
}
/**
@@ -316,7 +309,7 @@ class HttpParams
* @return string|null
* @throws RequestException
*/
public function email($name, $isNeed = FALSE): ?string
public function email(string $name, bool $isNeed = FALSE): ?string
{
$email = $this->required($name, $isNeed);
if ($email === null) {
@@ -336,7 +329,7 @@ class HttpParams
* @return bool|string
* @throws RequestException
*/
public function bool($name, $isNeed = FALSE): bool|string
public function bool(string $name, bool $isNeed = FALSE): bool
{
$email = $this->required($name, $isNeed);
if ($email === null) {
@@ -352,7 +345,7 @@ class HttpParams
* @return int|string|null
* @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);
if ($value === null) {
@@ -377,7 +370,7 @@ class HttpParams
* @return mixed
* @throws RequestException
*/
public function datetime($name, $default = NULL): mixed
public function datetime(string $name, string $default = NULL): string|null
{
$value = $this->required($name, false);
if ($value === null) {
@@ -398,7 +391,7 @@ class HttpParams
* @return mixed
* @throws RequestException
*/
public function ip($name, $default = NULL): mixed
public function ip(string $name, string $default = NULL): string|null
{
$value = $this->required($name, false);
if ($value == NULL) {