diff --git a/src/Dictionary.php b/src/Dictionary.php new file mode 100644 index 0000000..517fd86 --- /dev/null +++ b/src/Dictionary.php @@ -0,0 +1,91 @@ +setDictionary($dictionary ?: $locale); + } + + /** + * [getDictionary description] + * @return [type] [description] + */ + public function getDictionary() + { + return $this->dictionary; + } + + /** + * Set the dictionary to use + * @param array|string $dictionary + */ + public function setDictionary($dictionary) + { + $this->dictionary = $this->readDictionary($dictionary); + } + + /** + * [readDictionary description] + * @param [type] $dictionary [description] + * @return [type] [description] + */ + protected function readDictionary($dictionary) + { + $words = []; + $baseDictPath = $this->getBaseDictPath(); + if (is_array($dictionary)) { + foreach ($dictionary as $file) { + if (file_exists($baseDictPath.$file.'.php')) { + $dict = include($baseDictPath.$file.'.php'); + $words = array_merge($words, $dict); + } else { + // if the file isn't in the dict directory, + // it's probably a custom user library + $dict = include($file); + $words = array_merge($words, $dict); + } + } + // just a single string, not an array + } elseif (is_string($dictionary)) { + if (file_exists($baseDictPath.$dictionary.'.php')) { + $dict = include($baseDictPath.$dictionary.'.php'); + $words = array_merge($words, $dict); + } else { + if (file_exists($dictionary)) { + $dict = include($dictionary); + $words = array_merge($words, $dict); + } // else nothing is merged + } + } + + return $words; + } + + /** + * [getBaseDictPath description] + * @return [type] [description] + */ + protected function getBaseDictPath() + { + return property_exists($this, 'baseDictPath') ? $this->baseDictPath : __DIR__ . DIRECTORY_SEPARATOR .'dict/'; + } +} diff --git a/src/ProfaneValidator.php b/src/ProfaneValidator.php index e7437c2..d2c36f7 100644 --- a/src/ProfaneValidator.php +++ b/src/ProfaneValidator.php @@ -2,11 +2,17 @@ namespace LaravelProfane; -use Illuminate\Support\Facades\Config; +use LaravelProfane\Dictionary; use Illuminate\Contracts\Validation\Validator; class ProfaneValidator { + /** + * [$dictionary description] + * @var [type] + */ + protected $dictionary; + /** * [$badwords description] * @var array @@ -15,14 +21,12 @@ class ProfaneValidator /** * [__construct description] + * @param Dictionary $dictionary [description] */ - function __construct() + function __construct(Dictionary $dictionary) { - // 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); + $this->dictionary = $dictionary; + $this->badwords = $dictionary->getDictionary(); } /** @@ -36,7 +40,8 @@ class ProfaneValidator public function validate($attribute, $value, $parameters) { if ($parameters) { - $this->setDictionary($parameters); + $this->dictionary->setDictionary($parameters); + $this->badwords = $this->dictionary->getDictionary(); } return !$this->isProfane($value); @@ -54,68 +59,4 @@ class ProfaneValidator $this->badwords ); } - - /** - * [getBadwords description] - * @return [type] [description] - */ - public function getBadwords() - { - return $this->badwords; - } - - /** - * Set the dictionary to use - * @param array|string $dictionary - */ - public function setDictionary($dictionary) - { - $this->badwords = $this->readDictionary($dictionary); - } - - /** - * [readDictionary description] - * @param [type] $dictionary [description] - * @return [type] [description] - */ - 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; - } - - /** - * [getBaseDictPath description] - * @return [type] [description] - */ - protected function getBaseDictPath() - { - return property_exists($this, 'baseDictPath') ? $this->baseDictPath : __DIR__ . DIRECTORY_SEPARATOR .'dict/'; - } } diff --git a/tests/DictionaryTest.php b/tests/DictionaryTest.php new file mode 100644 index 0000000..3285ef2 --- /dev/null +++ b/tests/DictionaryTest.php @@ -0,0 +1,59 @@ +assertEquals($dictionary->getDictionary(), $expected); + } + + public function test_words_from_only_one_file() + { + $dictionary = new Dictionary(__DIR__.'/../src/dict/es.php'); + + $expected = include __DIR__.'/../src/dict/es.php'; + + $this->assertEquals($dictionary->getDictionary(), $expected); + } + + public function test_words_from_locale_array() + { + $dictionary = new Dictionary([ + 'es', + 'gr' + ]); + + $expected = array_merge( + include __DIR__.'/../src/dict/es.php', + include __DIR__.'/../src/dict/gr.php' + ); + + $this->assertEquals($dictionary->getDictionary(), $expected); + } + + public function test_words_from_file_array() + { + $dictionary = new Dictionary([ + __DIR__.'/../src/dict/es.php', + __DIR__.'/../src/dict/gr.php', + __DIR__.'/../src/dict/it.php', + ]); + + $expected = array_merge( + include __DIR__.'/../src/dict/es.php', + include __DIR__.'/../src/dict/gr.php', + include __DIR__.'/../src/dict/it.php' + ); + + $this->assertEquals($dictionary->getDictionary(), $expected); + } +} \ No newline at end of file diff --git a/tests/ProfaneValidatorTest.php b/tests/ProfaneValidatorTest.php index 97f7b05..16b7a95 100644 --- a/tests/ProfaneValidatorTest.php +++ b/tests/ProfaneValidatorTest.php @@ -8,53 +8,6 @@ use LaravelProfaneTests\Support\ProfaneValidatorBuilder; class ProfaneValidatorTest extends TestCase { - public function test_can_set_dictionary_when_you_pass_a_locale() - { - $builder = new ProfaneValidatorBuilder('es'); - - $expected = include __DIR__.'/../src/dict/es.php'; - - $this->assertEquals($builder->build()->getBadwords(), $expected); - } - - public function test_can_set_dictionary_when_you_pass_a_file() - { - $builder = new ProfaneValidatorBuilder(__DIR__.'/../src/dict/es.php'); - - $expected = include __DIR__.'/../src/dict/es.php'; - - $this->assertEquals($builder->build()->getBadwords(), $expected); - } - - public function test_can_set_dictionary_when_you_pass_an_array_of_files() - { - $builder = new ProfaneValidatorBuilder([ - __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' - ); - - $this->assertEquals($builder->build()->getBadwords(), $expected); - } - - public function test_can_set_dictionary_when_you_pass_an_array_of_locales() - { - $builder = new ProfaneValidatorBuilder(['es', 'en', 'it', 'zh-tw']); - - $expected = array_merge( - include __DIR__.'/../src/dict/es.php', - include __DIR__.'/../src/dict/en.php', - include __DIR__.'/../src/dict/it.php', - include __DIR__.'/../src/dict/zh-tw.php' - ); - - $this->assertEquals($builder->build()->getBadwords(), $expected); - } - public function test_can_validate_a_word_with_numbers() { $builder = new ProfaneValidatorBuilder(); diff --git a/tests/Support/ProfaneValidatorBuilder.php b/tests/Support/ProfaneValidatorBuilder.php index f7bf94d..63ae36d 100644 --- a/tests/Support/ProfaneValidatorBuilder.php +++ b/tests/Support/ProfaneValidatorBuilder.php @@ -3,6 +3,7 @@ namespace LaravelProfaneTests\Support; use LaravelProfane\ProfaneValidator; +use LaravelProfane\Dictionary; class ProfaneValidatorBuilder { @@ -18,11 +19,7 @@ class ProfaneValidatorBuilder */ public function __construct($dictionary = null) { - $this->profaneValidator = new ProfaneValidator; - - if ($dictionary) { - $this->profaneValidator->setDictionary($dictionary); - } + $this->profaneValidator = new ProfaneValidator(new Dictionary($dictionary)); } /**