PHP code example of jundayw / laravel-sensitive

1. Go to this page and download the library: Download jundayw/laravel-sensitive library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jundayw / laravel-sensitive example snippets




namespace App\Cloud;

use Illuminate\Support\Collection;
use Jundayw\LaravelSensitive\Contracts\InterceptorInterface;

class CloudInterceptor implements InterceptorInterface
{
    public function handle(string $content, string $field): Collection
    {
        return collect([
            // [
            //     'stop_type'   => 'REPLACE',
            //     'stop_words'  => '小额贷款',
            //     'replacement' => 'xxx',
            // ],
        ]);
    }

}

Sensitive::username(string $content);

Sensitive::nickname(string $content);

Sensitive::message(string $content);

Sensitive::content(string $content);

Sensitive::filter(string $content, string $field = 'username|nickname|message|content', int $scope = SensitiveInterface::STATUS_ALL);



use Jundayw\LaravelSensitive\Sensitive;

public function handle(SensitiveInterface $sensitive): void
{
    $content  = '本校小额贷款,安全、快捷、方便、无抵押,随机随贷,当天放款,上门服务。';
    $instance = $sensitive->content($content);

    if ($instance->isBlock()) {
        // 含有黑名单词语
    }

    if ($instance->isReview()) {
        // 含有待审核词语
    }

    if ($instance->isReplace()) {
        // 含有敏感词词语已被替换
        $content = $instance->getContent();
    }

    var_dump($content);
}



use Jundayw\LaravelSensitive\Facades\Sensitive;

try {
    $content  = '本校小额贷款,安全、快捷、方便、无抵押,随机随贷,当天放款,上门服务。';
    $instance = Sensitive::listen(SensitiveInterface::STATUS_REVIEW | SensitiveInterface::STATUS_BLOCK, function () {
        throw new Exception('含有敏感词');
    })->content($content);
    var_dump($instance->getContent());
} catch (Exception $exception) {
    // $exception->getMessage();
}



use Jundayw\LaravelSensitive\Contracts\SensitiveInterface;
use Jundayw\LaravelSensitive\Events\SensitiveReviewEvent;
use Jundayw\LaravelSensitive\Facades\Sensitive;

$content  = '本校小额贷款,安全、快捷、方便、无抵押,随机随贷,当天放款,上门服务。';
$instance = Sensitive::listen(
    SensitiveInterface::STATUS_REVIEW | SensitiveInterface::STATUS_BLOCK,
    function (string $content, string $field, int $scope, Collection $collect, SensitiveInterface $sensitive) {
        return $sensitive->replacement($content, $collect);
    })->content($content);
// 获取替换后短语
$content = $instance->getContent();
// 正常进行业务处理
$user = User::create([
    'nickname' => $content,
]);
// 通过事件对业务做异步处理
$instance->event(SensitiveInterface::STATUS_REVIEW | SensitiveInterface::STATUS_BLOCK, SensitiveReviewEvent::class);
$instance->dispatch($user, $instance->getContent(), $instance->getRaw());



use Jundayw\LaravelSensitive\Contracts\SensitiveInterface;
use Jundayw\LaravelSensitive\Events\SensitiveBlockEvent;
use Jundayw\LaravelSensitive\Events\SensitivePassEvent;
use Jundayw\LaravelSensitive\Events\SensitiveReplaceEvent;
use Jundayw\LaravelSensitive\Events\SensitiveReviewEvent;
use Jundayw\LaravelSensitive\Facades\Sensitive;

$content  = '本校小额贷款,安全、快捷、方便、无抵押,随机随贷,当天放款,上门服务。';

// listen 用于设置各状态的监听及回调处理函数
$instance = Sensitive::listen(SensitiveInterface::STATUS_BLOCK, function (string $content) {
    return $content;// @todo block
})->listen(SensitiveInterface::STATUS_REVIEW, function (string $content) {
    return $content;// @todo review
})->listen(SensitiveInterface::STATUS_REPLACE, function (string $content) {
    return $content;// @todo replace
})->listen(SensitiveInterface::STATUS_PASS, function (string $content) {
    return $content;// @todo pass
})->listen(SensitiveInterface::STATUS_ALL, function (string $content) {
    return $content;// @todo any
})->content($content);

var_dump($instance->isBlock());
var_dump($instance->isReview());
var_dump($instance->isReplace());
var_dump($instance->isPass());
var_dump($instance->getScope());
var_dump($instance->getRaw());
var_dump($instance->getContent());
var_dump($instance->getValues(SensitiveInterface::STATUS_BLOCK | SensitiveInterface::STATUS_REPLACE));
var_dump($instance->getStopWords(SensitiveInterface::STATUS_ALL));

// event 用于配置异步回调事件,方便对违规敏感词进一步做处理,如:修改状态为待审核、禁言、封号等后续操作
$instance
    //->event(SensitiveInterface::STATUS_ALL, SensitivePassEvent::class)
    ->event(SensitiveInterface::STATUS_BLOCK, SensitiveBlockEvent::class)
    ->event(SensitiveInterface::STATUS_REVIEW, SensitiveReviewEvent::class)
    ->event(SensitiveInterface::STATUS_REPLACE, SensitiveReplaceEvent::class)
    ->event(SensitiveInterface::STATUS_PASS, SensitivePassEvent::class)
    ->dispatch(...);
shell
php artisan vendor:publish --provider="Jundayw\LaravelSensitive\SensitiveServiceProvider"
shell
php artisan vendor:publish --tag=sensitive-config
shell
php artisan vendor:publish --tag=sensitive-migrations
shell
php artisan migrate --path=database/migrations/2022_08_31_182223_create_sensitive_table.php
shell
php artisan db:seed --class=SensitiveSeeder
shell
php artisan vendor:publish --tag=sensitive-migrations