I think it's done

This commit is contained in:
Arandi Lopez 2016-07-18 20:20:02 -05:00
parent 6a4e82ed56
commit 726e7f9797
8 changed files with 1977 additions and 1 deletions

View File

@ -8,6 +8,17 @@
"email": "arandilopez.93@gmail.com"
}
],
"autoload": {
"psr-4": {
"LaravelProfane\\": "src/"
}
},
"minimum-stability": "stable",
"require": {}
"require": {
"illuminate/support": "^5.2"
},
"require-dev": {
"phpunit/phpunit": "^4.8",
"mockery/mockery": "^0.9.5"
}
}

1446
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

7
phpunit.xml Normal file
View File

@ -0,0 +1,7 @@
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="profane">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>

View File

@ -0,0 +1,18 @@
<?php
namespace LaravelProfane;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Validator;
class ProfaneServiceProvider extends ServiceProvider
{
public function register()
{
# code...
}
public function boot()
{
Validator::extend('profane', 'LaravelProfane\ProfaneValidator@validate');
}
}

101
src/ProfaneValidator.php Normal file
View File

@ -0,0 +1,101 @@
<?php
namespace LaravelProfane;
use Illuminate\Support\Facades\Config;
use Illuminate\Contracts\Validation\Validator;
class ProfaneValidator
{
protected $badwords = [];
function __construct()
{
// Get default locale string in laravel project
// and set it as default dictionary
$locale_dict = Config::has('app.locale') ? Config::get('app.locale') : 'en';
$this->setDictionary($locale_dict);
}
/**
* Method to extends to Validator
* @param string $attribute
* @param midex $value
* @param array $parameters
* @param \Illuminate\Contracts\Validation\Validator $validator [description]
* @return bool
*/
public function validate($attribute, $value, $parameters, $validator)
{
if ($parameters) {
$this->loadDictionary($parameters);
}
return !$this->isProfane($value);
}
/**
* Check profanity of text
* @param string $text
* @return bool
*/
public function isProfane($text)
{
return str_contains($text, $this->badwords);
}
public function getBadwords()
{
return $this->badwords;
}
/**
* Merge the current dictionary with the passed
* @param array|string $dictionary
*/
public function loadDictionary($dictionary)
{
$this->badwords = array_merge($this->badwords, $this->readDictionary($dictionary));
}
/**
* Set the dictionary to use
* @param array|string $dictionary
*/
public function setDictionary($dictionary)
{
$this->badwords = $this->readDictionary($dictionary);
}
protected function readDictionary($dictionary)
{
$badwords = [];
$baseDictPath = __DIR__ . DIRECTORY_SEPARATOR .'dict/';
if (is_array($dictionary)) {
foreach ($dictionary as $file) {
if (file_exists($baseDictPath.$file.'.php')) {
$dict = include($baseDictPath.$file.'.php');
$badwords = array_merge($badwords, $dict);
} else {
// if the file isn't in the dict directory,
// it's probably a custom user library
$dict = include($file);
$badwords = array_merge($badwords, $dict);
}
}
// just a single string, not an array
} elseif (is_string($dictionary)) {
if (file_exists($baseDictPath.$dictionary.'.php')) {
$dict = include($baseDictPath.$dictionary.'.php');
$badwords = array_merge($badwords, $dict);
} else {
$dict = include($dictionary);
$badwords = array_merge($badwords, $dict);
}
}
return $badwords;
}
}

80
src/dict/en.php Normal file
View File

@ -0,0 +1,80 @@
<?php
return [
'anal',
'anus',
'ass',
'bastard',
'bitch',
'boob',
'cock',
'cum',
'cunt',
'dick',
'dildo',
'dyke',
'fag',
'faggot',
'fuck',
'fuk',
'handjob',
'homo',
'jizz',
'kike',
'kunt',
'muff',
'nigger',
'penis',
'piss',
'poop',
'pussy',
'queer',
'rape',
'semen',
'sex',
'shit',
'slut',
'titties',
'twat',
'vagina',
'vulva',
'wank',
'abortion',
'anus',
'beastiality',
'bestiality',
'bewb',
'blow',
'blumpkin',
'cawk',
'choad',
'cooter',
'cornhole',
'dong',
'douche',
'fart',
'foreskin',
'gangbang',
'gook',
'hell',
'honkey',
'humping',
'jiz',
'labia',
'nutsack',
'pen1s',
'poon',
'punani',
'queef',
'quim',
'rectal',
'rectum',
'rimjob',
'spick',
'spoo',
'spooge',
'taint',
'titty',
'vag',
'whore'
];

85
src/dict/es.php Normal file
View File

@ -0,0 +1,85 @@
<?php
return [
'bollera',
'cabron',
'cabrón',
'cabrona',
'cabronazo',
'capulla',
'capullo',
'chichi',
'chinga',
'chingo',
'chingon',
'chingona',
'chingatumadre',
'chinga tu madre',
'chingada',
'chingado',
'chocho',
'cojon',
'cojón',
'cojones',
'comepollas',
'cono',
'coño',
'culo',
'follar',
'follen',
'furcia',
'gilipollas',
'hijaputa',
'hijo puta',
'hijo de puta',
'hijodeputa',
'hijoputa',
'hostia',
'joder',
'jodete',
'jódete',
'joputa',
'mamada',
'mamon',
'mamón',
'mamona',
'marica',
'maricon',
'maricón',
'maricona',
'mariconazo',
'nazi',
'ojete',
'ostia',
'pajillero',
'pendon',
'pendón',
'pendejo',
'pendejos',
'pendeja',
'pendejas',
'pendejada',
'pendejadas',
'pedo',
'pedorra',
'pedorro',
'pedorron',
'pedorrón',
'picha',
'pito',
'pelana',
'polla',
'pollon',
'pollón',
'polvo',
'potorro',
'puta',
'puto',
'putona',
'puton',
'putón',
'tortillera',
'weputa',
'zorron',
'zorrón'
];

View File

@ -0,0 +1,228 @@
<?php
use LaravelProfane\ProfaneValidator;
use Illuminate\Support\Facades\Config;
use \Mockery as m;
class ProfaneValidatorTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
parent::tearDown();
m::close();
}
public function test_can_evaluate_profanity_of_a_word()
{
$word = 'fuck';
Config::shouldReceive('get')
->once()
->with('app.locale')
->andReturn('en');
Config::shouldReceive('has')
->once()
->with('app.locale')
->andReturn(true);
$profane = new ProfaneValidator();
$this->assertTrue($profane->isProfane($word));
}
public function test_can_evaluate_profanity_of_a_sentence()
{
$word = 'fuck you if you read this';
Config::shouldReceive('get')
->once()
->with('app.locale')
->andReturn('en');
Config::shouldReceive('has')
->once()
->with('app.locale')
->andReturn(true);
$profane = new ProfaneValidator();
$this->assertTrue($profane->isProfane($word));
}
public function test_can_evaluate_profanity_of_a_html_string()
{
$word = '<b>fuck</b> you if you read this.';
Config::shouldReceive('get')
->once()
->with('app.locale')
->andReturn('en');
Config::shouldReceive('has')
->once()
->with('app.locale')
->andReturn(true);
$profane = new ProfaneValidator();
$this->assertTrue($profane->isProfane($word));
}
public function test_can_set_dictionary_when_you_pass_a_locale()
{
Config::shouldReceive('get')
->once()
->with('app.locale')
->andReturn('en');
Config::shouldReceive('has')
->once()
->with('app.locale')
->andReturn(true);
$profane = new ProfaneValidator();
$profane->setDictionary('es');
$expected = include __DIR__.'/../src/dict/es.php';
$this->assertEquals($profane->getBadwords(), $expected);
}
public function test_can_set_dictionary_when_you_pass_a_file()
{
Config::shouldReceive('get')
->once()
->with('app.locale')
->andReturn('en');
Config::shouldReceive('has')
->once()
->with('app.locale')
->andReturn(true);
$profane = new ProfaneValidator();
$profane->setDictionary(__DIR__.'/../src/dict/es.php');
$expected = include __DIR__.'/../src/dict/es.php';
$this->assertEquals($profane->getBadwords(), $expected);
}
public function test_can_set_dictionary_when_you_pass_an_array_of_files()
{
Config::shouldReceive('get')
->once()
->with('app.locale')
->andReturn('en');
Config::shouldReceive('has')
->once()
->with('app.locale')
->andReturn(true);
$profane = new ProfaneValidator();
$profane->setDictionary([__DIR__.'/../src/dict/es.php']);
$expected = include __DIR__.'/../src/dict/es.php';
$this->assertEquals($profane->getBadwords(), $expected);
}
public function test_can_set_dictionary_when_you_pass_an_array_of_locales()
{
Config::shouldReceive('get')
->once()
->with('app.locale')
->andReturn('en');
Config::shouldReceive('has')
->once()
->with('app.locale')
->andReturn(true);
$profane = new ProfaneValidator();
$profane->setDictionary(['es']);
$expected = include __DIR__.'/../src/dict/es.php';
$this->assertEquals($profane->getBadwords(), $expected);
}
public function test_can_load_dictionary_when_you_pass_a_locale()
{
Config::shouldReceive('get')
->once()
->with('app.locale')
->andReturn('en');
Config::shouldReceive('has')
->once()
->with('app.locale')
->andReturn(true);
$profane = new ProfaneValidator();
$profane->loadDictionary('es');
$expected = array_merge(include __DIR__.'/../src/dict/en.php', include __DIR__.'/../src/dict/es.php');
$this->assertEquals($profane->getBadwords(), $expected);
}
public function test_can_load_dictionary_when_you_pass_a_file()
{
Config::shouldReceive('get')
->once()
->with('app.locale')
->andReturn('en');
Config::shouldReceive('has')
->once()
->with('app.locale')
->andReturn(true);
$profane = new ProfaneValidator();
$profane->loadDictionary(__DIR__.'/../src/dict/es.php');
$expected = array_merge(include __DIR__.'/../src/dict/en.php', include __DIR__.'/../src/dict/es.php');
$this->assertEquals($profane->getBadwords(), $expected);
}
public function test_can_load_dictionary_when_you_pass_an_array_of_files()
{
Config::shouldReceive('get')
->once()
->with('app.locale')
->andReturn('en');
Config::shouldReceive('has')
->once()
->with('app.locale')
->andReturn(true);
$profane = new ProfaneValidator();
$profane->loadDictionary([__DIR__.'/../src/dict/es.php']);
$expected = array_merge(include __DIR__.'/../src/dict/en.php', include __DIR__.'/../src/dict/es.php');
$this->assertEquals($profane->getBadwords(), $expected);
}
public function test_can_load_dictionary_when_you_pass_an_array_of_locales()
{
Config::shouldReceive('get')
->once()
->with('app.locale')
->andReturn('en');
Config::shouldReceive('has')
->once()
->with('app.locale')
->andReturn(true);
$profane = new ProfaneValidator();
$profane->loadDictionary(['es']);
$expected = array_merge(include __DIR__.'/../src/dict/en.php', include __DIR__.'/../src/dict/es.php');
$this->assertEquals($profane->getBadwords(), $expected);
}
}