Skip to content

Commit

Permalink
Besm Allah
Browse files Browse the repository at this point in the history
  • Loading branch information
YasserElgammal committed Nov 8, 2024
0 parents commit 9445929
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## Pure Text Package
for pure content
24 changes: 24 additions & 0 deletions composer.json
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"
]
}
}
}
6 changes: 6 additions & 0 deletions config/badwords.php
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
];
28 changes: 28 additions & 0 deletions src/PureTextServiceProvider.php
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();
});
}
}
22 changes: 22 additions & 0 deletions src/Services/PureTextFilterService.php
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);
}
}
29 changes: 29 additions & 0 deletions src/Traits/PureTextFilterable.php
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 : [];
}
}

0 comments on commit 9445929

Please sign in to comment.