tourze/user-id-idcard-bundle

用户身份证验证模块,提供身份证号码的存储、验证和管理功能

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

pkg:composer/tourze/user-id-idcard-bundle


README

PHP Version License Symfony Build Status Coverage

English | 中文

A Symfony bundle that provides Chinese ID card identity verification functionality as an extension to the user identity system.

Features

  • Chinese ID card identity management
  • Integration with user identity system
  • Doctrine ORM entity for ID card storage
  • Service decorator pattern for extensibility
  • Snowflake ID generation support
  • Timestamp and blame tracking

Installation

composer require tourze/user-id-idcard-bundle

Requirements

  • PHP 8.1+
  • Symfony 6.4+
  • Doctrine ORM 3.0+

Dependencies

  • tourze/user-id-bundle: Base identity system
  • tourze/doctrine-snowflake-bundle: Snowflake ID generation
  • tourze/doctrine-timestamp-bundle: Timestamp tracking
  • tourze/doctrine-user-bundle: User blame tracking

Quick Start

1. Enable the Bundle

Add the bundle to your config/bundles.php:

return [
    // ... other bundles
    Tourze\UserIDIdcardBundle\UserIDIdcardBundle::class => ['all' => true],
];

2. Update Database Schema

php bin/console doctrine:schema:update --force

3. Basic Usage

use Tourze\UserIDIdcardBundle\Entity\IdcardIdentity;
use Tourze\UserIDIdcardBundle\Service\UserIdentityIdcardService;

// Create ID card identity
$idcardIdentity = new IdcardIdentity();
$idcardIdentity->setIdcard('110101199001011234');
$idcardIdentity->setUser($user);

// Find by ID card number
$identity = $userIdentityService->findByType(
    IdcardIdentity::IDENTITY_TYPE,
    '110101199001011234'
);

// Get all identities for a user
$identities = $userIdentityService->findByUser($user);

Configuration

This bundle extends the tourze/user-id-bundle and requires no additional configuration. It automatically decorates the UserIdentityService to handle ID card identities.

Advanced Usage

Custom Validation

You can extend the ID card validation by creating custom validators:

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class CustomIdcardValidator extends ConstraintValidator
{
    public function validate($value, Constraint $constraint): void
    {
        // Custom validation logic
        if (!$this->isValidIdcard($value)) {
            $this->context->buildViolation($constraint->message)
                ->addViolation();
        }
    }
    
    private function isValidIdcard(string $idcard): bool
    {
        // Implement your custom validation logic
        return true;
    }
}

Service Integration

use Tourze\UserIDIdcardBundle\Service\UserIdentityIdcardService;

class YourService
{
    public function __construct(
        private UserIdentityIdcardService $identityService
    ) {}
    
    public function processUserIdentity(UserInterface $user): void
    {
        $identities = $this->identityService->findByUser($user);
        foreach ($identities as $identity) {
            if ($identity->getIdentityType() === IdcardIdentity::IDENTITY_TYPE) {
                // Process ID card identity
            }
        }
    }
}

Entity Structure

The IdcardIdentity entity includes:

  • id: Snowflake ID (primary key)
  • idcard: Chinese ID card number (18 digits)
  • user: Reference to user entity
  • createTime: Creation timestamp
  • updateTime: Last update timestamp
  • createBy: User who created the record
  • updateBy: User who last updated the record

License

MIT