From ae10aa90764d26e28ef17656dc5c043ecba06e60 Mon Sep 17 00:00:00 2001 From: tigerb <624661874@qq.com> Date: Sun, 21 May 2017 19:42:39 +0800 Subject: [PATCH] feat(request): add request param check :sparkles: --- app/demo/controllers/Index.php | 8 +++- framework/Request.php | 78 +++++++++++++++++++++++++++------- runtime/.gitignore | 0 runtime/.gitkeep | 0 4 files changed, 69 insertions(+), 17 deletions(-) mode change 100644 => 100755 runtime/.gitignore mode change 100644 => 100755 runtime/.gitkeep diff --git a/app/demo/controllers/Index.php b/app/demo/controllers/Index.php index 90e06fe..1af3c89 100644 --- a/app/demo/controllers/Index.php +++ b/app/demo/controllers/Index.php @@ -10,7 +10,7 @@ namespace App\Demo\Controllers; use Framework\App; -use Framework\Loger; +use Framework\Logger; /** * Index Controller @@ -42,12 +42,16 @@ public function hello() * * @param string $username 用户名 * @param string $password 密码 - * @example domain/Demo/Index/get?username=test&password=123456 + * @param number code 验证码 + * @example domain/Demo/Index/test?username=tigerb&password=123456789987&code=123456 * @return json */ public function test() { $request = App::$container->getSingle('request'); + $request->check('username', 'require'); + $request->check('password', 'length', 12); + $request->check('code', 'number'); return [ 'username' => $request->get('username', 'default value') ]; diff --git a/framework/Request.php b/framework/Request.php index 1b1f479..bb3ba35 100644 --- a/framework/Request.php +++ b/framework/Request.php @@ -11,6 +11,8 @@ namespace Framework; +use Framework\Exceptions\CoreHttpException; + /** * 请求 * @@ -154,21 +156,6 @@ public function __construct(App $app) $this->loadEnv($app); } - /** - * 加载环境参数 - * - * @param App $app 框架实例 - * @return void - */ - public function loadEnv(App $app) - { - $env = parse_ini_file($app->rootPath . '/.env', true); - if ($env === false) { - throw CoreHttpException('load env fail', 500); - } - $this->envParams = array_merge($_ENV, $env); - } - /** * 魔法函数__get. * @@ -292,4 +279,65 @@ public function env($value = '') } return ''; } + + /** + * 加载环境参数 + * + * @param App $app 框架实例 + * @return void + */ + public function loadEnv(App $app) + { + $env = parse_ini_file($app->rootPath . '/.env', true); + if ($env === false) { + throw CoreHttpException('load env fail', 500); + } + $this->envParams = array_merge($_ENV, $env); + } + + /** + * 参数验证 + * + * 支持必传参数验证,参数长度验证,参数类型验证 + * + * @param string $paramName 参数名 + * @param string $rule 规则 + * @return mixed + */ + public function check($paramName = '', $rule = '', $length = 0) + { + if (! is_int($length)) { + throw new CoreHttpException( + 400, + "length type is not int" + ); + } + + if ($rule === 'require') { + if (! empty($this->request($paramName))) { + return; + } + throw new CoreHttpException(404, "param {$paramName}"); + } + + if ($rule === 'length') { + if (strlen($this->request($paramName)) === $length) { + return; + } + throw new CoreHttpException( + 400, + "param {$paramName} length is not {$length}" + ); + } + + if ($rule === 'number') { + if (is_numeric($this->request($paramName))) { + return; + } + throw new CoreHttpException( + 400, + "{$paramName} type is not number" + ); + } + } } diff --git a/runtime/.gitignore b/runtime/.gitignore old mode 100644 new mode 100755 diff --git a/runtime/.gitkeep b/runtime/.gitkeep old mode 100644 new mode 100755