Skip to content

Commit

Permalink
feat(request): add request param check ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
TIGERB committed May 21, 2017
1 parent f78f870 commit ae10aa9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 17 deletions.
8 changes: 6 additions & 2 deletions app/demo/controllers/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace App\Demo\Controllers;

use Framework\App;
use Framework\Loger;
use Framework\Logger;

/**
* Index Controller
Expand Down Expand Up @@ -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')
];
Expand Down
78 changes: 63 additions & 15 deletions framework/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Framework;

use Framework\Exceptions\CoreHttpException;

/**
* 请求
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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"
);
}
}
}
Empty file modified runtime/.gitignore
100644 → 100755
Empty file.
Empty file modified runtime/.gitkeep
100644 → 100755
Empty file.

0 comments on commit ae10aa9

Please sign in to comment.