This commit is contained in:
2021-09-06 11:18:13 +08:00
parent ca24835df8
commit 4fac0621e7
+34 -34
View File
@@ -153,70 +153,70 @@ class Request implements RequestInterface
/** /**
* @param $name * @param string $name
* @param $required * @param bool $required
* @return mixed * @return string|null
* @throws Exception * @throws Exception
*/ */
public function string($name, $required): mixed public function string(string $name, bool $required = false): ?string
{
if (is_null($data = $this->post($name))) {
if ($required) {
throw new Exception('Parameter is required and cannot be empty.');
}
}
return $data;
}
/**
* @param $name
* @param $required
* @return mixed
* @throws Exception
*/
public function int($name, $required)
{ {
if (is_null($data = $this->post($name))) { if (is_null($data = $this->post($name))) {
if ($required) { if ($required) {
throw new Exception('Parameter is required and cannot be empty.'); throw new Exception('Parameter is required and cannot be empty.');
} }
return null;
} }
return (string)$data; return (string)$data;
} }
/** /**
* @param $name * @param string $name
* @param $required * @param bool $required
* @return mixed * @return int|null
* @throws Exception * @throws Exception
*/ */
public function float($name, $required) public function int(string $name, bool $required = false): ?int
{ {
if (is_null($data = $this->post($name))) { if (is_null($data = $this->post($name))) {
if ($required) { if ($required) {
throw new Exception('Parameter is required and cannot be empty.'); throw new Exception('Parameter is required and cannot be empty.');
} }
return null;
}
return (string)$data;
}
/**
* @param string $name
* @param bool $required
* @return float|null
* @throws Exception
*/
public function float(string $name, bool $required = false): ?float
{
if (is_null($data = $this->post($name))) {
if ($required) {
throw new Exception('Parameter is required and cannot be empty.');
}
return null;
} }
return (float)$data; return (float)$data;
} }
/** /**
* @param $name * @param string $name
* @param $required * @param array $default
* @return mixed * @return mixed
* @throws Exception
*/ */
public function array($name, $required) public function array(string $name, array $default = []): array
{ {
if (is_null($data = $this->post($name))) { $data = $this->post($name);
if ($required) { if (!is_array($data)) {
throw new Exception('Parameter is required and cannot be empty.'); return $default;
} }
}
if (!is_array($data)) return [];
return $data; return $data;
} }