Skip to content

Commit 542f15e

Browse files
committed
重构&编写文档
1 parent 335d3bc commit 542f15e

File tree

8 files changed

+430
-539
lines changed

8 files changed

+430
-539
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@ Windwork 工具组件
22
===============
33

44
TODO
5+
6+
<br />
7+
<br />
8+
> ### 要了解更多?
9+
> - [官方完整文档首页](http://docs.windwork.org/manual/)
10+
> - [官方源码首页](https://github.com/windwork

docs/validator.md

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,66 @@
11
字符串格式验证类
22
======================
3+
本类用于验证字符串格式是否为预期的值。
4+
5+
支持的验证方法:
6+
7+
方法 | 函数说明
8+
---------------------------| ----------------
9+
Validator::date($str) | 是否为YYYY-mm-dd或YYY/mm/dd或YYYY年日期
10+
Validator::datetime($str) | 是否为到秒的时间格式(YYYY-mm-dd HH:ii:ss 或 YYYY/mm/dd HH:ii:ss
11+
Validator::year($str) | 参数类型是否为年的格式(1-32767)
12+
Validator::month($str) | 参数类型是否为月格式(1-12)
13+
Validator::day($str) | 参数类型是否为日期的日格式(1-31)
14+
Validator::hour($str) | 小时,0-23
15+
Validator::minute($str) | 分钟,0-59
16+
Validator::second($str) | 秒钟,0-59
17+
Validator::week($str) | 否为星期范围内的值
18+
Validator::email($str) | 邮箱
19+
Validator::equal($str) | 值等于(==)
20+
Validator::equalAll($str) | 值全等于(===)
21+
Validator::hex($str) | 是否为16进制字符
22+
Validator::idCard($str) | 中国身份证号码
23+
Validator::ip($str) | 是否为IP地址
24+
Validator::max($str, $max) | 值不大于 $max,如果$max为数组,结构为: <pre> $max = ['max' => 值, 'msg' => '匹配错误提示信息']</pre>
25+
Validator::min($str, $max) | 值不小于 $min,如果$min为数组,结构为: <pre> $min = ['min' => 值, 'msg' => '匹配错误提示信息']</pre>
26+
Validator::len($str, $len) | 值长度等于 $len,如果$len为数组,结构为: <pre> $min = ['min' => 值, 'msg' => '匹配错误提示信息']</pre>
27+
Validator::minLen($str, $minLen) | 值长度不小于 $minLen,如果传入数组,结构为: <pre> $minLen = ['minLen' => 值, 'msg' => '匹配错误提示信息']</pre>
28+
Validator::maxLen($str, $maxLen) | 值长度不大于 $maxLen,如果传入数组,结构为: <pre> $maxLen = ['maxLen' => 值, 'msg' => '匹配错误提示信息']</pre>
29+
Validator::preg($str, $preg) | 自定义验证规则,$preg 可直接传入正则,如果传入数组,结构为 <pre> ['preg' => '正则规则', 'msg' => '匹配错误提示信息']</pre>
30+
Validator::mobile($str) | 中国大陆手机号码
31+
Validator::money($str) | 货币,两位小数的浮点数,小数点前面必须有数字
32+
Validator::number($str) | 数字
33+
Validator::safeString($str) | 安全字符串(只包含字母、数字、下划线)
34+
Validator::url($str) | 网址
35+
336

437
## 批量验证
538

639
```
740
$rules = [
841
'user_name' => [
942
// 验证方法只有一个参数
10-
'isNotEmpty' => '请输入用户名',
11-
'isSafeString' => '用户名只允许输入字母、数字和下划线',
43+
'required' => '请输入用户名',
44+
'safeString' => '用户名只允许输入字母、数字和下划线',
1245
1346
// 验证方法需要多个参数
1447
'minLen' => ['msg' => '用户名不能小于3个字符', 'minLen' => 3],
15-
'maxLen' => ['msg' => '用户名不能超过20个字符', 'maxLen' => 20],
48+
'maxLen' => ['msg' => '用户名不能超过24个字符', 'maxLen' => 24],
1649
],
1750
'email' => [
18-
'isNotEmpty' => '请输入邮箱',
51+
'required' => '请输入邮箱',
1952
'email' => '邮箱格式错误',
2053
]
21-
]
22-
54+
];
55+
$data = [
56+
'user_name' => 'my_name_is_han_mei_mei',
57+
'password' => '123456',
58+
'email' => 'hanmeimei@windwork.org',
59+
];
60+
$validator = new \wf\util\Validator();
61+
if(!$validator->validate($data, $rules)) {
62+
// 如果匹配错误,获取错误信息并进行处理
63+
$err = $validator->getLastError();
64+
// do sth.
65+
}
2366
```

lib/Tree.php

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,40 @@
1515
* 树结点的结构 array('结点编号', '父结点编号', ... )
1616
*
1717
* 术语:
18-
* tree 树
19-
* subTree 子数
20-
* node 结点
21-
* degree 结点的度
22-
* leaf 叶子,度为0的结点
23-
* child 孩子,结点子树的根
24-
* parent 结点的上层结点
25-
* level 结点的层次
26-
* depth 数的深度,树中结点最大层次数
27-
* ancestor 祖先,树叶的顶层父结点
18+
* tree
19+
* subTree 子树
20+
* node 结点
21+
* degree 结点的度
22+
* leaf 叶子,度为0的结点
23+
* child 孩子,结点子树的根
24+
* parent 结点的上层结点
25+
* level 结点的层次
26+
* depth 数的深度,树中结点最大层次数
27+
* ancestor 祖先,树叶的顶层父结点
2828
* descendant 树的子孙,结点的祖先和子孙不包含结点 本身
29-
* path 路径,一个结点到达另一个结点经过的结点
29+
* path 路径,一个结点到达另一个结点经过的结点
3030
*
31-
* 这里用不上sibling、forest
31+
* 我们这个类没用上sibling、forest
3232
*
3333
* @package wf.util
3434
* @author cm <cmpan@qq.com>
3535
* @since 0.1.0
3636
*/
37-
class Tree {
37+
class Tree
38+
{
3839
/**
3940
* 要处理的结点的数组
4041
*
4142
* @var array
4243
*/
43-
protected $inNodes = array();
44+
protected $inNodes = [];
4445

4546
/**
4647
* 处理后的结点的数组
4748
*
4849
* @var array
4950
*/
50-
protected $outNodes = array();
51+
protected $outNodes = [];
5152

5253
/**
5354
* 结点中表示该结点的父结点id的属性名(下标)
@@ -76,59 +77,41 @@ class Tree {
7677
* @var int
7778
*/
7879
public $depth = 0;
79-
80-
//public function __construct() {}
81-
82-
public function setParentNodeIdKey($key) {
83-
$this->nodeParentIdKey = $key;
84-
}
85-
86-
public function setNodeIdKey($key) {
87-
$this->nodeIdKey = $key;
88-
}
8980

9081
/**
91-
* 填充要计算的数组
82+
* 在构造设置要计算的数组
9283
*
9384
* @param array $var
9485
* @param string $id
9586
* @param string $parentId
9687
*/
97-
public function set($vars, $id = 'id', $parentId = 'parent_id') {
98-
if(!is_array($vars)) throw new \Exception("错误的数据类型");
88+
public function __construct(array $vars, $id = 'id', $parentId = 'parent_id')
89+
{
9990
foreach ($vars as $var) {
10091
$this->inNodes[$var[$id]] = $var;
10192
}
10293

10394
$this->nodeIdKey = $id;
10495
$this->nodeParentIdKey = $parentId;
10596
}
106-
107-
/**
108-
* 添加结点
109-
*
110-
* @param array $node 结点
111-
* @param string $key = null 结点的下标
112-
*/
113-
public function add($node, $key = null) {
114-
$node = $key ? array($key => $node) : array($node);
115-
$this->inNodes = array_merge($this->inNodes, $node);
116-
}
11797

11898
/**
11999
* 获取子结点,子结点将按父结点id和结点id来排序
120100
*
121101
* @param int $id 结点的id
122102
* @param bool $isReturnSelf 是否返回自己
123-
* @param array $unReturnFields 不返回的属性,可设置:[isLeaf, ancestorIdArr, descendantIdArr, descendantId, childArr, isLastNode, icon]
103+
* @param array $unReturnFields 设置不返回的属性,可设置:[isLeaf, ancestorIdArr, descendantIdArr, descendantId, childArr, isLastNode, icon]
124104
* @return array
125105
*/
126-
public function get($id = 0, $isReturnSelf = true, array $unReturnFields = []) {
106+
public function get($id = 0, $isReturnSelf = true, array $unReturnFields = [])
107+
{
127108
// 层次排序
128-
$this->levelOrder(0);
109+
$this->parse(0);
129110

130111
// 设置缩进图标/形状
131-
empty($unReturnFields['icon']) && $this->setLevelIcon();
112+
if(empty($unReturnFields['icon'])) {
113+
$this->parseLevelIcon();
114+
}
132115

133116
$cats = $this->outNodes;
134117
if($id && isset($cats[$id])) {
@@ -157,10 +140,11 @@ public function get($id = 0, $isReturnSelf = true, array $unReturnFields = []) {
157140
}
158141

159142
/**
160-
* 设置树的层次缩进图标/形状
143+
* 提取树的层次缩进图标/形状
161144
*
162145
*/
163-
private function setLevelIcon() {
146+
private function parseLevelIcon()
147+
{
164148
$nodes = $this->outNodes;
165149
$fidName = $this->nodeParentIdKey;
166150

@@ -208,7 +192,8 @@ private function setLevelIcon() {
208192
*
209193
* @param int|string $id 结点id
210194
*/
211-
protected function levelOrder($id) {
195+
protected function parse($id)
196+
{
212197
foreach($this->inNodes as $node) {
213198
$nodeId = $node[$this->nodeIdKey]; // 结点ID
214199
$nodeFid = $node[$this->nodeParentIdKey]; // 结点的父结点的id
@@ -253,16 +238,11 @@ protected function levelOrder($id) {
253238
//$this->addChildMark($nodeId, $nodeFid);
254239
$this->depth = ($this->depth > $this->outNodes[$nodeId]['level']) ? $this->depth : $this->outNodes[$nodeId]['level'];
255240
if($nodeId) {
256-
$this->levelOrder($nodeId);
241+
$this->parse($nodeId);
257242
}
258243
//unset($this->inNodes[$nodeId]);
259244
}
260245
}
261246
}
262-
263-
public function __destruct() {
264-
unset($this->inNodes);
265-
unset($this->outNodes);
266-
}
267247
}
268248

0 commit comments

Comments
 (0)