Fixes #1. Match in caseless way

This commit is contained in:
Arandi López 2017-05-08 11:45:36 -05:00
parent e9156ed51b
commit 076cde1a1b
3 changed files with 36 additions and 1 deletions

View File

@ -43,7 +43,7 @@ class ProfaneValidator
*/
public function isProfane($text)
{
return str_contains($text, $this->badwords);
return Str::containsCaseless($text, $this->badwords);
}
public function getBadwords()

25
src/Str.php Normal file
View File

@ -0,0 +1,25 @@
<?php
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
*/
public static function containsCaseless($haystack, $needles)
{
foreach ((array) $needles as $needle) {
$needle = preg_quote($needle);
if ($needle != '' && preg_match("/$needle/iu", $haystack)) {
return true;
}
}
return false;
}
}

View File

@ -66,6 +66,16 @@ class ProfaneValidatorTest extends PHPUnit_Framework_TestCase
$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();
$profane = new ProfaneValidator();
$this->assertTrue($profane->isProfane($word));
}
public function test_can_set_dictionary_when_you_pass_a_locale()
{
$this->mockConfigs();