PHP code example of jundayw / laravel-interceptor

1. Go to this page and download the library: Download jundayw/laravel-interceptor 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-interceptor example snippets




namespace App\Filter\Cloud;

use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;
use Jundayw\LaravelInterceptor\Contracts\Filter;

class CloudFilter implements Filter
{
    use Macroable;

    public function __construct()
    {
        //
    }

    public function handle(string $content, string $type): Collection
    {
        // cloud filter
        // $matches
        // $keywords
        // $replacement
        return collect([
            'type' => (string)$type,// 验证类型
            'matches' => (array)$matches,// 内容中匹配到的敏感词集
            'keywords' => (string)$keywords,// 敏感词
            'replacement' => (string)$replacement,// 替换词或替换规则
        ]);
    }

}

use Jundayw\LaravelInterceptor\Contracts\Interceptor;

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

    $content = $interceptor->filter(content: $content, type: 'message', pass: function ($matches, $content) {
        return $content;
    }, review: function ($matches, $content) {
        // event(new ReViewEvent(...));
        return $content;
    }, replace: function ($matches, $content, $collect) {
        return str_replace(current($matches), $collect->get('replacement'), $content);
    }, block: function ($matches, $content) {
        throw new \Exception('含有违规词');
    });

    echo $content;
}

use Jundayw\LaravelInterceptor\Support\Facades\Interceptor;

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

    $content = Interceptor::filter(content: $content, type: 'message');

    echo $content;
}
shell
php artisan vendor:publish --provider="Jundayw\LaravelInterceptor\InterceptorServiceProvider"
shell
php artisan vendor:publish --tag=interceptor-config
shell
php artisan vendor:publish --tag=interceptor-migrations
shell
php artisan migrate --path=database/migrations/2022_08_31_182223_create_interceptor_table.php
shell
php artisan db:seed --class=InterceptorSeeder
shell
php artisan vendor:publish --tag=interceptor-migrations