From 81082f9a070fdaf27d81965a26aedc17c95b4916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arandi=20L=C3=B3pez?= Date: Mon, 29 May 2017 13:44:25 -0500 Subject: [PATCH] Identation... If your are using a non supported locale, it wont try to loaded --- src/ProfaneServiceProvider.php | 30 +++--- src/ProfaneValidator.php | 158 +++++++++++++-------------- src/Str.php | 14 +-- src/dict/en.php | 152 +++++++++++++------------- src/dict/es.php | 166 ++++++++++++++--------------- src/lang/en/validation.php | 2 +- src/lang/es/validation.php | 2 +- tests/ProfaneValidatorTest.php | 188 ++++++++++++++++----------------- 8 files changed, 357 insertions(+), 355 deletions(-) diff --git a/src/ProfaneServiceProvider.php b/src/ProfaneServiceProvider.php index 62cc35b..ae41f89 100644 --- a/src/ProfaneServiceProvider.php +++ b/src/ProfaneServiceProvider.php @@ -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... + } } diff --git a/src/ProfaneValidator.php b/src/ProfaneValidator.php index b0dbacc..78c9bdc 100644 --- a/src/ProfaneValidator.php +++ b/src/ProfaneValidator.php @@ -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/'; + } } diff --git a/src/Str.php b/src/Str.php index 42152e5..aa974f9 100644 --- a/src/Str.php +++ b/src/Str.php @@ -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) { diff --git a/src/dict/en.php b/src/dict/en.php index 7670c8a..b2f33ae 100644 --- a/src/dict/en.php +++ b/src/dict/en.php @@ -1,80 +1,80 @@ 'The :attribute contains vulgar content' + 'profane' => 'The :attribute contains vulgar content' ]; diff --git a/src/lang/es/validation.php b/src/lang/es/validation.php index 50f4848..f8592ed 100644 --- a/src/lang/es/validation.php +++ b/src/lang/es/validation.php @@ -1,5 +1,5 @@ 'El :attribute contiene palabras vulgares' + 'profane' => 'El :attribute contiene palabras vulgares' ]; diff --git a/tests/ProfaneValidatorTest.php b/tests/ProfaneValidatorTest.php index 7f69f2a..d93a3b2 100644 --- a/tests/ProfaneValidatorTest.php +++ b/tests/ProfaneValidatorTest.php @@ -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 = 'fuck you if you read this.'; - $this->mockConfigs(); + public function test_can_evaluate_profanity_of_a_html_string() + { + $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_as_caseless_mode() - { - $word = 'FUCK you BITCH if you read this.'; - $this->mockConfigs(); + public function test_can_evaluate_as_caseless_mode() + { + $word = 'FUCK 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); + } }