1
1
<?php
2
-
3
2
/**
4
3
* 敏感词类库.
5
- * User: wanghui
4
+ * User: Lustre
6
5
* Date: 17/3/9
7
6
* Time: 上午9:11
8
7
*/
9
8
namespace DfaFilter ;
10
9
10
+ use DfaFilter \Exceptions \PdsBusinessException ;
11
+
11
12
class SensitiveHelper
12
13
{
13
14
/**
@@ -51,16 +52,19 @@ public static function init()
51
52
return self ::$ _instance ;
52
53
}
53
54
55
+
54
56
/**
55
57
* 构建铭感词树【文件模式】
56
58
*
57
- * @param string $sensitiveWord
59
+ * @param string $filepath
60
+ *
58
61
* @return $this
62
+ * @throws \DfaFilter\Exceptions\PdsBusinessException
59
63
*/
60
64
public function setTreeByFile ($ filepath = '' )
61
65
{
62
- if (! file_exists ($ filepath )) {
63
- throw new \ Exception ('词库文件不存在 ' );
66
+ if (!file_exists ($ filepath )) {
67
+ throw new PdsBusinessException ('词库文件不存在 ' , PdsBusinessException:: CANNOT_FIND_FILE );
64
68
}
65
69
66
70
// 词库树初始化
@@ -77,13 +81,15 @@ public function setTreeByFile($filepath = '')
77
81
/**
78
82
* 构建铭感词树【数组模式】
79
83
*
80
- * @param string $sensitiveWord
84
+ * @param null $sensitiveWords
85
+ *
81
86
* @return $this
87
+ * @throws \DfaFilter\Exceptions\PdsBusinessException
82
88
*/
83
89
public function setTree ($ sensitiveWords = null )
84
90
{
85
91
if (empty ($ sensitiveWords )) {
86
- throw new \ Exception ('词库不能为空 ' );
92
+ throw new PdsBusinessException ('词库不能为空 ' , PdsBusinessException:: EMPTY_WORD_POOL );
87
93
}
88
94
89
95
$ this ->wordTree = new HashMap ();
@@ -101,6 +107,7 @@ public function setTree($sensitiveWords = null)
101
107
* @param int $matchType 匹配类型 [默认为最小匹配规则]
102
108
* @param int $wordNum 需要获取的敏感词数量 [默认获取全部]
103
109
* @return array
110
+ * @throws \DfaFilter\Exceptions\PdsSystemException
104
111
*/
105
112
public function getBadWord ($ content , $ matchType = 1 , $ wordNum = 0 )
106
113
{
@@ -162,29 +169,26 @@ public function getBadWord($content, $matchType = 1, $wordNum = 0)
162
169
return $ badWordList ;
163
170
}
164
171
165
-
166
172
/**
167
173
* 替换敏感字字符
168
174
*
169
- * @param $wordMap
170
- * @param $content
171
- * @param $replaceChar
175
+ * @param $content
176
+ * @param string $replaceChar
172
177
* @param string $sTag
173
178
* @param string $eTag
174
- * @param int $matchType
179
+ * @param int $matchType
180
+ *
175
181
* @return mixed
182
+ * @throws \DfaFilter\Exceptions\PdsBusinessException
183
+ * @throws \DfaFilter\Exceptions\PdsSystemException
176
184
*/
177
185
public function replace ($ content , $ replaceChar = '' , $ sTag = '' , $ eTag = '' , $ matchType = 1 )
178
186
{
179
187
if (empty ($ content )) {
180
- throw new \ Exception ('请填写检测的内容 ' );
188
+ throw new PdsBusinessException ('请填写检测的内容 ' , PdsBusinessException:: EMPTY_CONTENT );
181
189
}
182
190
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 );
188
192
189
193
// 未检测到敏感词,直接返回
190
194
if (empty ($ badWordList )) {
@@ -200,7 +204,14 @@ public function replace($content, $replaceChar = '', $sTag = '', $eTag = '', $ma
200
204
return $ content ;
201
205
}
202
206
203
- // 被检测内容是否合法
207
+ /**
208
+ * 被检测内容是否合法
209
+ *
210
+ * @param $content
211
+ *
212
+ * @return bool
213
+ * @throws \DfaFilter\Exceptions\PdsSystemException
214
+ */
204
215
public function islegal ($ content )
205
216
{
206
217
$ this ->contentLength = mb_strlen ($ content , 'utf-8 ' );
0 commit comments