Skip to content

Commit 390d4c6

Browse files
committed
some update, optimization and improvement
1 parent fb1809f commit 390d4c6

File tree

7 files changed

+192
-232
lines changed

7 files changed

+192
-232
lines changed

document.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,20 +236,22 @@ $valid = Validation::make($_POST,[
236236

237237
validator | description | rule example
238238
----------|-------------|------------
239-
`int` | validate int | ....
239+
`int` | validate int | `['userId', 'int']`
240240
`number` | validate number | ....
241241
`bool` | validate bool | ....
242242
`float` | validate float | ....
243+
`string` | validate string. support length check (like `length`) | `['name', 'string']`, `['name', 'string', 'min'=>4, 'max'=>16]`
243244
`regexp` | validate regexp | ....
244245
`url` | validate url | ....
245246
`email` | validate email | ....
246247
`ip` | validate ip | ....
247248
`required` | validate required | `['tagId,userId', 'required' ]`
248-
`length` | validate length | ....
249-
`size` | validate size | `['tagId', 'size', 'min'=>4, 'max'=>567]`
249+
`size` | validate size, can validate `int`, `string`, `array` | `['tagId', 'size', 'min'=>4, 'max'=>567]` `['name', 'size', 'max' => 16]`
250+
`range` | alias `size` | same `size`
251+
`length` | like `size`, but only validate `string`, `array` length | ....
250252
`min` | validate min | `['title', 'min', 'value' => 40],]`
251253
`max` | validate max | ....
252254
`in` | validate in | `['id', 'in', 'value' => [1,2,3],]`
253-
`string` | validate string | ....
255+
`compare` | compare attr | `['passwd', 'compare', 'repasswd']`
254256
`isArray` | validate is Array | ....
255-
`callback` | validate by custom callback | ....
257+
`callback` | validate by custom callback | ....

example/DataModel.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
*/
88
class DataModel
99
{
10-
1110
use \inhere\validate\ValidationTrait;
1211

1312
protected $data = [];
1413

15-
1614
/**
1715
* @param array $data
1816
* @return $this
@@ -23,6 +21,4 @@ public function setData($data)
2321

2422
return $this;
2523
}
26-
27-
28-
}
24+
}

example/index.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,27 @@
1515
});
1616

1717
$data = [
18+
// 'userId' => 234,
1819
'userId' => 'sdfdffffffffff',
1920
'tagId' => '234535',
2021
// 'freeTime' => 'sdfdffffffffff',
2122
'distanceRequire' => 'sdfdffffffffff',
2223
'note' => '',
24+
'name' => 'john',
25+
'passwd' => 'password',
26+
'repasswd' => 'repassword',
2327
'insertTime' => '1456767657',
2428
'lastReadTime' => '1456767657',
2529
];
2630

2731
$rules = [
2832
['tagId,userId,freeTime', 'required', 'msg' => '{attr} is required!'],// set message
33+
['tagId,userId,freeTime', 'number'],
2934
['note', 'email', 'skipOnEmpty' => false], // set skipOnEmpty is false.
3035
['insertTime', 'email', 'scene' => 'otherScene' ],// set scene. will is not validate it on default.
31-
['tagId', 'size', 'min'=>4, 'max'=>567], // 4<= tagId <=567
36+
['tagId', 'size', 'max'=>567, 'min'=>4, ], // 4<= tagId <=567
37+
['passwd', 'compare', 'repasswd'], //
38+
3239
['freeTime', 'size', 'min'=>4, 'max'=>567, 'when' => function($data, $valid) {
3340
echo " use when pre-check\n";
3441

@@ -60,14 +67,14 @@
6067
$model->getErrors()
6168
);
6269

63-
/*
70+
6471
echo "--------------\n";
6572
echo "use Validation\n";
6673

67-
$valid = \inhere\validate\Validation::make($data, $rules)->validate();
74+
$valid = \inhere\validate\Validation::make($data, $rules)->validate([], false);
6875

6976
var_dump(
7077
$valid->all(),
7178
$valid->getErrors()
7279
);
73-
*/
80+

src/StrHelper.php

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,6 @@ public static function regexVerify($value,$rule)
4141
return preg_match($rule,$value)===1;
4242
}
4343

44-
/**
45-
* 检查字符串是否是正确的变量名
46-
* @param $string
47-
* @return bool
48-
*/
49-
public static function isVarName($string)
50-
{
51-
return preg_match('@^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*@i', $string)===1;
52-
}
53-
5444
/**
5545
* 计算字符长度
5646
* @param [type] $str
@@ -75,107 +65,6 @@ public static function length($str)
7565
return count($arr[0]);
7666
}
7767

78-
/**
79-
* @from web
80-
* 可以统计中文字符串长度的函数
81-
* @param string $str 要计算长度的字符串
82-
* @internal param bool $type 计算长度类型,0(默认)表示一个中文算一个字符,1表示一个中文算两个字符
83-
* @return int
84-
*/
85-
public static function abs_length($str)
86-
{
87-
if (empty($str)){
88-
return 0;
89-
}
90-
91-
if (function_exists('mb_strwidth')){
92-
return mb_strwidth($str,'utf-8');
93-
}
94-
95-
if (function_exists('mb_strlen')){
96-
return mb_strlen($str,'utf-8');
97-
}
98-
99-
preg_match_all('/./u', $str, $ar);
100-
return count($ar[0]);
101-
}
102-
103-
/**
104-
* @from web
105-
* utf-8编码下截取中文字符串,参数可以参照substr函数
106-
* @param string $str 要进行截取的字符串
107-
* @param int $start 要进行截取的开始位置,负数为反向截取
108-
* @param int $end 要进行截取的长度
109-
* @return string
110-
*/
111-
public static function utf8_substr($str,$start=0,$end=null)
112-
{
113-
if (empty($str)){
114-
return false;
115-
}
116-
117-
if (function_exists('mb_substr')){
118-
if (func_num_args() >= 3) {
119-
$end = func_get_arg(2);
120-
121-
return mb_substr($str,$start,$end,'utf-8');
122-
}
123-
124-
mb_internal_encoding('UTF-8');
125-
126-
return mb_substr($str,$start);
127-
128-
}
129-
130-
$null = '';
131-
preg_match_all('/./u', $str, $ar);
132-
133-
if (func_num_args() >= 3) {
134-
$end = func_get_arg(2);
135-
136-
return implode($null, array_slice($ar[0],$start,$end));
137-
}
138-
139-
return implode($null, array_slice($ar[0],$start));
140-
}
141-
142-
143-
/**
144-
* @from web
145-
* 中文截取,支持gb2312,gbk,utf-8,big5 *
146-
* @param string $str 要截取的字串
147-
* @param int $start 截取起始位置
148-
* @param int $length 截取长度
149-
* @param string $charset utf-8|gb2312|gbk|big5 编码
150-
* @param bool $suffix 是否加尾缀
151-
* @return string
152-
*/
153-
public static function zh_substr($str, $start=0, $length, $charset = 'utf-8', $suffix=true)
154-
{
155-
if (function_exists('mb_substr'))
156-
{
157-
if (mb_strlen($str, $charset) <= $length) {
158-
return $str;
159-
}
160-
161-
$slice = mb_substr($str, $start, $length, $charset);
162-
} else {
163-
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
164-
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
165-
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
166-
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
167-
168-
preg_match_all($re[$charset], $str, $match);
169-
if (count($match[0]) <= $length) {
170-
return $str;
171-
}
172-
173-
$slice = implode('',array_slice($match[0], $start, $length));
174-
}
175-
176-
return (bool)$suffix ? $slice. '' : $slice;
177-
}
178-
17968
/**
18069
* @param $str
18170
* @return bool|string

src/Validation.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@
1111
/**
1212
* Class Validation
1313
* @package inhere\validate
14+
*
15+
* usage:
16+
*
17+
* $vd = Validation::make($_POST, [
18+
* ['tagId,userId,name,email,freeTime', 'required'],
19+
* ['email', 'email'],
20+
* ['userId', 'number'],
21+
* ['name', function ($name) {
22+
* return preg_match('/[a-z]\w{2,12}/', $name);
23+
* }],
24+
* ])->validate();
25+
*
26+
* $vd->hasError();// bool
27+
* $vd->firstError(); // get first error message.
28+
*
1429
*/
1530
class Validation
1631
{
@@ -31,10 +46,12 @@ class Validation
3146
* @param string $scene
3247
* @param bool $startValidate 立即开始验证
3348
*/
34-
public function __construct(array $data=[], array $rules = [], array $attrTrans = [], $scene='', $startValidate=false)
49+
public function __construct(array $data=[], array $rules = [], array $attrTrans = [], $scene = '', $startValidate = false)
3550
{
3651
$this->data = $data;
37-
$this->setRules($rules)->setScene($scene)->setAttrTrans($attrTrans);
52+
$this->setRules($rules)
53+
->setScene($scene)
54+
->setAttrTrans($attrTrans);
3855

3956
if ( $startValidate ) {
4057
$this->validate();

0 commit comments

Comments
 (0)