Skip to content

Commit

Permalink
异常处理优化
Browse files Browse the repository at this point in the history
  • Loading branch information
FireLustre committed Sep 20, 2018
1 parent 1fb1296 commit e522a9a
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 23 deletions.
18 changes: 18 additions & 0 deletions src/DfaFilter/Exceptions/PdsBusinessException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Class PdsBusinessException
* User: Lustre
* Date: 2018/9/20
* Time: 下午9:16
*/

namespace DfaFilter\Exceptions;

use Exception;

class PdsBusinessException extends Exception
{
const EMPTY_CONTENT = 10001; // 空检测文本内容
const EMPTY_WORD_POOL = 10002; // 空词库
const CANNOT_FIND_FILE = 10003; // 找不到词库文件
}
16 changes: 16 additions & 0 deletions src/DfaFilter/Exceptions/PdsSystemException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Class PdsSystemException
* User: wanghui
* Date: 2018/9/20
* Time: 下午9:39
*/

namespace DfaFilter\Exceptions;

use Exception;

class PdsSystemException extends Exception
{

}
7 changes: 3 additions & 4 deletions src/DfaFilter/HashMap.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php

/**
* php构建哈希表类.
* User: wanghui
* User: Lustre
* Date: 17/3/9
* Time: 上午9:10
**/
Expand Down Expand Up @@ -99,7 +98,7 @@ public function values()
/**
* 将一个HashMap的值全部put到当前HashMap中
*
* @param $map
* @param \DfaFilter\HashMap $map
*/
public function putAll($map)
{
Expand Down Expand Up @@ -132,7 +131,7 @@ public function removeAll()
*/
public function containsValue($value)
{
while ($curValue = current($this->H_table)) {
while ($curValue = current($this->hashTable)) {
if ($curValue == $value) {
return true;
}
Expand Down
49 changes: 30 additions & 19 deletions src/DfaFilter/SensitiveHelper.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php

/**
* 敏感词类库.
* User: wanghui
* User: Lustre
* Date: 17/3/9
* Time: 上午9:11
*/
namespace DfaFilter;

use DfaFilter\Exceptions\PdsBusinessException;

class SensitiveHelper
{
/**
Expand Down Expand Up @@ -51,16 +52,19 @@ public static function init()
return self::$_instance;
}


/**
* 构建铭感词树【文件模式】
*
* @param string $sensitiveWord
* @param string $filepath
*
* @return $this
* @throws \DfaFilter\Exceptions\PdsBusinessException
*/
public function setTreeByFile($filepath = '')
{
if (! file_exists($filepath)) {
throw new \Exception('词库文件不存在');
if (!file_exists($filepath)) {
throw new PdsBusinessException('词库文件不存在', PdsBusinessException::CANNOT_FIND_FILE);
}

// 词库树初始化
Expand All @@ -77,13 +81,15 @@ public function setTreeByFile($filepath = '')
/**
* 构建铭感词树【数组模式】
*
* @param string $sensitiveWord
* @param null $sensitiveWords
*
* @return $this
* @throws \DfaFilter\Exceptions\PdsBusinessException
*/
public function setTree($sensitiveWords = null)
{
if (empty($sensitiveWords)) {
throw new \Exception('词库不能为空');
throw new PdsBusinessException('词库不能为空', PdsBusinessException::EMPTY_WORD_POOL);
}

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


/**
* 替换敏感字字符
*
* @param $wordMap
* @param $content
* @param $replaceChar
* @param $content
* @param string $replaceChar
* @param string $sTag
* @param string $eTag
* @param int $matchType
* @param int $matchType
*
* @return mixed
* @throws \DfaFilter\Exceptions\PdsBusinessException
* @throws \DfaFilter\Exceptions\PdsSystemException
*/
public function replace($content, $replaceChar = '', $sTag = '', $eTag = '', $matchType = 1)
{
if (empty($content)) {
throw new \Exception('请填写检测的内容');
throw new PdsBusinessException('请填写检测的内容', PdsBusinessException::EMPTY_CONTENT);
}

if (empty(self::$badWordList)) {
$badWordList = $this->getBadWord($content, $matchType);
} else {
$badWordList = self::$badWordList;
}
$badWordList = self::$badWordList ? self::$badWordList : $this->getBadWord($content, $matchType);

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

// 被检测内容是否合法
/**
* 被检测内容是否合法
*
* @param $content
*
* @return bool
* @throws \DfaFilter\Exceptions\PdsSystemException
*/
public function islegal($content)
{
$this->contentLength = mb_strlen($content, 'utf-8');
Expand Down
21 changes: 21 additions & 0 deletions src/DfaFilter/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace DfaFilter;

use DfaFilter\Exceptions\PdsSystemException;

/**
* @param $str
* @param null $encoding
*
* @return int
* @throws \DfaFilter\Exceptions\PdsSystemException
*/
function mb_strlen($str, $encoding = null)
{
$length = \mb_strlen($str, $encoding);
if ($length === false) {
throw new PdsSystemException(' encoding 无效');
}

return $length;
}

0 comments on commit e522a9a

Please sign in to comment.