tourze/idcard-manage-bundle

提供身份证验证、解析生日性别等功能的 Symfony Bundle

Installs: 10

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

pkg:composer/tourze/idcard-manage-bundle

0.0.3 2025-06-14 05:56 UTC

This package is auto-updated.

Last update: 2025-11-01 19:17:35 UTC


README

English | 中文

Latest Version PHP Version Require License Build Status Quality Score Code Coverage Total Downloads

A Symfony bundle for Chinese ID card management and validation.

Features

  • ✅ Chinese ID card format validation
  • ✅ Birthday extraction with custom separator support
  • ✅ Gender detection with enum support (GB/T 2261 standard)
  • ✅ Full Symfony framework integration
  • ✅ Comprehensive test coverage
  • ⚠️ Two-element verification (placeholder for external API integration)

Installation

composer require tourze/idcard-manage-bundle

Quick Start

<?php

use Tourze\IdcardManageBundle\Service\IdcardService;
use Tourze\GBT2261\Gender;

class YourController
{
    public function __construct(
        private IdcardService $idcardService
    ) {}

    public function validateIdcard(string $idNumber): void
    {
        // Validate ID card number
        if ($this->idcardService->isValid($idNumber)) {
            echo "Valid ID card number";
            
            // Get birthday
            $birthday = $this->idcardService->getBirthday($idNumber);
            if ($birthday !== false) {
                echo "Birthday: " . $birthday;
            }
            
            // Get gender
            $gender = $this->idcardService->getGender($idNumber);
            match ($gender) {
                Gender::MAN => echo "Male",
                Gender::WOMAN => echo "Female",
                default => echo "Unknown gender",
            };
        } else {
            echo "Invalid ID card number";
        }
    }
}

Configuration

Add the bundle to your config/bundles.php:

<?php

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

API Documentation

Service Methods

isValid(string $number): bool

Validates if the ID card number follows basic format rules.

getBirthday(string $number, string $sep = '-'): string|false

Extracts the birthday from the ID card number.

  • $number: The ID card number
  • $sep: Date separator (default: '-')
  • Returns: Birthday string or false if invalid

getGender(string $number): Gender

Extracts the gender from the ID card number.

  • Returns: Gender enum value (MAN, WOMAN, UNKNOWN)

twoElementVerify(string $certName, string $certNo): bool

Verifies the ID card number with the holder's name using external API.

  • Currently returns false as it requires third-party API integration

Testing

# Run tests
./vendor/bin/phpunit packages/idcard-manage-bundle/tests

# Run static analysis
php -d memory_limit=2G ./vendor/bin/phpstan analyse packages/idcard-manage-bundle

Contributing

Please see CONTRIBUTING.md for details.

Dependencies

License

The MIT License (MIT). Please see License File for more information.