<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mohammed-abd-razaq / laravel-toxicity-filter example snippets
use Packages\ToxicityFilter\Facades\ToxicityFilter;
// Analyze English content
$result = ToxicityFilter::analyze("This is some content to check");
echo $result->getToxicityScore(); // 0.85
echo $result->getProvider(); // 'openai'
var_dump($result->getCategories()); // ['harassment', 'hate']
// Analyze Arabic content (automatic language detection)
$arabicResult = ToxicityFilter::analyze("مرحبا بالعالم");
// Analyze multilingual content
$multilingualResult = ToxicityFilter::analyze("Hello مرحبا world");
// Quick checks (uses language-specific thresholds)
if (ToxicityFilter::shouldBlock($content)) {
// Block the content
}
if (ToxicityFilter::shouldFlag($content)) {
// Flag for manual review
}
if (ToxicityFilter::shouldWarn($content)) {
// Show warning to user
}
// Use OpenAI specifically
$result = ToxicityFilter::analyze($content, 'openai');
// Use Perspective API specifically
$result = ToxicityFilter::analyze($content, 'perspective');
// Get available providers
$providers = ToxicityFilter::getAvailableProviders();
// In app/Http/Kernel.php
protected $routeMiddleware = [
// ... other middleware
'toxicity-filter' => \Packages\ToxicityFilter\Middleware\ToxicityFilterMiddleware::class,
];
// In your routes file
Route::post('/comments', [CommentController::class, 'store'])
->middleware('toxicity-filter');
// Or specify fields to check
Route::post('/posts', [PostController::class, 'store'])
->middleware('toxicity-filter:title,content,description');
use Packages\ToxicityFilter\Contracts\ToxicityFilterInterface;
class ContentModerationService
{
public function __construct(
private ToxicityFilterInterface $toxicityFilter
) {}
public function moderateComment(string $content, User $user): array
{
$result = $this->toxicityFilter->analyze($content);
$response = [
'allowed' => true,
'message' => null,
'
// Arabic content is automatically detected
$arabicContent = "مرحبا بالعالم";
$result = ToxicityFilter::analyze($arabicContent);
// Language is automatically detected as 'ar'
// Multilingual content is supported
$mixedContent = "Hello مرحبا world";
$result = ToxicityFilter::analyze($mixedContent);
// Primary language is determined based on character count
// Raw Arabic text with diacritics
$rawArabic = "مَرْحَباً بِالعَالَمِ";
// Package automatically normalizes for analysis
$result = ToxicityFilter::analyze($rawArabic);
use Packages\ToxicityFilter\Jobs\AnalyzeToxicityJob;
// Process large content asynchronously
AnalyzeToxicityJob::dispatch($content, $userId, $options);
use Packages\ToxicityFilter\Contracts\ToxicityProviderInterface;
use Packages\ToxicityFilter\ValueObjects\ToxicityResult;
class CustomProvider implements ToxicityProviderInterface
{
public function analyze(string $content, array $options = []): ToxicityResult
{
// Implement your provider logic
}
public function getName(): string
{
return 'custom';
}
// ... implement other interface methods
}