-
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.
- Loading branch information
0 parents
commit 9445929
Showing
7 changed files
with
112 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor |
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,2 @@ | ||
## Pure Text Package | ||
for pure content |
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,24 @@ | ||
{ | ||
"name": "yasser-elgammal/pure-text", | ||
"description": "Filter Bad Words", | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"YasserElgammal\\PureText\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "YasserElgammal", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"minimum-stability": "dev", | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"YasserElgammal\\PureText\\PureTextServiceProvider" | ||
] | ||
} | ||
} | ||
} |
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,6 @@ | ||
<?php | ||
|
||
return [ | ||
'words' => ['badword1', 'badword2', 'badword3'], // List of bad words to filter | ||
'replacement' => '***', // Replacement text for bad words | ||
]; |
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,28 @@ | ||
<?php | ||
|
||
namespace YasserElgammal\PureText; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
use YasserElgammal\PureText\Services\PureTextFilterService; | ||
|
||
class PureTextServiceProvider extends ServiceProvider | ||
{ | ||
public function boot() | ||
{ | ||
$this->publishes([ | ||
__DIR__ . '/../config/badwords.php' => config_path('badwords.php'), | ||
], 'config'); | ||
} | ||
|
||
public function register() | ||
{ | ||
$this->mergeConfigFrom( | ||
__DIR__ . '/../config/badwords.php', | ||
'badwords' | ||
); | ||
|
||
$this->app->singleton(PureTextFilterService::class, function () { | ||
return new PureTextFilterService(); | ||
}); | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
namespace YasserElgammal\PureText\Services; | ||
|
||
use Illuminate\Support\Facades\Config; | ||
|
||
class PureTextFilterService | ||
{ | ||
/** | ||
* Replace bad words in the given text with a specified replacement. | ||
* | ||
* @param string $text | ||
* @return string | ||
*/ | ||
public function filter($text) | ||
{ | ||
$badWords = Config::get('badwords.words', []); | ||
$replacement = Config::get('badwords.replacement', '***'); | ||
|
||
return str_ireplace($badWords, $replacement, $text); | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
namespace YasserElgammal\PureText\Traits; | ||
|
||
use YasserElgammal\PureText\Services\PureTextFilterService; | ||
|
||
|
||
trait PureTextFilterable | ||
{ | ||
protected static function bootPureTextFilterable() | ||
{ | ||
static::saving(function ($model) { | ||
foreach ($model->filterableAttributes() as $attribute) { | ||
if (isset($model->$attribute)) { | ||
$model->$attribute = app(PureTextFilterService::class)->filter($model->$attribute); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Define the list of attributes that need to be filtered. | ||
* @return array | ||
*/ | ||
public function filterableAttributes() | ||
{ | ||
return property_exists($this, 'filterable') ? $this->filterable : []; | ||
} | ||
} |