blaspsoft/blasp
Advanced profanity filtering for Laravel with driver-based detection (regex/pattern/phonetic/pipeline), multi-language support, severity scoring, masking strategies, Eloquent trait, middleware and validation rules, events, and test fakes. Powers blasp.app API.
Installation:
composer require blaspsoft/blasp
php artisan vendor:publish --tag="blasp"
Publish only the config if you don’t need language files.
First Use Case: Check a string for profanity and mask it:
use Blaspsoft\Blasp\Facades\Blasp;
$result = Blasp::check('This is a fucking sentence');
echo $result->clean(); // "This is a ******* sentence"
Quick Validation: Use the validation rule in a form request:
$request->validate(['comment' => 'required|blasp_check']);
config/blasp.php for default settings (driver, language, masking).Blasp facade for fluent API usage.Blaspsoft\Blasp\Rules\Profanity for customizable checks.Fluent API Chaining:
$cleanText = Blasp::in('spanish')
->mask('#')
->withSeverity(Severity::High)
->check($text)
->clean();
Middleware Integration: Protect routes with built-in middleware:
Route::post('/submit', SubmitController::class)
->middleware('blasp:sanitize,moderate');
Configure fields to check in config/blasp.php under middleware.fields.
Eloquent Model Sanitization:
Add the Blaspable trait to models:
class Comment extends Model {
use Blaspable;
protected $blaspable = ['body', 'title'];
}
Override defaults per model (e.g., blaspMode = 'reject').
Batch Processing: Check multiple strings at once:
$results = Blasp::checkMany(['text1', 'text2']);
foreach ($results as $result) {
if ($result->isOffensive()) {
// Handle profanity
}
}
Custom Validation Logic:
Use the Profanity rule object for granular control:
$validator = Validator::make($data, [
'bio' => ['required', Profanity::in('french')->maxScore(30)]
]);
@clean($text) to sanitize and escape text in views.Str::cleanProfanity() or Str::of($text)->cleanProfanity() for quick checks.ProfanityDetected or ModelProfanityDetected to log or notify admins:
Event::listen(ModelProfanityDetected::class, function ($event) {
Log::warning("Profanity detected in {$event->attribute}", $event->result->toArray());
});
Blasp::fake() to mock results in tests:
Blasp::fake([
'This is clean' => false,
'Profane text' => true,
]);
Performance with Regex Driver:
regex driver is the most thorough but can be slow for large texts or high traffic.pattern driver for speed or cache results (config/blasp.php > cache.enabled = true).False Positives in Phonetic Driver:
phonetic driver may flag words like "fork" or "duck" as profanity.config/blasp.php > drivers.phonetic.false_positives or config/blasp.php > false_positives.Separator Limits in Regex:
f--u--c--k).phonetic driver in a pipeline.Case Sensitivity:
Blasp::caseSensitive()->check($text) if needed (though this is rare).Middleware Field Exclusions:
middleware.except are always skipped, even if explicitly included in middleware.fields.except doesn’t accidentally block critical fields.Model Reject Mode:
reject mode, the model won’t save if profanity is detected. Ensure you handle the ProfanityRejectedException gracefully:
try {
$model->save();
} catch (ProfanityRejectedException $e) {
return back()->withErrors(['field' => 'Profanity not allowed.']);
}
Language-Specific Quirks:
Blasp::in('language')->check($text) to verify behavior.Inspect Results:
Dump the Result object to debug matches:
$result = Blasp::check($text);
dd($result->words()); // Collection of MatchedWord objects with positions/severity
Enable Events for Logging:
Set 'events' => true in config/blasp.php to log profanity detections via ProfanityDetected events.
Check Cache: If using caching, clear it during development:
php artisan cache:clear
Override Default Drivers: Temporarily switch drivers to isolate issues:
Blasp::driver('pattern')->check($text); // Faster but less thorough
Custom Drivers:
Create a driver by implementing DriverInterface:
use Blaspsoft\Blasp\Core\Contracts\DriverInterface;
class CustomDriver implements DriverInterface {
public function check(string $text, Dictionary $dictionary): Result {
// Implement your logic
return new Result($text, $matches);
}
}
Register it in Blasp::extend('custom', CustomDriver::class).
Dynamic Config:
Override config per request using Blasp::configure():
Blasp::configure(function ($config) {
$config->set('language', 'spanish');
$config->set('severity', Severity::Moderate);
});
Custom Masking: Use a callback for dynamic masking:
Blasp::mask(fn($word, $length) => str_repeat('X', $length))->check($text);
Language-Specific Dictionaries: Extend language files by publishing and modifying:
php artisan vendor:publish --tag="blasp-languages"
Add words to resources/lang/blasp/{language}.php.
Severity Tuning:
Adjust severity thresholds in config/blasp.php or dynamically:
Blasp::withSeverity(Severity::High)->check($text);
pipeline, ensure sub-drivers are listed in config/blasp.php > drivers.pipeline.drivers.cache.ttl to a reasonable value (e.g., 86400 for 24 hours) to avoid stale results.config/blasp.php are merged with per-check allow/block lists. Block lists override allows.Blasp::fake([
'clean text' => false,
'bad text' => true,
]);
$this->assertTrue(Blasp::check('bad text')->isOffensive());
$this->assertEquals('****', Blasp::check('bad')->clean());
How can I help you explore Laravel packages today?