Separation of responsabilities on ProfaneValidator class

This commit is contained in:
Dorian Neto 2017-10-09 11:25:03 -03:00 committed by Arandi López
parent 175b321868
commit e2492d5dd4
5 changed files with 165 additions and 124 deletions

91
src/Dictionary.php Normal file
View File

@ -0,0 +1,91 @@
<?php
namespace LaravelProfane;
use Illuminate\Support\Facades\Config;
class Dictionary
{
/**
* [$dictionary description]
* @var [type]
*/
private $dictionary;
/**
* [__construct description]
* @param [type] $dictionary [description]
*/
function __construct($dictionary = null)
{
// Get default locale string in laravel project
// and set it as default dictionary
$locale = Config::has('app.locale') ? Config::get('app.locale') : 'en';
$this->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/';
}
}

View File

@ -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/';
}
}

59
tests/DictionaryTest.php Normal file
View File

@ -0,0 +1,59 @@
<?php
namespace LaravelProfaneTests;
use LaravelProfane\Dictionary;
use LaravelProfaneTests\TestCase;
class DictionaryTest extends TestCase
{
public function test_words_from_only_one_locale()
{
$dictionary = new Dictionary('es');
$expected = include __DIR__.'/../src/dict/es.php';
$this->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);
}
}

View File

@ -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();

View File

@ -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));
}
/**