Skip to content

Commit 253c299

Browse files
committed
Added reset() and details.
1 parent 72c457f commit 253c299

File tree

2 files changed

+81
-18
lines changed

2 files changed

+81
-18
lines changed

app/ProfanityFilter.php

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ProfanityFilter
88

99
protected $badWords = [];
1010

11-
protected $censorChecks = [];
11+
protected $filterChecks = [];
1212

1313
protected $replaceFullWords = true;
1414

@@ -20,22 +20,33 @@ class ProfanityFilter
2020

2121
protected $config = [];
2222

23+
protected $filteredStrings = [];
24+
25+
protected $wasFiltered = false;
26+
2327
public function __construct($config, $badWordsArray)
2428
{
2529
$this->config = $config;
2630

2731
$this->strReplace = $this->config['strReplace'];
2832

29-
$this->replaceFullWords($this->config['replaceFullWords']);
30-
31-
$this->replaceWith($this->config['replaceWith']);
33+
$this->reset();
3234

3335
$this->badWords = array_merge(
3436
$this->config['defaults'],
3537
$badWordsArray
3638
);
3739

38-
$this->generateCensorChecks();
40+
$this->generateFilterChecks();
41+
}
42+
43+
public function reset()
44+
{
45+
$this->replaceFullWords($this->config['replaceFullWords']);
46+
47+
$this->replaceWith($this->config['replaceWith']);
48+
49+
return $this;
3950
}
4051

4152
public function replaceWith($string)
@@ -53,62 +64,93 @@ public function replaceFullWords($boolean)
5364
{
5465
$this->replaceFullWords = $boolean;
5566

56-
$this->generateCensorChecks();
67+
$this->generateFilterChecks();
5768

5869
return $this;
5970
}
6071

61-
public function filter($string)
72+
private function resetFiltered()
6273
{
74+
$this->filteredStrings = [];
75+
76+
$this->wasFiltered = false;
77+
}
78+
79+
public function filter($string, $details = null)
80+
{
81+
$this->resetFiltered();
82+
6383
if (!is_string($string) || !trim($string)) {
6484
return $string;
6585
}
6686

67-
return $this->filterString($string);
87+
$filtered = $this->filterString($string);
88+
89+
if ($details) {
90+
return [
91+
'orig' => $string,
92+
'clean' => $filtered,
93+
'hasMatch' => $this->wasFiltered,
94+
'matched' => $this->filteredStrings
95+
];
96+
}
97+
98+
return $filtered;
6899
}
69100

70101
private function filterString($string)
71102
{
72-
return preg_replace_callback($this->censorChecks, function ($matches) {
103+
return preg_replace_callback($this->filterChecks, function ($matches) {
73104
return $this->replaceWithFilter($matches[0]);
74105
}, $string);
75106
}
76107

108+
private function setFiltered($string)
109+
{
110+
array_push($this->filteredStrings, $string);
111+
112+
if (!$this->wasFiltered) {
113+
$this->wasFiltered = true;
114+
}
115+
}
116+
77117
private function replaceWithFilter($string)
78118
{
119+
$this->setFiltered($string);
120+
79121
$strlen = strlen($string);
80122

81123
if ($this->multiCharReplace) {
82124
return str_repeat($this->replaceWith, $strlen);
83125
}
84126

85-
return $this->randomCensorChar($strlen);
127+
return $this->randomFilterChar($strlen);
86128
}
87129

88-
private function generateCensorChecks()
130+
private function generateFilterChecks()
89131
{
90132
foreach ($this->badWords as $string) {
91-
$this->censorChecks[] = $this->getCensorRegexp($string);
133+
$this->filterChecks[] = $this->getFilterRegexp($string);
92134
}
93135
}
94136

95-
private function getCensorRegexp($string)
137+
private function getFilterRegexp($string)
96138
{
97-
$replaceCensor = $this->replaceCensor($string);
139+
$replaceFilter = $this->replaceFilter($string);
98140

99141
if ($this->replaceFullWords) {
100-
return '/\b'.$replaceCensor.'\b/i';
142+
return '/\b'.$replaceFilter.'\b/i';
101143
}
102144

103-
return '/'.$replaceCensor.'/i';
145+
return '/'.$replaceFilter.'/i';
104146
}
105147

106-
private function replaceCensor($string)
148+
private function replaceFilter($string)
107149
{
108150
return str_ireplace(array_keys($this->strReplace), array_values($this->strReplace), $string);
109151
}
110152

111-
private function randomCensorChar($len)
153+
private function randomFilterChar($len)
112154
{
113155
return str_shuffle(str_repeat($this->replaceWith, intval($len / $this->replaceWithLength)).substr($this->replaceWith, 0, ($len % $this->replaceWithLength)));
114156
}

tests/ProfanityFilterTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public function testReplaceFullWords()
3737
$this->assertEquals('hi you ****ing **** **** ****!', app('profanityFilter')->replaceFullWords(false)->filter($this->string));
3838
}
3939

40+
public function testMultipleFiltersWithReset()
41+
{
42+
$this->assertEquals('hi you fucking #### #### ####!', app('profanityFilter')->replaceWith('#')->filter($this->string));
43+
$this->assertEquals('hi you ****ing **** **** ****!', app('profanityFilter')->reset()->replaceFullWords(false)->filter($this->string));
44+
}
45+
4046
public function testEsLanguage()
4147
{
4248
app()->setLocale('es');
@@ -48,4 +54,19 @@ public function testEmptyFilter()
4854
{
4955
$this->assertEquals(' ', app('profanityFilter')->filter(' '));
5056
}
57+
58+
public function testFilterDetails()
59+
{
60+
$this->assertEquals([
61+
"orig" => "hi you fucking cunt fuck shit!",
62+
"clean" => "hi you ****ing **** **** ****!",
63+
"hasMatch" => true,
64+
"matched" => [
65+
0 => "fuck",
66+
1 => "shit",
67+
2 => "cunt",
68+
3 => "fuck",
69+
]
70+
], app('profanityFilter')->replaceFullWords(false)->filter($this->string, true));
71+
}
5172
}

0 commit comments

Comments
 (0)