Skip to content

Commit 2a6b523

Browse files
author
yang qingwu
committed
优化变量转换后的数组的获取方式,从引用改为函数方式
1 parent 8fd556c commit 2a6b523

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

Document.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ if (Validator::has_fails()) {
302302
  2.一个严格的程序员往往需要明确变量类型,只有明确了变量类型,才能写得一手好代码
303303

304304
  基于以上或更多的场景,往往我们希望在参数校验完毕后,转换变量类型,得到我们想要的数组。那么 to_type 就由此而来!
305+
306+
  Validator::data() 方法返回处理后的参数数组!
305307

306308

307309
``` php
@@ -311,7 +313,7 @@ Validator::make($data, [
311313
if (Validator::has_fails()) {
312314
echo Validator::error_msg();
313315
} else {
314-
var_dump($data);
316+
var_dump(Validator::data());
315317
}
316318
```
317319

Validator.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ class Validator
1818
*/
1919
private static $error_msg = "";
2020

21+
/**
22+
* 参数校验后被处理过的输入参数
23+
*
24+
* @var array
25+
*/
26+
private static $data = [];
27+
2128
/**
2229
* 参数校验是否不通过
2330
*
@@ -38,6 +45,16 @@ public static function error_msg()
3845
return self::$error_msg;
3946
}
4047

48+
/**
49+
* 获取被处理过的请求参数
50+
*
51+
* @return array
52+
*/
53+
public static function data()
54+
{
55+
return self::$data;
56+
}
57+
4158
/**
4259
* 初始化数据
4360
*
@@ -56,7 +73,7 @@ private static function init()
5673
* @param array $rules 参数校验规则
5774
* @param array $messages 自定义文案
5875
*/
59-
public static function make(array &$data, array $rules, array $messages = [])
76+
public static function make(array $data, array $rules, array $messages = [])
6077
{
6178
self::init();
6279
$validator = new Common\Validator($data, $rules, $messages);
@@ -68,6 +85,6 @@ public static function make(array &$data, array $rules, array $messages = [])
6885
self::$has_fails = $validator->has_fails();
6986
self::$error_msg = $validator->err_msg();
7087

71-
$data = $validator->data();
88+
self::$data = $validator->data();
7289
}
7390
}

0 commit comments

Comments
 (0)