From 4fac0621e733f060664f2325fb2ed8ce57527b64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 6 Sep 2021 11:18:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http-server/Message/Request.php | 68 ++++++++++++++++----------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/http-server/Message/Request.php b/http-server/Message/Request.php index 10ba5899..69f1b7b5 100644 --- a/http-server/Message/Request.php +++ b/http-server/Message/Request.php @@ -153,70 +153,70 @@ class Request implements RequestInterface /** - * @param $name - * @param $required - * @return mixed + * @param string $name + * @param bool $required + * @return string|null * @throws Exception */ - public function string($name, $required): mixed - { - 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) + 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 null; } return (string)$data; } /** - * @param $name - * @param $required - * @return mixed + * @param string $name + * @param bool $required + * @return int|null * @throws Exception */ - public function float($name, $required) + public function int(string $name, bool $required = false): ?int { if (is_null($data = $this->post($name))) { if ($required) { 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; } /** - * @param $name - * @param $required + * @param string $name + * @param array $default * @return mixed - * @throws Exception */ - public function array($name, $required) + public function array(string $name, array $default = []): array { - if (is_null($data = $this->post($name))) { - if ($required) { - throw new Exception('Parameter is required and cannot be empty.'); - } + $data = $this->post($name); + if (!is_array($data)) { + return $default; } - if (!is_array($data)) return []; return $data; }