Skip to content

Commit

Permalink
Merge pull request #23 from tuanht/master
Browse files Browse the repository at this point in the history
Allowed add more dictionary to CensorWords
  • Loading branch information
snipe committed Aug 26, 2015
2 parents 18b5dd8 + 4c68599 commit 180f508
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
68 changes: 47 additions & 21 deletions src/CensorWords.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

class CensorWords
{
public $badwords;

/*
* When the dictionary is loaded, a ton of regular expression strings are generated
* These regular expressions are used to perform the profanity checks.
* Store them here so when we call censorString we don't need to regenerate them on every call
*/
private $censorChecks = null;

public function __construct() {
public function __construct() {
$this->badwords = array();
$this->replacer = '*';
$this->setDictionary('en-us');
}
Expand All @@ -26,34 +29,57 @@ public function __construct() {
* string
*/
public function setDictionary($dictionary) {


$this->badwords = $this->readBadWords($dictionary);
}

/**
* Add more the dictionar(y|ies) to current bad words list
* This can accept a string to a language file path,
* or an array of strings to multiple paths
*
* @param string/array
* string
*/
public function addDictionary($dictionary) {

$this->badwords = array_merge($this->badwords, $this->readBadWords($dictionary));
}

/**
* Read bad words list from dictionar(y|ies) and return it
*
* @param string/array
* array
*/
private function readBadWords($dictionary) {
$badwords = array();
$this->dictionary = $dictionary;
if (is_array($this->dictionary)) {
for ($x=0; $x < count($this->dictionary); $x++) {
if (file_exists(__DIR__ . DIRECTORY_SEPARATOR .'dict/'.$this->dictionary[$x].'.php')) {
include(__DIR__ . DIRECTORY_SEPARATOR .'dict/'.$this->dictionary[$x].'.php');
$baseDictPath = __DIR__ . DIRECTORY_SEPARATOR .'dict/';

if (is_array($dictionary)) {
for ($x=0; $x < count($dictionary); $x++) {
if (file_exists($baseDictPath.$dictionary[$x].'.php')) {
include($baseDictPath.$dictionary[$x].'.php');
} else {
// if the file isn't in the dict directory,
// if the file isn't in the dict directory,
// it's probably a custom user library
include($this->dictionary[$x]);
include($dictionary[$x]);
}
}
// just a single string, not an array
} elseif (is_string($this->dictionary)) {
if (file_exists(__DIR__ . DIRECTORY_SEPARATOR .'dict/'.$this->dictionary.'.php')) {
include(__DIR__ . DIRECTORY_SEPARATOR .'dict/'.$this->dictionary.'.php');

}

// just a single string, not an array
} elseif (is_string($dictionary)) {
if (file_exists($baseDictPath.$dictionary.'.php')) {
include($baseDictPath.$dictionary.'.php');
} else {
include($this->dictionary);
include($dictionary);
}
}
$this->badwords = $badwords;
}

return $badwords;
}


/**
* Sets the replacement character to use
*
Expand Down
14 changes: 14 additions & 0 deletions tests/CensorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ public function testSetDictionary()
$censor->setDictionary('fr');
$this->assertNotEmpty($censor->badwords);
}

public function testAddDictionary()
{
$censor = new CensorWords();
$censor->addDictionary('fr');

$this->assertNotEmpty($censor->badwords);

$string1 = $censor->censorString('fuck');
$this->assertEquals('****', $string1['clean']);

$string2 = $censor->censorString('nique');
$this->assertEquals('*****', $string2['clean']);
}

/**
* @expectedException PHPUnit_Framework_Error
Expand Down

0 comments on commit 180f508

Please sign in to comment.