Skip to content

Commit 2c28d6d

Browse files
committed
代码初步优化
1 parent b4ab85a commit 2c28d6d

File tree

4 files changed

+225
-179
lines changed

4 files changed

+225
-179
lines changed

Lib/HashMap.php

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?php
2+
3+
/**
4+
* php构建哈希表类.
5+
* User: wanghui
6+
* Date: 17/3/9
7+
* Time: 上午9:10
8+
**/
9+
10+
namespace Lib;
11+
12+
class HashMap
13+
{
14+
/**
15+
* 哈希表变量
16+
*
17+
* @var array
18+
*/
19+
protected $hashTable;
20+
21+
public function __construct()
22+
{
23+
$this->hashTable = array();
24+
}
25+
26+
/**
27+
* 向HashMap中添加一个键值对
28+
*
29+
* @param $key
30+
* @param $value
31+
* @return mixed|null
32+
*/
33+
public function put($key, $value)
34+
{
35+
if (! array_key_exists($key, $this->hashTable)) {
36+
$this->hashTable[$key] = $value;
37+
return null;
38+
} else {
39+
$_temp = $this->hashTable[$key];
40+
$this->hashTable[$key] = $value;
41+
return $_temp;
42+
}
43+
}
44+
45+
/**
46+
* 根据key获取对应的value
47+
*
48+
* @param $key
49+
* @return mixed|null
50+
*/
51+
public function get($key)
52+
{
53+
if (array_key_exists($key, $this->hashTable))
54+
return $this->hashTable[$key];
55+
56+
return null;
57+
}
58+
59+
/**
60+
* 删除指定key的键值对
61+
*
62+
* @param $key
63+
* @return mixed|null
64+
*/
65+
public function remove($key)
66+
{
67+
$temp_table = array();
68+
if (array_key_exists($key, $this->hashTable)) {
69+
$tempValue = $this->hashTable[$key];
70+
while ($curValue = current($this->hashTable)) {
71+
if (! (key($this->hashTable) == $key))
72+
$temp_table[key($this->hashTable)] = $curValue;
73+
next($this->hashTable);
74+
}
75+
$this->hashTable = null;
76+
$this->hashTable = $temp_table;
77+
return $tempValue;
78+
}
79+
80+
return null;
81+
}
82+
83+
/**
84+
* 获取HashMap的所有键值
85+
*
86+
* @return array
87+
*/
88+
public function keys()
89+
{
90+
return array_keys($this->hashTable);
91+
}
92+
93+
/**
94+
* 获取HashMap的所有value值
95+
*
96+
* @return array
97+
*/
98+
public function values()
99+
{
100+
return array_values($this->hashTable);
101+
}
102+
103+
/**
104+
* 将一个HashMap的值全部put到当前HashMap中
105+
*
106+
* @param $map
107+
*/
108+
public function putAll($map)
109+
{
110+
if (! $map->isEmpty() && $map->size() > 0) {
111+
$keys = $map->keys();
112+
foreach ($keys as $key) {
113+
$this->put($key, $map->get($key));
114+
}
115+
}
116+
117+
return ;
118+
}
119+
120+
/**
121+
* 移除HashMap中所有元素
122+
*
123+
* @return bool
124+
*/
125+
public function removeAll()
126+
{
127+
$this->hashTable = null;
128+
return true;
129+
}
130+
131+
/**
132+
* 判断HashMap中是否包含指定的值
133+
*
134+
* @param $value
135+
* @return bool
136+
*/
137+
public function containsValue($value)
138+
{
139+
while ($curValue = current($this->H_table)) {
140+
if ($curValue == $value) {
141+
return true;
142+
}
143+
next($this->hashTable);
144+
}
145+
146+
return false;
147+
}
148+
149+
/**
150+
* 判断HashMap中是否包含指定的键key
151+
*
152+
* @param $key
153+
* @return bool
154+
*/
155+
public function containsKey($key)
156+
{
157+
if (array_key_exists($key, $this->hashTable)) {
158+
return true;
159+
} else {
160+
return false;
161+
}
162+
}
163+
164+
/**
165+
* 获取HashMap中元素个数
166+
*
167+
* @return int
168+
*/
169+
public function size()
170+
{
171+
return count($this->hashTable);
172+
}
173+
174+
/**
175+
* 判断HashMap是否为空
176+
*
177+
* @return bool
178+
*/
179+
public function isEmpty()
180+
{
181+
return (count($this->hashTable) == 0);
182+
}
183+
184+
185+
}

class/LSensitiveWordFilter.php renamed to Lib/SensitiveWordFilterHelper.php

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,22 @@
66
* Date: 17/3/9
77
* Time: 上午9:11
88
*/
9-
require_once 'LHashMap.php';
9+
namespace Lib;
1010

11-
class LSensitiveWordFilter
11+
use Lib\HashMap;
12+
13+
class SensitiveWordFilterHelper
1214
{
1315

1416
private static $_instance;
1517

1618
public static $badWordList = array();
1719

18-
//获取单例
20+
/**
21+
* 获取单例
22+
*
23+
* @return LSensitiveWordFilter
24+
*/
1925
public static function init()
2026
{
2127
if (!self::$_instance instanceof self) {
@@ -26,18 +32,19 @@ public static function init()
2632

2733
/**
2834
* 将敏感词加入到HashMap中
35+
*
2936
* @param $sensitiveWord
30-
* @return obj
31-
**/
37+
* @return HashMap
38+
*/
3239
public function setHashMap($sensitiveWord)
3340
{
34-
$wordMap = new LHashMap();
41+
$wordMap = new HashMap();
3542
foreach ($sensitiveWord as $word) {
3643
$nowMap = $wordMap;
3744
$wordLength = mb_strlen($word, 'utf-8');
3845
for ($i = 0; $i < $wordLength; $i++) {
3946
$keyChar = mb_substr($word, $i, 1, 'utf-8');
40-
//获取
47+
// 获取
4148
$tempMap = $nowMap->get($keyChar);
4249
if ($tempMap) {
4350
$nowMap = $tempMap;
@@ -60,9 +67,12 @@ public function setHashMap($sensitiveWord)
6067

6168
/**
6269
* 获取文字中的敏感词
70+
*
71+
* @param $wordMap
6372
* @param $content
64-
* @return obj
65-
**/
73+
* @param int $matchType
74+
* @return array
75+
*/
6676
public function getSensitiveWord($wordMap, $content, $matchType = 1)
6777
{
6878
$contentLength = mb_strlen($content, 'utf-8');
@@ -73,39 +83,38 @@ public function getSensitiveWord($wordMap, $content, $matchType = 1)
7383
$tempMap = $wordMap;
7484
for ($i = $len; $i < $contentLength; $i++) {
7585
$keyChar = mb_substr($content, $i, 1, 'utf-8');
76-
//获取指定key
86+
// 获取指定key
7787
$nowMap = $tempMap->get($keyChar);
78-
//存在,则判断是否为最后一个
88+
// 存在,则判断是否为最后一个
7989
if ($nowMap != null) {
8090
$tempMap = $nowMap;
81-
//找到相应key,偏移量+1
91+
// 找到相应key,偏移量+1
8292
$matchFlag++;
83-
//如果为最后一个匹配规则,结束循环,返回匹配标识数
93+
// 如果为最后一个匹配规则,结束循环,返回匹配标识数
8494
if ($nowMap->get("isEnd") == '1') {
8595
$flag = true;
8696
if ($matchType == 1) {
87-
//最小规则,直接返回
97+
// 最小规则,直接返回
8898
break;
8999
} else {
90-
//最大规则还需继续查找
100+
// 最大规则还需继续查找
91101
continue;
92102
}
93103
} else {
94104
continue;
95105
}
96106
} else {
97-
//不存在,直接返回
107+
// 不存在,直接返回
98108
break;
99109
}
100110
}
101111
if (!$flag) {
102112
$matchFlag = 0;
103113
}
104-
105-
//未找到相应key
114+
// 未找到相应key
106115
if ($matchFlag > 0) {
107116
$badWordList[] = mb_substr($content, $len, $matchFlag, 'utf-8');
108-
//需匹配内容标志位往后移
117+
// 需匹配内容标志位往后移
109118
$len = $len + $matchFlag - 1;
110119
} else {
111120
continue;
@@ -115,7 +124,17 @@ public function getSensitiveWord($wordMap, $content, $matchType = 1)
115124
}
116125

117126

118-
//替换敏感字字符
127+
/**
128+
* 替换敏感字字符
129+
*
130+
* @param $wordMap
131+
* @param $content
132+
* @param $replaceChar
133+
* @param string $sTag
134+
* @param string $eTag
135+
* @param int $matchType
136+
* @return mixed
137+
*/
119138
public function replaceSensitiveWord($wordMap, $content, $replaceChar, $sTag = '', $eTag = '', $matchType = 1)
120139
{
121140
if (empty(self::$badWordList)) {

0 commit comments

Comments
 (0)