tourze/sensitive-text-detect-bundle

敏感文本识别服务,提供文本内容的敏感性检测功能

Installs: 147

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

pkg:composer/tourze/sensitive-text-detect-bundle

0.0.1 2025-04-20 20:48 UTC

This package is auto-updated.

Last update: 2025-11-01 19:24:06 UTC


README

English | 中文

Latest Version Build Status Total Downloads License Coverage Status

A Symfony bundle for detecting sensitive text in content. This bundle provides a flexible interface for implementing content filtering and sensitive text detection with user context support.

Features

  • Simple Integration: Easy to integrate with any Symfony application
  • Flexible Interface: Replaceable detector implementation through the service container
  • Context-Aware: Support for user-specific detection with UserInterface integration
  • Framework Native: Built on Symfony's dependency injection and configuration system
  • Extensible: Default implementation that can be easily replaced with custom logic

Requirements

  • PHP 8.1+
  • Symfony 6.4+

Installation

Install via Composer:

composer require tourze/sensitive-text-detect-bundle

Quick Start

Register the Bundle in your Symfony application

// config/bundles.php
return [
    // ...
    Tourze\SensitiveTextDetectBundle\SensitiveTextDetectBundle::class => ['all' => true],
];

Inject and use the service

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Tourze\SensitiveTextDetectBundle\Service\SensitiveTextDetector;

class TextCheckController extends AbstractController
{
    public function __construct(
        private readonly SensitiveTextDetector $sensitiveTextDetector,
    ) {
    }

    #[Route('/check-text', name: 'app_check_text')]
    public function checkText(): Response
    {
        $text = 'Sample text to check';
        $isSensitive = $this->sensitiveTextDetector->isSensitiveText($text);
        
        return $this->json([
            'is_sensitive' => $isSensitive,
        ]);
    }
}

Customizing the Detector

By default, the bundle uses DefaultTextSensitiveTextDetector implementation, which always returns false. You can customize the detection logic by implementing the SensitiveTextDetector interface and registering your implementation in the container:

<?php

namespace App\Service;

use Symfony\Component\DependencyInjection\Attribute\AsAlias;
use Symfony\Component\Security\Core\User\UserInterface;
use Tourze\SensitiveTextDetectBundle\Service\SensitiveTextDetector;

#[AsAlias(SensitiveTextDetector::class)]
class CustomSensitiveTextDetector implements SensitiveTextDetector
{
    public function isSensitiveText(string $text, ?UserInterface $user = null): bool
    {
        // Implement your custom sensitive text detection logic
        return str_contains($text, 'sensitive-word');
    }
}

API Reference

SensitiveTextDetector Interface

interface SensitiveTextDetector
{
    /**
     * Check if text contains sensitive content
     *
     * @param string $text The text to check
     * @param UserInterface|null $user Optional user context
     * @return bool True if text is sensitive, false otherwise
     */
    public function isSensitiveText(string $text, ?UserInterface $user = null): bool;
}

Default Implementation

The bundle includes a DefaultTextSensitiveTextDetector that always returns false. This is intended as a placeholder that you should replace with your own implementation.

Testing

Run tests:

./vendor/bin/phpunit packages/sensitive-text-detect-bundle/tests

Contributing

Contributions are welcome! Please ensure:

  1. All tests pass
  2. Code follows PSR-12 standards
  3. New features include tests
  4. Documentation is updated

License

This package is released under the MIT License. See the LICENSE file for details.