11<?php
2-
32/**
43 * 敏感词类库.
5- * User: wanghui
4+ * User: Lustre
65 * Date: 17/3/9
76 * Time: 上午9:11
87 */
98namespace DfaFilter ;
109
10+ use DfaFilter \Exceptions \PdsBusinessException ;
11+
1112class 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 ' );
0 commit comments