Skip to content

Commit

Permalink
优化变量转换后的数组的获取方式,从引用改为函数方式
Browse files Browse the repository at this point in the history
  • Loading branch information
yang qingwu committed Nov 23, 2016
1 parent 8fd556c commit 2a6b523
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Document.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ if (Validator::has_fails()) {
  2.一个严格的程序员往往需要明确变量类型,只有明确了变量类型,才能写得一手好代码

  基于以上或更多的场景,往往我们希望在参数校验完毕后,转换变量类型,得到我们想要的数组。那么 to_type 就由此而来!

  Validator::data() 方法返回处理后的参数数组!


``` php
Expand All @@ -311,7 +313,7 @@ Validator::make($data, [
if (Validator::has_fails()) {
echo Validator::error_msg();
} else {
var_dump($data);
var_dump(Validator::data());
}
```

Expand Down
21 changes: 19 additions & 2 deletions Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class Validator
*/
private static $error_msg = "";

/**
* 参数校验后被处理过的输入参数
*
* @var array
*/
private static $data = [];

/**
* 参数校验是否不通过
*
Expand All @@ -38,6 +45,16 @@ public static function error_msg()
return self::$error_msg;
}

/**
* 获取被处理过的请求参数
*
* @return array
*/
public static function data()
{
return self::$data;
}

/**
* 初始化数据
*
Expand All @@ -56,7 +73,7 @@ private static function init()
* @param array $rules 参数校验规则
* @param array $messages 自定义文案
*/
public static function make(array &$data, array $rules, array $messages = [])
public static function make(array $data, array $rules, array $messages = [])
{
self::init();
$validator = new Common\Validator($data, $rules, $messages);
Expand All @@ -68,6 +85,6 @@ public static function make(array &$data, array $rules, array $messages = [])
self::$has_fails = $validator->has_fails();
self::$error_msg = $validator->err_msg();

$data = $validator->data();
self::$data = $validator->data();
}
}

0 comments on commit 2a6b523

Please sign in to comment.