-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from krlxfm/profanity-rule
Add a profanity rule
- Loading branch information
Showing
4 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace KRLX\Rules; | ||
|
||
use Illuminate\Contracts\Validation\Rule; | ||
|
||
class Profanity implements Rule | ||
{ | ||
/** | ||
* The word that has been found. | ||
* | ||
* @var string|null | ||
*/ | ||
public $word; | ||
|
||
/** | ||
* Create a new rule instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* @param string $attribute | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function passes($attribute, $value) | ||
{ | ||
$target = preg_replace('/[@#\$%\^]/', '*', strtolower($value)); | ||
$words = explode(' ', $target); | ||
foreach (array_merge(config('defaults.banned_words.full'), config('defaults.banned_words.partial')) as $bad_word) { | ||
if (in_array($bad_word, $words) or in_array(str_plural($bad_word), $words)) { | ||
$this->word = $bad_word; | ||
|
||
return false; | ||
} | ||
} | ||
|
||
return $this->partialWordsPass($target); | ||
} | ||
|
||
/** | ||
* Check the partial words - these are words which could appear as they | ||
* normally are, or in any number of creative derivatives. | ||
* | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
protected function partialWordsPass($value) | ||
{ | ||
$bad_words = $this->assembleDerivatives(); | ||
foreach ($bad_words as $word => $derivatives) { | ||
if (! $this->singleWordDerivativesPass($word, $derivatives, $value)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Check if a single word's derivatives show up. | ||
* | ||
* @param string $word | ||
* @param array $derivatives | ||
* @param string $value | ||
* @return bool | ||
*/ | ||
private function singleWordDerivativesPass($word, $derivatives, $value) | ||
{ | ||
foreach ($derivatives as $derivative) { | ||
if (strpos($value, $derivative) !== false) { | ||
$this->word = $word; | ||
|
||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Assemble the derivatives list used in partial word validation. | ||
* | ||
* @return array | ||
*/ | ||
protected function assembleDerivatives() | ||
{ | ||
$bad_words = []; | ||
foreach (config('defaults.banned_words.partial') as $bad_word) { | ||
$bad_words[$bad_word] = [ | ||
$bad_word, | ||
str_plural($bad_word), | ||
$bad_word[0].str_repeat('*', strlen($bad_word) - 1), | ||
$bad_word[0].str_repeat('*', strlen($bad_word) - 2).$bad_word[-1], | ||
]; | ||
} | ||
|
||
return $bad_words; | ||
} | ||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @return string | ||
*/ | ||
public function message() | ||
{ | ||
return "The word {$this->word} can't appear in the :attribute."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters