Skip to content

Commit e522a9a

Browse files
committed
异常处理优化
1 parent 1fb1296 commit e522a9a

File tree

5 files changed

+88
-23
lines changed

5 files changed

+88
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Class PdsBusinessException
4+
* User: Lustre
5+
* Date: 2018/9/20
6+
* Time: 下午9:16
7+
*/
8+
9+
namespace DfaFilter\Exceptions;
10+
11+
use Exception;
12+
13+
class PdsBusinessException extends Exception
14+
{
15+
const EMPTY_CONTENT = 10001; // 空检测文本内容
16+
const EMPTY_WORD_POOL = 10002; // 空词库
17+
const CANNOT_FIND_FILE = 10003; // 找不到词库文件
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Class PdsSystemException
4+
* User: wanghui
5+
* Date: 2018/9/20
6+
* Time: 下午9:39
7+
*/
8+
9+
namespace DfaFilter\Exceptions;
10+
11+
use Exception;
12+
13+
class PdsSystemException extends Exception
14+
{
15+
16+
}

src/DfaFilter/HashMap.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php
2-
32
/**
43
* php构建哈希表类.
5-
* User: wanghui
4+
* User: Lustre
65
* Date: 17/3/9
76
* Time: 上午9:10
87
**/
@@ -99,7 +98,7 @@ public function values()
9998
/**
10099
* 将一个HashMap的值全部put到当前HashMap中
101100
*
102-
* @param $map
101+
* @param \DfaFilter\HashMap $map
103102
*/
104103
public function putAll($map)
105104
{
@@ -132,7 +131,7 @@ public function removeAll()
132131
*/
133132
public function containsValue($value)
134133
{
135-
while ($curValue = current($this->H_table)) {
134+
while ($curValue = current($this->hashTable)) {
136135
if ($curValue == $value) {
137136
return true;
138137
}

src/DfaFilter/SensitiveHelper.php

+30-19
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?php
2-
32
/**
43
* 敏感词类库.
5-
* User: wanghui
4+
* User: Lustre
65
* Date: 17/3/9
76
* Time: 上午9:11
87
*/
98
namespace DfaFilter;
109

10+
use DfaFilter\Exceptions\PdsBusinessException;
11+
1112
class SensitiveHelper
1213
{
1314
/**
@@ -51,16 +52,19 @@ public static function init()
5152
return self::$_instance;
5253
}
5354

55+
5456
/**
5557
* 构建铭感词树【文件模式】
5658
*
57-
* @param string $sensitiveWord
59+
* @param string $filepath
60+
*
5861
* @return $this
62+
* @throws \DfaFilter\Exceptions\PdsBusinessException
5963
*/
6064
public function setTreeByFile($filepath = '')
6165
{
62-
if (! file_exists($filepath)) {
63-
throw new \Exception('词库文件不存在');
66+
if (!file_exists($filepath)) {
67+
throw new PdsBusinessException('词库文件不存在', PdsBusinessException::CANNOT_FIND_FILE);
6468
}
6569

6670
// 词库树初始化
@@ -77,13 +81,15 @@ public function setTreeByFile($filepath = '')
7781
/**
7882
* 构建铭感词树【数组模式】
7983
*
80-
* @param string $sensitiveWord
84+
* @param null $sensitiveWords
85+
*
8186
* @return $this
87+
* @throws \DfaFilter\Exceptions\PdsBusinessException
8288
*/
8389
public function setTree($sensitiveWords = null)
8490
{
8591
if (empty($sensitiveWords)) {
86-
throw new \Exception('词库不能为空');
92+
throw new PdsBusinessException('词库不能为空', PdsBusinessException::EMPTY_WORD_POOL);
8793
}
8894

8995
$this->wordTree = new HashMap();
@@ -101,6 +107,7 @@ public function setTree($sensitiveWords = null)
101107
* @param int $matchType 匹配类型 [默认为最小匹配规则]
102108
* @param int $wordNum 需要获取的敏感词数量 [默认获取全部]
103109
* @return array
110+
* @throws \DfaFilter\Exceptions\PdsSystemException
104111
*/
105112
public function getBadWord($content, $matchType = 1, $wordNum = 0)
106113
{
@@ -162,29 +169,26 @@ public function getBadWord($content, $matchType = 1, $wordNum = 0)
162169
return $badWordList;
163170
}
164171

165-
166172
/**
167173
* 替换敏感字字符
168174
*
169-
* @param $wordMap
170-
* @param $content
171-
* @param $replaceChar
175+
* @param $content
176+
* @param string $replaceChar
172177
* @param string $sTag
173178
* @param string $eTag
174-
* @param int $matchType
179+
* @param int $matchType
180+
*
175181
* @return mixed
182+
* @throws \DfaFilter\Exceptions\PdsBusinessException
183+
* @throws \DfaFilter\Exceptions\PdsSystemException
176184
*/
177185
public function replace($content, $replaceChar = '', $sTag = '', $eTag = '', $matchType = 1)
178186
{
179187
if (empty($content)) {
180-
throw new \Exception('请填写检测的内容');
188+
throw new PdsBusinessException('请填写检测的内容', PdsBusinessException::EMPTY_CONTENT);
181189
}
182190

183-
if (empty(self::$badWordList)) {
184-
$badWordList = $this->getBadWord($content, $matchType);
185-
} else {
186-
$badWordList = self::$badWordList;
187-
}
191+
$badWordList = self::$badWordList ? self::$badWordList : $this->getBadWord($content, $matchType);
188192

189193
// 未检测到敏感词,直接返回
190194
if (empty($badWordList)) {
@@ -200,7 +204,14 @@ public function replace($content, $replaceChar = '', $sTag = '', $eTag = '', $ma
200204
return $content;
201205
}
202206

203-
// 被检测内容是否合法
207+
/**
208+
* 被检测内容是否合法
209+
*
210+
* @param $content
211+
*
212+
* @return bool
213+
* @throws \DfaFilter\Exceptions\PdsSystemException
214+
*/
204215
public function islegal($content)
205216
{
206217
$this->contentLength = mb_strlen($content, 'utf-8');

src/DfaFilter/functions.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace DfaFilter;
3+
4+
use DfaFilter\Exceptions\PdsSystemException;
5+
6+
/**
7+
* @param $str
8+
* @param null $encoding
9+
*
10+
* @return int
11+
* @throws \DfaFilter\Exceptions\PdsSystemException
12+
*/
13+
function mb_strlen($str, $encoding = null)
14+
{
15+
$length = \mb_strlen($str, $encoding);
16+
if ($length === false) {
17+
throw new PdsSystemException(' encoding 无效');
18+
}
19+
20+
return $length;
21+
}

0 commit comments

Comments
 (0)