Identation... If your are using a non supported locale, it wont try to loaded

This commit is contained in:
Arandi López 2017-05-29 13:44:25 -05:00
parent 076cde1a1b
commit 81082f9a07
8 changed files with 357 additions and 355 deletions

View File

@ -7,23 +7,23 @@ use Illuminate\Support\Facades\Lang;
class ProfaneServiceProvider extends ServiceProvider
{
public function boot()
{
$this->loadTranslationsFrom(__DIR__.'/lang', 'laravel-profane');
public function boot()
{
$this->loadTranslationsFrom(__DIR__.'/lang', 'laravel-profane');
$this->publishes([
__DIR__.'/lang' => resource_path('lang/vendor/laravel-profane'),
]);
$this->publishes([
__DIR__.'/lang' => resource_path('lang/vendor/laravel-profane'),
]);
Validator::extend('profane', 'LaravelProfane\ProfaneValidator@validate', Lang::get('laravel-profane::validation.profane'));
Validator::extend('profane', 'LaravelProfane\ProfaneValidator@validate', Lang::get('laravel-profane::validation.profane'));
Validator::replacer('profane', function($message, $attribute, $rule, $parameters) {
return str_replace(':attribute', $attribute, $message);
});
}
Validator::replacer('profane', function($message, $attribute, $rule, $parameters) {
return str_replace(':attribute', $attribute, $message);
});
}
public function register()
{
// code...
}
public function register()
{
// code...
}
}

View File

@ -8,90 +8,92 @@ use Illuminate\Contracts\Validation\Validator;
class ProfaneValidator
{
protected $badwords = [];
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';
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)
{
if ($parameters) {
$this->setDictionary($parameters);
$this->setDictionary($locale_dict);
}
return !$this->isProfane($value);
}
/**
* Check profanity of text
* @param string $text
* @return bool
*/
public function isProfane($text)
{
return Str::containsCaseless($text, $this->badwords);
}
public function getBadwords()
{
return $this->badwords;
}
/**
* Set the dictionary to use
* @param array|string $dictionary
*/
public function setDictionary($dictionary)
{
$this->badwords = $this->readDictionary($dictionary);
}
protected function readDictionary($dictionary)
{
$badwords = [];
$baseDictPath = $this->getBaseDictPath();
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);
/**
* 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)
{
if ($parameters) {
$this->setDictionary($parameters);
}
}
// 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 !$this->isProfane($value);
}
return $badwords;
}
/**
* Check profanity of text
* @param string $text
* @return bool
*/
public function isProfane($text)
{
return Str::containsCaseless($text, $this->badwords);
}
protected function getBaseDictPath()
{
return property_exists($this, 'baseDictPath') ? $this->baseDictPath : __DIR__ . DIRECTORY_SEPARATOR .'dict/';
}
public function getBadwords()
{
return $this->badwords;
}
/**
* Set the dictionary to use
* @param array|string $dictionary
*/
public function setDictionary($dictionary)
{
$this->badwords = $this->readDictionary($dictionary);
}
protected function readDictionary($dictionary)
{
$badwords = [];
$baseDictPath = $this->getBaseDictPath();
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 {
if (file_exists($dictionary)) {
$dict = include($dictionary);
$badwords = array_merge($badwords, $dict);
} // else nothing is merged
}
}
return $badwords;
}
protected function getBaseDictPath()
{
return property_exists($this, 'baseDictPath') ? $this->baseDictPath : __DIR__ . DIRECTORY_SEPARATOR .'dict/';
}
}

View File

@ -5,13 +5,13 @@ namespace LaravelProfane;
class Str
{
/**
* Taken from Illuminate\Support\Str
* Determine if a given string contains a given word with case insensitive match.
*
* @param string $haystack
* @param string|array $needles
* @return bool
*/
* Taken from Illuminate\Support\Str
* Determine if a given string contains a given word with case insensitive match.
*
* @param string $haystack
* @param string|array $needles
* @return bool
*/
public static function containsCaseless($haystack, $needles)
{
foreach ((array) $needles as $needle) {

View File

@ -1,80 +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'
'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'
];

View File

@ -1,87 +1,87 @@
<?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',
'culero',
'culera',
'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'
'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',
'culero',
'culera',
'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

@ -1,5 +1,5 @@
<?php
return [
'profane' => 'The :attribute contains vulgar content'
'profane' => 'The :attribute contains vulgar content'
];

View File

@ -1,5 +1,5 @@
<?php
return [
'profane' => 'El :attribute contiene palabras vulgares'
'profane' => 'El :attribute contiene palabras vulgares'
];

View File

@ -6,134 +6,134 @@ use \Mockery as m;
class ProfaneValidatorTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
parent::tearDown();
m::close();
}
public function tearDown()
{
parent::tearDown();
m::close();
}
public function test_can_validate_a_word()
{
$this->mockConfigs();
$attribute = 'username';
$word = 'culero23';
$parameters = ['es'];
public function test_can_validate_a_word()
{
$this->mockConfigs();
$attribute = 'username';
$word = 'culero23';
$parameters = ['es'];
$profane = new ProfaneValidator();
$profane = new ProfaneValidator();
$this->assertFalse($profane->validate($attribute, $word, $parameters));
}
$this->assertFalse($profane->validate($attribute, $word, $parameters));
}
public function test_can_validate_a_text()
{
$this->mockConfigs();
$attribute = 'description';
$text = 'fck you bitch';
$parameters = ['es', 'en'];
public function test_can_validate_a_text()
{
$this->mockConfigs();
$attribute = 'description';
$text = 'fck you bitch';
$parameters = ['es', 'en'];
$profane = new ProfaneValidator();
$profane = new ProfaneValidator();
$this->assertFalse($profane->validate($attribute, $text, $parameters));
}
$this->assertFalse($profane->validate($attribute, $text, $parameters));
}
public function test_can_evaluate_profanity_of_a_word()
{
$word = 'fuck';
$this->mockConfigs();
public function test_can_evaluate_profanity_of_a_word()
{
$word = 'fuck';
$this->mockConfigs();
$profane = new ProfaneValidator();
$profane = new ProfaneValidator();
$this->assertTrue($profane->isProfane($word));
}
$this->assertTrue($profane->isProfane($word));
}
public function test_can_evaluate_profanity_of_a_sentence()
{
$word = 'fuck you if you read this';
$this->mockConfigs();
public function test_can_evaluate_profanity_of_a_sentence()
{
$word = 'fuck you if you read this';
$this->mockConfigs();
$profane = new ProfaneValidator();
$profane = new ProfaneValidator();
$this->assertTrue($profane->isProfane($word));
}
$this->assertTrue($profane->isProfane($word));
}
public function test_can_evaluate_profanity_of_a_html_string()
{
$word = '<b>fuck</b> you if you read this.';
$this->mockConfigs();
public function test_can_evaluate_profanity_of_a_html_string()
{
$word = '<b>fuck</b> you if you read this.';
$this->mockConfigs();
$profane = new ProfaneValidator();
$profane = new ProfaneValidator();
$this->assertTrue($profane->isProfane($word));
}
$this->assertTrue($profane->isProfane($word));
}
public function test_can_evaluate_as_caseless_mode()
{
$word = '<b>FUCK</b> you BITCH if you read this.';
$this->mockConfigs();
public function test_can_evaluate_as_caseless_mode()
{
$word = '<b>FUCK</b> you BITCH if you read this.';
$this->mockConfigs();
$profane = new ProfaneValidator();
$profane = new ProfaneValidator();
$this->assertTrue($profane->isProfane($word));
}
$this->assertTrue($profane->isProfane($word));
}
public function test_can_set_dictionary_when_you_pass_a_locale()
{
$this->mockConfigs();
public function test_can_set_dictionary_when_you_pass_a_locale()
{
$this->mockConfigs();
$profane = new ProfaneValidator();
$profane->setDictionary('es');
$profane = new ProfaneValidator();
$profane->setDictionary('es');
$expected = include __DIR__.'/../src/dict/es.php';
$expected = include __DIR__.'/../src/dict/es.php';
$this->assertEquals($profane->getBadwords(), $expected);
}
$this->assertEquals($profane->getBadwords(), $expected);
}
public function test_can_set_dictionary_when_you_pass_a_file()
{
$this->mockConfigs();
public function test_can_set_dictionary_when_you_pass_a_file()
{
$this->mockConfigs();
$profane = new ProfaneValidator();
$profane->setDictionary(__DIR__.'/../src/dict/es.php');
$profane = new ProfaneValidator();
$profane->setDictionary(__DIR__.'/../src/dict/es.php');
$expected = include __DIR__.'/../src/dict/es.php';
$expected = include __DIR__.'/../src/dict/es.php';
$this->assertEquals($profane->getBadwords(), $expected);
}
$this->assertEquals($profane->getBadwords(), $expected);
}
public function test_can_set_dictionary_when_you_pass_an_array_of_files()
{
$this->mockConfigs();
public function test_can_set_dictionary_when_you_pass_an_array_of_files()
{
$this->mockConfigs();
$profane = new ProfaneValidator();
$profane->setDictionary([__DIR__.'/../src/dict/es.php', __DIR__.'/../src/dict/en.php']);
$profane = new ProfaneValidator();
$profane->setDictionary([__DIR__.'/../src/dict/es.php', __DIR__.'/../src/dict/en.php']);
$expected = array_merge(include __DIR__.'/../src/dict/es.php', include __DIR__.'/../src/dict/en.php');
$expected = array_merge(include __DIR__.'/../src/dict/es.php', include __DIR__.'/../src/dict/en.php');
$this->assertEquals($profane->getBadwords(), $expected);
}
$this->assertEquals($profane->getBadwords(), $expected);
}
public function test_can_set_dictionary_when_you_pass_an_array_of_locales()
{
$this->mockConfigs();
public function test_can_set_dictionary_when_you_pass_an_array_of_locales()
{
$this->mockConfigs();
$profane = new ProfaneValidator();
$profane->setDictionary(['es', 'en']);
$profane = new ProfaneValidator();
$profane->setDictionary(['es', 'en']);
$expected = array_merge(include __DIR__.'/../src/dict/es.php', include __DIR__.'/../src/dict/en.php');
$expected = array_merge(include __DIR__.'/../src/dict/es.php', include __DIR__.'/../src/dict/en.php');
$this->assertEquals($profane->getBadwords(), $expected);
}
$this->assertEquals($profane->getBadwords(), $expected);
}
private function mockConfigs()
{
Config::shouldReceive('get')
->once()
->with('app.locale')
->andReturn('en');
private function mockConfigs()
{
Config::shouldReceive('get')
->once()
->with('app.locale')
->andReturn('en');
Config::shouldReceive('has')
->once()
->with('app.locale')
->andReturn(true);
}
Config::shouldReceive('has')
->once()
->with('app.locale')
->andReturn(true);
}
}